How to programmatically remove PDF layers?

4.2k Views Asked by At

I know about Doc.SetOCGOrder() and the likes, but apparently the Acrobat API does not provide means to remove layers (aka optional content groups, ocgs), only to hide them from users while leaving them accessible via scripting.

I'm not forced to use Acrobat, in fact I'd prefer a FOSS-solution (or at least freeware) using e.g. or . edit There is another post using , which is unfortunately not free. Is there any way to port that behaviour to Acrobat's JS or ? Essentially the answer seems to loop through every page's content, which seems to include the layers.

(Concerning merging, I notice pdftk does remove layers when merging multiple files, so one can simple pdftk A=input.pdf B=input.pdf cat A1-end output output.pdf.)

2

There are 2 best solutions below

0
On

My approach so far: For the relevant layers obtained via GetOCGs() set locked=true and both initState=false and state=false. Then use SetOCGOrder() to remove the layer from the layers view. Unfortunately this is not perfect, since at least in Acrobat DC I can still select "show all layers" in the layers panel, which still shows the layer. One could merge all layers now, but neither did I find an option to achieve that programmatically nor do I believe that would actually remove the hidden layer's elements.

0
On

You can't do it programmatically but you can create an Action that will process a folder full of PDF files using Adobe Acrobat. Set the Action to use the PDF Optimizer to discard hidden layers and flatten visible ones. See the graphic.

enter image description here