PDF without color operators
If you are lucky and the PDF image does not contain any color operators and you are using pdfTeX/LuaTeX, then a simple \textcolor
works:
\textcolor{red}{\includegraphics{myimage.pdf}}
PDF with color operators
The color operators can be indentified and removed from the PDF file. Usually the page contents streams are compressed. pdftk
can uncompress them:
pdftk myimage.pdf output temp.pdf uncompress
Then you need to find the page object. Look for /Page
(not /Pages
), e.g.:
4 0 obj <<
/Type /Page
/Contents 5 0 R
/Resources 3 0 R
/MediaBox [0 0 595.276 841.89]
/Parent 7 0 R
>> endobj
Then the number after /Contents
is the object number for the page contents stream.
In this case the number is 5
and we search for 5 0 obj
:
5 0 obj <<
/Length 254
>>
stream
0 g 0 G
1 0 0 1 14.944 500.863 cm
q
10 0 0 10 0 0 cm
...
endstream
Now we have 254 bytes between stream
and endstream
to scan for color operators.
The operators follows the operands (postfix notation). The main color operators
(<n>
is a number between 0 and 1):
<n> G
, <n> g
for gray color model
<n> <n> <n> RG
, <n> <n> <n> rg
for RGB color model
<n> <n> <n> <n> K
, <n> <n> <n> <n> k
for CMYK color model
- others are
CS
, cs
, SC
, sc
, SCN
, scn
with parameters each.
In the case of the monochrome black image, I would expect 0 g
and 0 G
near the beginning of the stream. The above example contains the two operations: 0 g 0 G
.
If the PDF file is edited great care is needed the size of the object is not changed.
First the length of the stream is given in the /Length
entry. And the file offsets of the objects are written in the xref
table of the PDF file. Therefore the color operations are deleted by overwriting. Also the editor should not do its own editing by changing line end characters, for example.
If the color operations are on a line by its own, then it is enough to replace the
first character with the percent char, that is also a comment char in PostScript and PDF, in the example above:
% g 0 G
Or overwrite the entry by spaces (
, ordinary spaces with character code 32).
The the file is recompressed:
pdftk temp.pdf output myimage-colorless.pdf compress
And the trick with pdfTeX will work:
\textcolor{red}{\includegraphics{myimage-colorless.pdf}}
Low level editing of color operations via PostScript
If it is difficult to edit the PDF file directly (compressed, size requirements, editor requirement, adding colors, …), then the PDF can be converted to PostScript.
My first choice is pdftops
from xpdf
:
pdftops -eps myimage.pdf myimage.eps
It uses the same operator names as in the PDF file. In case of pdftops
the page starts after %%EndSetup
(with pdfStartPage
). PostScript is a programming language, in general it can be difficult to locate the color operators: setgray
, setrgbcolor
, setcmykcolor
. In case of pdftops
, new operators are defined with the same names as the PDF operators. Thus the same can be done as described in the previous section. However, the size of the file can easily change, also the line endings are less critical and new color operations can be inserted.
Then the file is reconverted to PDF, e.g.:
ps2pdf myimage.eps myimage-changedcolors.pdf
This way can also be used, if later a different driver than pdfTeX/LuaTeX is used that
always ensure the black is the default color. Then the color is changed in the PostScript
file.
\includegraphics
tikz
todraw
over the image, orfill
regions in color. Depending on how simple these images are, it might be better to just redo them in TikZ. So, without seeing what the figures look like it is difficult to recommend an appropriate solution. You mention that there are thousands of these, if they have a specific repeating pattern of where you would like to add the color than usingtikz
would work well.