Reverse white and black colors in a PDF

后端 未结 4 699
梦如初夏
梦如初夏 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.

提交回复
热议问题