Remove OCG layers from PDF with Python

759 Views Asked by At

Is there a way to delete an OCG layer from a PDF within Python?

I normally work with pymupdf but couldn't the functionality there. Is there any other library with this functionality?

1

There are 1 best solutions below

1
On

disclaimer: I am the author of borb the library mentioned in this answer.

borb will turn any input PDF into a JSON-like data structure. If you know what to delete in the content-tree, you can simply remove that item from the dictionary as you would in a normal dictionary object.

Reading the Document is easy:

with open("input.pdf", "rb") as in_file_handle:
    document = PDF.loads(in_file_handle)

You need document["XRef"]["Trailer"]["Root"]["OCGs"] which will be a List of layers. Remove whatever element(s) you want.

If you then store the PDF, the layer will be gone.

with open("output.pdf", "wb") as out_file_handle:
    PDF.dumps(out_file_handle, document)