Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reverse white and black colors in a PDF

Tags:

pdf

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 -> Accessibility) for viewing purposes only in the program. But does not change the document inherently such that the colors are reversed also in other PDF readers.

How to reverse colors permanently?

like image 763
cerebrou Avatar asked May 17 '15 07:05

cerebrou


People also ask

Is there a way to invert colors on a PDF?

Invert colors when rendering a PDF document by selecting View Tab > Invert Colors from the menu. For typical documents, the display will change from "Black on White" to "White on Black".

Can you invert Colours on Adobe Acrobat?

Click the "Custom Color" radio button in the "Document Colors Options" section. Click the color boxes next to the "Page Background" and "Document Text" to select a new color for each category. To invert white, select black or another color. Click the "OK" button on the "Preferences" window to complete the change.

Why does my PDF turn black and white?

REASON. This problem is usually caused by corrupted PDF files produced by outdated scanning software. When Pipeline tries to read such a document, it is unable to read some of the information properly thereby resulting in one of these problems.

How do you reverse an image in PDF?

Open the PDF in Acrobat, and then choose Tools > Edit PDF > Edit . Select the image (or images) - click the image to select it. Under Objects in the right hand panel, click one of the following tools: Flip Vertical Flips the image vertically, on the horizontal axis.


3 Answers

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).

like image 191
Kurt Pfeifle Avatar answered Nov 16 '22 04:11

Kurt Pfeifle


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.

like image 43
Anush Avatar answered Nov 16 '22 06:11

Anush


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.

like image 37
lblb Avatar answered Nov 16 '22 04:11

lblb