Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to change all colours in a PDF to their respective complimentary colours; how to make a PDF negative

How can all colours in a PDF be changed to their compliments? So, I mean that a document consisting of black text on a white background would be changed to a document consisting of white text on a black background. Any red colours in a document would be changed to turquoise colours and so on. Is there some standard utility that could be used for this purpose or am I likely to have to contrive some awkward ImageMagick image conversions?

EDIT: Here's a very manual way of doing this using ImageMagick:

convert -density 300 -quality 100 "${fileName}" tmp.png
mogrify -flatten *.png
mogrify -negate *.png
convert *.png "${fileName}"_1.pdf

EDIT: I changed the wording for the purposes of clarity.

like image 957
d3pd Avatar asked Dec 14 '13 14:12

d3pd


People also ask

Can you edit Colours on a PDF?

Convert object colors. If certain objects in the PDF don't match the color space of the document, you can use the Edit Object tool to correct them. The Edit Object tool can change the color space of selected objects.


2 Answers

I can think of at least 3 ways to invert (negate, compliment) colors of a PDF page description -- I mean treating page content as a black box, therefore not counting direct diving into page content and messing around as per Dingo's answer. Unfortunately, ready-made free tools (Ghostscript, mainly) provide incomplete solution and require manual intervention.

Note, that all specific terms used below require at least some knowledge of basics of PDF and Postscript Language References and are presented here somewhat as a simplification, please refer to manuals or google for thorough description.

  1. The most obvious method is to use inverting Transfer function. Transfer function (TF) expects an argument in the range 0..1 which is (additive) color component, and returns color value, too. Negating TF is, of course, {1 sub neg} and is easy to inject:

    gs -q -sDEVICE=pdfwrite -o out.pdf -c '{1 sub neg} settransfer' -f in.pdf
    

    That's great, and Adobe Reader displays our out.pdf as (see below) negated. But here 'greatness' ends. All other viewers ignore TF, (probably) considering it to be device-dependent and actually present in PDF as a compensation for output device pecularities (non-linear printer response etc.) and therefore something to be ignored when displaying PDF on-screen. Further, depending in Reader's version, negation of black-on-white text leads to either white-on-black or yellowish-on-black text. And that's not great.

    Therefore, we need not only TF injection, but the way to properly apply TF to PDF content before viewing. And, regardless of ps2pdf Ghostscript's manual saying:

    Currently, the transfer function is always applied

    (of three options: Apply, Preserve, Remove)

    using current 9.10 version, I couldn't make Ghostscript to actually apply TF (i.e. modify page description operators) when outputting to high-level (pdfwrite, as opposing to image output) devices. Maybe I'm missing something here.

    But, Adobe Distiller, with proper options set, does apply TF to input postscript file.

  2. Somewhat related to TF is the use of inverting Device-Link color profiles, which are simple identity DL profiles with inverting (input or output) curves.

    That's an interesting use of interesting technology, but, again, Ghostscript currently doesn't support proper Color Management (and DL profiles) in PDF-2-PDF workflows. Moreover, Adobe Acrobat doesn't know what to do with DL profiles, their use within Acrobat requires expensive third-party plugins.

  3. If PDF viewer (renderer) claims to support 1.4 and transparency (they all do, nowadays), that's another way to go. PDF Reference says, that if current Blending Mode is Difference and we paint with white, it effectively means inverting backdrop. So, we explicitly paint background with white (if there's no background then there's nothing to invert), then put our current content (treating it as black box), then set Blending Mode to Difference and paint on top with white. Is that clear? Again, I had no success setting Blending Mode using Ghostscript, with:

    [ /BM /Difference /SetTransparency pdfmark 
    

    It works OK with Distiller but is ignored by Ghostscript. Maybe (again) I'm missing something.

OK, to round up (the answer's getting somewhat long), here's Perl solution for 3d method using proper API (programming site, isn't it. Any programming language and appropriate API will do):

use strict;
use warnings;
use PDF::API2;
use PDF::API2::Basic::PDF::Utils;

my $pdf = PDF::API2->open('adobe_supplement_iso32000.pdf');
for my $n (1..$pdf->pages()) {
    my $p = $pdf->openpage($n);

    $p->{Group} = PDFDict();
    $p->{Group}->{CS} = PDFName('DeviceRGB');
    $p->{Group}->{S} = PDFName('Transparency');

    my $gfx = $p->gfx(1);  # prepend
    $gfx->fillcolor('white');
    $gfx->rect($p->get_mediabox());
    $gfx->fill();

    $gfx = $p->gfx();  # append
    $gfx->egstate($pdf->egstate->blendmode('Difference'));
    $gfx->fillcolor('white');
    $gfx->rect($p->get_mediabox());
    $gfx->fill();
}
$pdf->saveas('out.pdf');

Here I take one of Adobe documents and invert it.

enter image description here

What's important: page should have transparency blending space set to RGB explicitly, because Adobe Reader defaults to CMYK, and inverting colors in CMYK you probably don't want. Pure CMYK black 0-0-0-100 inverts to 100-100-100-0, that's (nearly) black, too. RGB black gives something like 70-60-50-70 CMYK that inverts to brown 30-40-50-30, and you don't want that. That's why I add Group entry to pages dictionaries.

like image 185
user2846289 Avatar answered Nov 10 '22 03:11

user2846289


Your question seems to be very similar to this:

Change background color of pdf

but you also want to change the colour of the text.

so you can follow the workflow I suggested some time ago for the same task:

----------------

vector pdf background (meaning not raster image) in pdf files can be easily changed in a couple of steps (see also my stackoverflow answer that now I'll extend and improve

  • Change background color of pdf

PRELIMINAR CHECK:

open your pdf file with an editor able to show the internal pdf structure, like

notepad++ - http://notepad-plus-plus.org/download/v6.1.8.html

and verify if you can see code snippets like

0.000 0.000 0.000 rg (it means *black*)
1.000 1.000 1.000 rg (it means *white*)

and so on...

(code snippet can change, for instance, in pdf produced by openoffice internal pdf exporting feature, the same code snippepts are in this forms:

0 0 0 rg (it means *black*)
1 1 1 rg (it means *white*)

and so on...

if you are able to see these code snippets, then you can start to change values, otherwise, you need to decompress text streams

you can perform this task with

pdftk

http://www.pdflabs.com/docs/install-pdftk/

pdftk file.pdf output uncompressed.pdf uncompress

and recompress after finished changes

pdftk uncompressed.pdf output recompressed.pdf compress

now, if you see these code snippets, you can change values

STEP 1 (for pdf editing) -

the first thing you need is to find the right equivalence between RGB color values of text and background and the internal pdf represerntation of same colors

Since it seems you are a windowsian inhabitant from the third planet in the Microsoft constellation, you can use a free color picker like this

http://www.iconico.com/download.aspx?app=ColorPic&type=free

to identify the rgb values of text and background colors

once you have these values, you need to convert into special internal pdf representation

to do this take i mind this proportion:

1:255=x:color you selected

for instance: let say you have this RGB triplet for background:

30,144,255

azure background

to know correspondent values in pdf in order to insert in code snippet to change pdf background color, you do: (you can use http:// www.wolframalpha.com/ to compute with precision)

1:255=x:30 = 30/255 = 0.117 (approximated to first three decimals)

1:255=x:144 = 144/255 = 0.564 (approximated to first three decimals)

1:255=x:255 = 255/255 = 1

so, the whole triplet in pdf, corresponding to RGB 30,144,255, will be:

0.117 0.564 1.000

STEP 2 (for pdf editing)

we look for 0.117 0.564 1.000 in pdf file with notepad++ (wrap around and match one word only need to be checked) and we found the internal pdf representation of background and we can change from azure to, let say, white

1.000 1.000 1.000

or

1 1 1

but, since you wrote about black background, to be more precise, I created a sample pdf with white background and black text

  • http://ge.tt/1N7Vuz91/v/0

black text on white background

since we know that 0.000 0.000 0.000 rg means black, we look for this and we can change from 0.000 0.000 0.000 rg, to 1.000 1.000 1.000 rg (white) BUT...

at same time, if, your text is black, nd you want change its color to white, you need also to change first the text from black to other color, otherwise it will be invisible, white on white

so, we cannot simply change directly white background to black, at once, since doing this, we have not a difference between color text and background values

and then we act as follows:

we change white background from 1.000 1.000 1.000 into something like

0.5 0.5 0.5 (light grey)
  • http://ge.tt/1N7Vuz91/v/1 (resulting pdf - intermediate step)

black_on_gray

then looking for

0.000 0.000 0.000 (black text) and change to **white**
1.000 1.000 1.000

resulting intermediate pdf file:

  • http://ge.tt/1N7Vuz91/v/2

white on gray

finally, we change again the color of background from

0.5 0.5 0.5 (light grey)

to black

0.000 0.000 0.000

and we have now a vector pdf with white text and black background

  • http://ge.tt/1N7Vuz91/v/3

white on black

please, remember to

1 - compress again this pdf you mmodified if you uncompressed with pdftk 2 - repair

pdftk file.pdf output fixed.pdf

steps

there is another way, starting from postscript, to perform the same task, but being you a windowsian, I guess the postscript way is the harder way for you, but if someone (a linuxian from Torvald constellation) is interested I can explain how do the same thing in postscript

not in this post to avoid to be too verbose

give a feedback, please, and feel free to ask more

like image 23
Dingo Avatar answered Nov 10 '22 03:11

Dingo