ZYinMD
ZYinMD

Reputation: 5069

Permanently change the background color of a pdf book from white to a gentle grey

I'm reading a 800-page PDF book, and the white background color burns my eyes. I want to give it a grey shade, for instance this background color.

I'm looking to modify the file, as opposed to using a viewer that can adjust reading colors, because I already have a preferred viewer.

I found a 2015 answer that achieved it with something like this:

gs -o gray.pdf -sDEVICE=pdfwrite -g85x110 -c ".9 setgray 0 0 85 110 rectfill showpage"

pdftk book.pdf background gray.pdf output new-book.pdf

This works, however, it makes the file 3x larger in size, maybe it's adding a background to every single page. I wonder if there's a less hacky way to simply change the background color.

Upvotes: 1

Views: 1485

Answers (2)

KenS
KenS

Reputation: 31139

You can use a BeginPage procedure with Ghostscript to initially fill the page with a colour, and then render the page contents. If you use the pdfwrite device as the output device then you can create a new PDF file where the background is non-white.

Make a text file on disk (eg gray.ps) with this content

%!
<<
/BeginPage
{
pop
0.9 setgray
0 0
currentpagedevice /PageSize get
aload pop rectfill
}
>> setpagedevice

Then invoke Ghostscript like this:

gs -sDEVICE=pdfwrite -o out.pdf gray.ps in.pdf

You should find that 'out.pdf' has the same content as in.pdf, but the page background is now a light gray. For a darker gray change the value of setgray from 0.9, 0 = black, 1.0 = white.

As I said, if the PDF file does something such as filling the entire page with white this will defeat the background painting, because it will overwrite the background. But I suspect that would defeat any trickery with viewer backgrounds too.

The resulting PDF in this case will work with any viewer since the PDF file now has the background 'built in'.

Upvotes: 2

K J
K J

Reputation: 11727

"a preferred viewer"

usually may include that option, Acrobat, SumatraPDF, Okular (the widest set of choices) and several others do, its only browser inbuilt extensions may not.

This was mentioned by @mkl in a previous 2014 answer to a similar opposing question.

The backdrop is nominally white, although varying according to the actual properties of the medium. However, some conforming readers may choose to provide a different backdrop, ... https://stackoverflow.com/a/20946491/10802527

Intrigued that elsewhere you recommend SumatraPDF which has such an advanced options setting that can be switched at will from command line start (agreed it not that easy) via shortcuts.

Simpler as per OP comment

for Sumatra users who find this answer: an easier way is to go to Settings > Advanced Options, then change the "BackgroundColor =" value, to something like #eeeeee. – @ZYinMD

Many users ask how to alter between two states like an on off toggle so for those its easiest to set via command line or two+ shortcuts.

enter image description here

Works in version 3.3.3 but a short term issue with command line affects 3.4.0/1 (should be working again in 3.4.2 onward)

-set-color-range < text-hexcolor > < background-hexcolor > so at end of a shortcut such as "C:\Program Files\SumatraPDF\SumatraPDF.exe" -set-color-range #------ #------

For natural colours you start with

-set-color-range #000000 #ffffff

For muted background colour you start with

-set-color-range #000000 #c0c0c0

enter image description here

But it will alter the full colour gamut. You can also use hotkey i to invert them.

enter image description here

Another 2 advanced background options are available for outside page boundary too.

Upvotes: 1

Related Questions