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
    Commented Mar 6, 2014 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
    Commented Mar 6, 2014 at 16:35
  • Ok, that's more clear.
    – lgeorget
    Commented Mar 6, 2014 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
    Commented Mar 6, 2014 at 16:44
  • 1
    The reference might help.
    – l0b0
    Commented Mar 6, 2014 at 16:46

3 Answers 3


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.

@jgosmamn's solution is a bit out of date: It appears to use python 2, resulting in errors related to byte-string vs. decoded-string operations. I decided to publish a python 3-friendly version on gist.

This version also has the following changes:

  • Fixes a bug where highlight annotations that occupy more than one line in the file are ignored.
  • Creates new files by default. This lets you check the result before overwriting things.
  • Color string is now hardcoded (no longer a command-line argument). The assumption is you will use this script to normalize the highlight color to some preferred value, so you won't need to change it very often. This has the added benefit of reducing the risk of corrupting the file due to command-line typos.

Note it seems neither of these solutions is guaranteed to get every single highlight. I have some highlights in old documents that are evidently not colored by the \C instruction.


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? Commented May 29, 2019 at 18:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .