Reverse white and black colors in a PDF

后端 未结 4 696
梦如初夏
梦如初夏 2021-02-02 17:08

Given a black and white PDF, how do I reverse the colors such that background is black and everything else is white?

Adobe Reader does it (Preferences -> Accessibili

相关标签:
4条回答
  • 2021-02-02 17:46

    None of the previously posted solutions worked for me so I wrote this simple bash script. It depends on pdftk and awk. Just copy the code into a file and make it executable. Then run it like:

    $ /path/to/this_script.sh /path/to/mypdf.pdf
    

    The script:

    #!/bin/bash                                                                      
    pdftk "$1" output - uncompress | \                                               
    awk '                                                                            
      /^1 1 1 / {                                                                    
        sub(/1 1 1 /,"0 0 0 ",$0);                                                   
        print;                                                                       
        next;                                                                        
      }                                                                              
    
      /^0 0 0 / {                                                                    
        sub(/0 0 0 /,"1 1 1 ",$0);                                                   
        print;                                                                       
        next;                                                                        
      }                                                                              
    
      { print }' | \                                                                 
    pdftk - output "${1/%.pdf/_inverted.pdf}" compress
    

    This script works for me but your mileage may vary. In particular sometimes the colors are listed in the form 1.000 1.000 1.000 instead of 1 1 1. The script can easily be modified as needed. If desired, additional color conversions could be added as well.

    For me, the pdf2ps -> edit -> ps2pdf solution did not work. The intermediate .ps file is inverted correctly, but the final .pdf is the same as the original. The final .pdf in the suggested gs solution was also the same as the original.

    0 讨论(0)
  • 2021-02-02 17:49

    If you have the Adobe PDF printer installed, you go to Print -> Adobe PDF -> Advanced... -> Output area and select the "Invert" checkbox. Your printed PDF file will then be inverted permanently.

    0 讨论(0)
  • 2021-02-02 17:56

    You can run the following Ghostscript command:

    gs -o inverted.pdf    \
       -sDEVICE=pdfwrite  \
       -c "{1 exch sub}{1 exch sub}{1 exch sub}{1 exch sub} setcolortransfer" \
       -f input.pdf
    

    Acrobat will show the colors inverted.

    The four identical parts {1 exch sub} are meant for CMYK color spaces and are applied to C(yan), M(agenta), Y(ellow) and (blac)K color channels in the order of appearance.

    You may use only three of them -- then it is meant for RGB color spaces and is applied to R(ed), G(reen) and B(lue).

    Of course you can "invent" you own transfer functions too, instead of the simple 1 exch sub one: for example {0.5 mul} will just use 50% of the original color values for each color channel.


    Note: Above command will show ALL colors inverted, not just black+white!

    Caveats:

    1. Some PDF viewers won't display the inverted colors, notably Preview.app on Mac OS X, Evince, MuPDF and PDF.js (Firefox PDF Viewer) won't. But Chrome's native PDF viewer PDFium will do it, as well as Ghostscript and Adobe Reader.

    2. It will not work with all PDFs (or for all pages of the PDF), because it is also dependent on how exactly the document's colors are defined.


    Update

    Command above updated with added -f parameter (required) before the input.pdf. Sorry for not noticing this flaw in my command line before. I got aware of it again only because some good soul gave it its first upvote today...
    Additional update: The most recent versions of Ghostscript do not require the added -f parameter any more. Verified with v9.26 (may also be true even with v9.25 or earlier versions).

    0 讨论(0)
  • 2021-02-02 18:08

    Best method would be to use "pdf2ps - Ghostscript PDF to PostScript translator", which convert the PDF to PS file.

    Once PS file is created, open it with any text editor & add {1 exch sub} settransfer before first line.

    Now "re-convert" the PS file back to PDF with same software used above.

    0 讨论(0)
提交回复
热议问题