Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the text and background color of a PDF file

I'd like to change the background color and text color programmatically in PDF documents so that they're nicer to read at night (kinda like in Adobe Reader: Edit -> Preferences -> Accessibility -> Replace Document Colors).

Is there any good command line tool or API for Windows that can do that?

So far I haven't found any. It's OK if it needs to save the newly colored PDF into a new file.

like image 512
Bedford Avatar asked Apr 29 '15 11:04

Bedford


2 Answers

Just in case the OP doesn't really need to change the PDF document colors permanently, but only wants a PDF viewer other than Acrobat that can do similarly change the displayed colors...

MuPDF: MuPDF is a lightweight PDF viewer (amongst other things). It can invert the displayed colors with the simple stroke of the i for any document while it is open. MuPDF is also available for Windows (and iOS, and Android, and OSX, and Linux). (It is made by the same company which brought us Ghostscript).

MuPDF screenshots here: "normal" view (left) and "inverted" view (right)

 

SumatraPDF: This is a very popular alternative PDF viewer for Windows. Its PDF rendering engine is based on MuPDF. Hitting . in presentation mode, it changes background to black. Hitting w in presentation mode, it changes background to white. (I don't think it can also invert all colors, but I do not have the latest release available right now to check.) For startup adding -invert-colors to the command line, it will invert the colors for the rendered document.

Zathura: A lightweight PDF viewer for Linux and OSX, which can be controlled by Vim-like keyboard shortcuts. ctrl+r will re-color the rendering of any opened document. Background will change to dark, texts will change to bright gray (however it will not invert a, say blue text to yellow, like MuPDF does). I'm not sure if it is available on Windows too.

Evince: The Gnome PDF viewer, available for Linux, OSX and Windows. It can invert the colors of the open document too; the keyboard shortcut is ctrl+i.

XPDF: XPDF is quite an ancient PDF viewer for Unix + Linux (not sure if there is a Windows version available -- maybe in Cygwin). It has a startup option in its command line: xpdf -rv -papercolor "#333333" file.pdf will invert the colors (-rv is for reverse video, -papercolor lets you change the background to something different from pure black [as any inverted white would become]).

like image 125
Kurt Pfeifle Avatar answered Oct 17 '22 10:10

Kurt Pfeifle


As you asked for an API, I'll throw one additional possibility in the mix. It is actually possible to write a plug-in for Adobe Acrobat (should be possible for Adobe Reader too, but Reader plug-ins are more difficult) that interferes with display.

A long time ago I wrote code for Enfocus PitStop to implement a wireframe rendering mode for PDF files inside of Adobe Acrobat. Click a button and the display changes to wireframe, click again and you have your normal view. This works because you can (as a plug-in) modify the display list (the list of objects) drawn by Acrobat.

This means that to draw your special display mode you could create a new display list (or modify the existing one) so that it has a rectangle at the very back in the color that you want and then modify the color of all objects in the display list to suit your needs.

This is relatively complex as it is, what makes it more complex is that - if you don't want your changes to affect the PDF file on disk, you have to intercept a myriad of Acrobat notifications and undo your changes. For example, if the user attempts to save the PDF document while viewing in your display mode, you have to make sure you are warned about that and undo the changes during the save. Adobe Acrobat makes this possible because it sends you notifications before and after the save process but it's still a serious job to make sure nothing gets screwed up.

But it's a absolutely cool and very flexible way to implement what you were after. Just make sure you have more than a couple of weeks to implement it :)

like image 36
David van Driessche Avatar answered Oct 17 '22 09:10

David van Driessche