1

Is it possible to merge layers of a PDF (OCG) with the base PDF to result in a PDF without layers?

I saw that it's possible to accomplish this using an application as Adobe Acrobat DC using a "Flatten Layers" option but I need this programmed in my Java application using iText7.

EDIT:

@joelgeraci has a useful and good answer that solves the previous question, but I have initially some hidden layers that will be displayed anyway when removing the OCProperties from the catalog.

mbanchero
  • 134
  • 1
  • 10
  • That's an interesting use case. It can be achieved in a dirty way by removing all OCG-related information from the catalog, but it would be interesting to provide this kind of functionality in a way that all the OCG-related information is also removed from the content streams. – Bruno Lowagie Sep 08 '17 at 05:32

1 Answers1

1

You don't actually need to "merge" the layers. All of the layer content is already part of the page content. Layers, or more properly Optional Content Groups, are sets of instructions that the viewer can either draw or not, depending on the settings, for viewers that don't support layers, they just all show. To "flatten" the layers, you just need to modify the PDF so that the viewer doesn't think there is any optional content. The easiest way is to delete the OCProperties dictionary from the Catalog. Once you have the catalog object, use "remove" passing the name of the OCPropreties dictionary.

catalog.remove(PdfName.OCPROPERTIES)
joelgeraci
  • 4,606
  • 1
  • 12
  • 19
  • Indeed, that's the *dirty* solution I mentioned in my comment (+1 for that). The elegant solution would be to also remove the corresponding OCG syntax from the content streams, Form XObjects, and widget annotations. – Bruno Lowagie Sep 08 '17 at 06:40
  • I'd agree with you *if* there were any consistency in how the optional content is defined at the page level. There is consistency from application to application but each application creates these dictionaries differently. It's certainly possible but it's a lot of work without much payoff. The OC data just doesn't take up enough space to be worth the trouble. Plus, assuming you do remove the OCProperties dictionary, there's no penalty if you miss any of it. If getting it all doesn't matter, it really doesn't matter if you get any. – joelgeraci Sep 08 '17 at 15:08
  • @joelgeraci I tried using this solution by deleting the OCProperties dictionary, it partially worked for me because initially some of the layers are "hidden", and after removing the OCProperties dictionary then those layers are being showed again in the resulted pdf. – mbanchero Sep 12 '17 at 21:56
  • 1
    If that detail were in your original question, I would not have recommended my solution. The OCProperties is *where* the layer visibility defaults are stored. Detecting the content that belongs to a particular layer is non-trivial and can be different based on what the PDF creation tool is used. – joelgeraci Sep 13 '17 at 16:39