Ghostscript can convert color documents into grayscale. Example commandline:
gs \
-o grayscale.pdf \
-sDEVICE=pdfwrite \
-sColorConversionStrategy=Gray \
-sProcessColorModel=/DeviceGray \
/path/to/your.pdf
The most recent version of Ghostscript can also check for the CMYK ink coverage of PDF documents (not image by image, but page by page), using a new "device" called inkcov
. Check for the ink coverage of an example PDF:
gs -o - -sDEVICE=inkcov /path/to/your.pdf
Example output:
Page 1
0.00000 0.00000 0.00000 0.02230 CMYK OK
Page 2
0.02360 0.02360 0.02360 0.02360 CMYK OK
Page 3
0.02525 0.02525 0.02525 0.00000 CMYK OK
Page 4
0.00000 0.00000 0.00000 0.01982 CMYK OK
Here 1.00000 would mean 100%, 0.02525 means 2.525% and 0.00000 means 0% ink coverage. You can see here that pages 1+4 are using no color, while pages 2+3 do. This case is particularly 'nasty' for people who want to save on color ink: because all the C, M, Y (and K) values are exactly the same for each of the pages 2+3, they possibly could appear to the human eye not as color pages, but as grayscale (or "rich" grayscale in the case of page 3) anyway. (That is, if each single pixel is mixed with these color values -- of course, if the page is made by four different squares of the same size using "pure" colors each, then you'd get the same ink coverage result).
Now convert the original PDF to a grayscale one, using the command I initially gave. Then check for the ink coverage distribution again (note how the addition of -q
to the parameters slightly changes the output format):
gs -q -o - -sDEVICE=inkcov grayscale.pdf
0.00000 0.00000 0.00000 0.02230 CMYK OK
0.00000 0.00000 0.00000 0.02360 CMYK OK
0.00000 0.00000 0.00000 0.02525 CMYK OK
0.00000 0.00000 0.00000 0.01982 CMYK OK
\documentclass[landscape]{article}
\usepackage[svgnames]{xcolor}
\usepackage{tikz}
\usetikzlibrary{backgrounds}
\begin{document}
\begin{tikzpicture}[scale=.5,
show background rectangle,
background rectangle/.style={fill=black},
color=white,help lines/.style={color=lightgray,line width=0.2pt}]
\draw (0,14) node[anchor=north west] {{\bf text}};
\draw (0,13.5) node[anchor=north west] {text};
\draw (0,13) node[anchor=north west] {text};
\draw (0,12.5) node[anchor=north west] {text};
\draw (0,12) node[anchor=north west] {text};
\draw (0,1) node[anchor=north west] {{\bf Deliberately left blank}};
% bounding box
\draw[style=help lines] (11,0.5) rectangle (12.5,14);
\draw[style=help lines] (11.75,0.5) rectangle (13.25,14);
\draw[style=help lines] (14,0.5) rectangle (15.5,14);
\draw[style=help lines] (14.75,0.5) rectangle (16.25,14);
\draw[style=help lines] (17,0.5) rectangle (18.5,14);
\draw[style=help lines] (17.75,0.5) rectangle (19.25,14);
\draw[style=help lines] (20,0.5) rectangle (21.5,14);
\draw[style=help lines] (20.75,0.5) rectangle (22.25,14);
\draw[style=help lines] (23,0.5) rectangle (24.5,14);
\draw[style=help lines] (23.75,0.5) rectangle (25.25,14);
\draw[style=help lines] (0,14) -- (26,14);
\draw[style=help lines] (0,11) -- (26,11);
\draw[style=help lines] (0,7.5) -- (26,7.5);
\draw[style=help lines] (0,3.5) -- (26,3.5);
\draw[style=help lines] (0,1) -- (26,1);
\draw (11,0.5) rectangle (26,14);
\draw (14,0.5) rectangle (23,14);
\draw (17,0.5) rectangle (20,14);
\draw (12.5,14) node[anchor=south] {Year 1};
\draw (15.5,14) node[anchor=south] {Year 2};
\draw (18.5,14) node[anchor=south] {Year 3};
\draw (21.5,14) node[anchor=south] {Year 4};
\draw (24.5,14) node[anchor=south] {Year 5};
% blocks
\filldraw[fill=Silver] (11,13) rectangle (13.25,13.5);
\filldraw[fill=Silver] (11.75,12.5) rectangle (16.25,13);
\filldraw[fill=Silver] (14,12) rectangle (17.75,12.5);
\filldraw[fill=Silver] (15.5,11.5) rectangle (20,12);
%\filldraw[fill=Silver] (17,11) rectangle (17.75,11.5);
\filldraw[fill=Silver] (13.25,10) rectangle (16.25,10.5);
\filldraw[fill=Silver] (14.75,9.5) rectangle (18.5,10);
\filldraw[fill=Silver] (17,9) rectangle (20.75,9.5);
\filldraw[fill=Silver] (18.5,8.5) rectangle (22.25,9);
%\filldraw[fill=Silver] (20.75,8) rectangle (20,8.5);
\filldraw[fill=Silver] (14,6.5) rectangle (20,7);
\filldraw[fill=Silver] (19.25,6) rectangle (24.5,6.5);
\filldraw[fill=Silver] (15.5,5.5) rectangle (17.75,6);
\filldraw[fill=Silver] (17.75,5) rectangle (20, 5.5);
\filldraw[fill=Silver] (20,4.5) rectangle (22.25,5);
\filldraw[fill=Silver] (22.25,4) rectangle (24.5,4.5);
\filldraw[fill=Silver] (11,2.5) rectangle (13.25,3);
\filldraw[fill=Silver] (13.25,2) rectangle (15.5,2.5);
\filldraw[fill=Silver] (15.5,1.5) rectangle (24.5, 2);
\filldraw[fill=Silver] (17.75,1) rectangle (24.5,1.5);
\filldraw[fill=Silver] (24.5,0.5) rectangle (26,1);
\end{tikzpicture}
\end{document}
Best Answer
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: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:Then you need to find the page object. Look for
/Page
(not/Pages
), e.g.:Then the number after
/Contents
is the object number for the page contents stream. In this case the number is5
and we search for5 0 obj
:Now we have 254 bytes between
stream
andendstream
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 modelCS
,cs
,SC
,sc
,SCN
,scn
with parameters each.In the case of the monochrome black image, I would expect
0 g
and0 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 thexref
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:
Or overwrite the entry by spaces (
, ordinary spaces with character code 32).
The the file is recompressed:
And the trick with pdfTeX will work:
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
fromxpdf
:It uses the same operator names as in the PDF file. In case of
pdftops
the page starts after%%EndSetup
(withpdfStartPage
). PostScript is a programming language, in general it can be difficult to locate the color operators:setgray
,setrgbcolor
,setcmykcolor
. In case ofpdftops
, 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.:
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.