Changing the text and background color of a PDF file

16,146

Solution 1

There is no way to do this directly, with no (Free Software or gratis) tool I'm aware of. (Because in the general case, you'll have to change all colors of the PDF pages, not just the background alone, so you can still have some contrast and color differences.)

What you describe for Adobe Reader does not change the PDF file itself, it changes the way the application renders the pages (by inverting colors, or similar). The PDF remains the same during and after viewing it.

However, you might be able to achieve a similar thing by applying a suitable ICC color profile to the input PDF and produce, with the help of (a very recent version of) Ghostscript, a new output PDF from this.

The question would remain: what IS a "suitable" ICC color profil for your purpose??


I've shortly considered to apply a gray-ish background to the PDF with the help of pdftk ... background ... command line. But this would probably make some or many PDFs unreadable. (A black background would surely make it unreadable, because most text is black and would remain so.)

To create a PDF page (A4 size) which could serve as the gray background, you could use Ghostscript: gs -o gray.pdf -sDEVICE=pdfwrite -g5950x8420 -c ".8 setgray 0 0 595 842 rectfill showpage".

Then apply it to your original PDF (A4): pdftk original.pdf background gray.pdf output orig-with-backgr.pdf.

Note, this will only change the background of these pages (or those areas of pages), where the original background is transparent, as most text-based PDFs are. It will not work for pages or areas where the background is opaque white or color.)


You can also achieve a permanent color change (inverse colors) quite easily with the help of ImageMagick. But this will at the same time munch and make mincemeat of your nice vector PDFs, converting them into complete-raster image pages: convert nice.pdf -alpha off -invert inverted-colors-ugly-raster.pdf.


Finally, here is a rather unreliable way to accomplish the inverting of colors with the help of Ghostscript. It sets up a colortransfer function for the output PDF file:

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

It is unreliable, because not every PDF viewer will honor that setting. I've tested it a few times in the past...

These viewers DO SHOW inverted colors:

  1. Adobe Reader
  2. Adobe Acrobat
  3. gv
  4. Ghostscript/gs
  5. Chrome's native PDF renderer ('pdfium')

These ones DON'T SHOW inverted colors:

  1. Chrome with PDF.js
  2. Firefox with PDF.js
  3. Zathura
  4. MuPDF

Solution 2

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

Solution 3

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

Solution 4

  1. Install nodejs.
  2. npm i -g serve
  3. In directory with pdfs run: serve
  4. Open http://localhost:5000 in Chrome and click on some file.
  5. Install Chrome extension Dark Reader
  6. Dark Reader > Toggle localhost:5000

enter image description here

Share:
16,146

Related videos on Youtube

Bedford
Author by

Bedford

Updated on September 19, 2022

Comments

  • Bedford
    Bedford over 1 year

    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.

  • David van Driessche
    David van Driessche almost 9 years
    As a (perhaps too geeky) addition to your answer, there are actually tools such as PitStop or pdfToolbox (warning - affiliation) that can apply such a transfer curve once it's there (in other words, calculate new colors taking into account the transfer curve). That would make it permanent and thus available for all viewers :)
  • Kurt Pfeifle
    Kurt Pfeifle almost 9 years
    @DavidvanDriessche: Thanks David -- of course I meant to say that I was not aware of any 'Free' (as in liberty) or 'free' (as in gratis) tool I'm aware of... I totally forgot to mention commercial utilities, although I had intended to when I wrote up my answer.
  • SalahAdDin
    SalahAdDin almost 4 years
    Does this enable to print in black and white?
  • User12345
    User12345 over 3 years
    you can add w3m to the list of viewers that do show inverted colors.