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?

-
maybe pdftk can do help you. – kiss my armpit Jul 28 '12 at 22:05
To manipulate pdf documents (extracting parts, changing colors, etc.) I recommend Inkscape. which can open/import pdf documents page-wise. – jofel Aug 13 '12 at 8:41

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:

\documentclass{article}
\usepackage[active,tightpage,floats]{preview}
\begin{document}
\begin{figure}
\Huge\bfseries\sffamily
\setlength{\fboxrule}{2pt}
\fbox{Hello World}
\end{figure}
\end{document}


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

\documentclass{article}
\usepackage{color}
\usepackage{graphicx}
\pagecolor{black}
\color{white}
\begin{document}
\Huge\bfseries
\noindent
Some text.\\
\includegraphics{t.pdf}\\
Some text.\\
\textcolor{yellow}{\includegraphics{t.pdf}}\\
Some text.
\end{document}


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.

-
If the pdf is to be edited, look for the rg (fill color) or the RG (line color) operator in the content stream in the case of RGB colors. The three numbers before the operand indicate the RGB values. For CMYK, the operators are called k or K, respectively, and the 4 CMYK color values precede them. (Was just in the same situation.) – sebschub Jan 12 '14 at 14:30

protected by doncherryMar 18 '14 at 14:14

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.