Reverse white and black colors in a PDF

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

提交回复
热议问题