[Tex/LaTex] Change color in finished pdf file


Suppose I have a finished PDF file, say a figure generated from some software, where the ink is black. I would include it in a document. Suppose I want to include it in a beamer presentation with black background.

Is there a way to configure the include so i can specify whatever is black should be displayed white? If not, what command line tools could I use to process the PDF file?

Best Answer

Easy case with uncolored PDF and pdfTeX (unhappily very seldom):

The PDF file contains some drawings without explicit color settings and the image will be included by the pdftex driver of the graphics package.

The following file generates such an image:

\fbox{Hello World}

enter image description here

The following file includes the image (t.pdf) and changes its colors.

Some text.\\
Some text.\\
Some text.

enter image description here

This trick does not work with other drivers, because the other drivers normalize the color before the image is included. If pdftex.def is given the option resetcolor then the color is set to \normalcolor during image inclusion.

Manual color fixes

With some knowledge of the internals structures the PDF file can be fixed manually. First the compressed page contents needs to be uncompresed, e.g.:

pdftk test.pdf cat output test-uncompress.pdf uncompress

Then the pages content streams need to be identified and the color operations can be either changed or deleted (by overwriting with spaces). Best is not to change the size of the object otherwise the file offsets of the objects in the xref table needs to be updated. BTW, also PDF (or PS) use % as comment char. Then the file can be recompressed.

Or the PDF file is converted to PostScript (e.g. with pstopdf from xpdf). Editing is easier if changing the file size does not matter. PostScript does not have a xref table. However, the detection of the color operators can be more difficult, because often they are renamed or hidden in procedures. Unlike PDF, PostScript is a programming language.

Programs for processing vector images like Inkscape

If the PDF file can be successfully imported in Inkscape or similar programs, the colors could be changed there.

Bitmap conversion

As last resort the image can be converted to a bitmap image (ghostscript and other convertes). That means quality loss because of pixel data. But many image processing programs should be able to change the colors.

Generating inverse colors can even be done by a feature of the PDF format that is supported by pdftex.def. Colors can be inverted via the /Decode array (except images with indexed color spaces). Each color component has then two float values inbetween 0 and 1. Thus the number of color components must be known (Mono: 1, RGB: 3, CMYK: 4)

\includegraphics[decodearray=1 0 1 0 1 0]{rgbimage.png}

For example, the ghostscript device png16m can be used to generate PNGs for this usage. More details are explained in the PDF reference. The options of pdftex.def are shortly explained in the file itself.

Related Question