5

I want to change the color of every highlight in a PDF file (without having it do for every highlight manually). Preferably with a command line command.

Here is an example PDF with a highlighted line.

  • You should give more details on what you want to achieve. Are you talking about the annotations in the PDF file? The way they are rendered depends on your PDF file reader. – lgeorget Mar 6 '14 at 16:15
  • Yes, I am talking about annotations in the PDF file. The text highlights have a color which is stored in the PDF and respected by my PDF readers during rendering. – jgosmann Mar 6 '14 at 16:35
  • Ok, that's more clear. – lgeorget Mar 6 '14 at 16:36
  • Can you give us an example file? This is probably information that is hardcoded into the PDF and would involve editing the file with ghostscript or similar tools. – terdon Mar 6 '14 at 16:44
  • 1
    The reference might help. – l0b0 Mar 6 '14 at 16:46
4

I wrote a Python script to perform the task. It searches for all objects in the PDF file (marked by obj and endobj) and checks for every object if it is an annotation (/Type/Annot) of the highlight type (/Subtype/Highlight). If that is the case the color definition (/C[...]) will be replaced.

There are some limitations:

  • No real parsing of the PDF is done. The regular expressions used may not be suitable for some PDF files.
  • This might not work for encrypted or compressed PDF files. (I am not sure whether the annotations might be compressed.)
  • The original file will be overwritten. Don't blame me for lost data! (The script is easily edited to create new files.)
  • I assume that certain PDF objects reference other objects by their position in the file. Thus, I prevent the file size from changing. This means the new color definition might not take up more bytes than the old one.
  • The color definition is not validated. You might break your PDF with an invalid expression.
0

Here is my regex attempt. Replacement happens in-place so make a copy of your document before you start.

This variation works when the colour array is present:

$ perl -pi -e 's/(\/Type.*\/Annot.*\/Subtype.*\/Highlight.*\/C\s*\[)[^]]*]/${1}0.5 0.5 0.5]/' document.pdf

This one is for when the colour array is not specified (which defaults to black):

$ perl -pi -e 's/(?=\/Type.*\/Annot.*\/Subtype.*\/Highlight)(?!\C\s*\[)(.*)Highlight/$1Highlight\/C[0.5 0.5 0.5]/' document.pdf

If the first command doesn't work, try the other. If the second one worked, then for subsequent colour changes use the first.

Replace 0.5 0.5 0.5 with your favourite colour, e.g.

  • 0 0 1 for blue (0/255 0/255 255/255),
  • 1 1 0 for yellow (255/255 255/255 0/255), or
  • 1 0.8745098039 0.3607843137 for kind of orange (255/255 253/255 92/255).
  • Tried this script and for me it only works when the numbers are not fractional. When they are things like 0.5 I get an invalid PDF. Any idea what it could be? – Catalin Hritcu May 29 '19 at 18:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.