Detecting saved and restored graphics state via ElementReader

Hello,

I’m using PDFTron to import PDF into our propietary format. Some of the PDF we need to be able to import contain several clipping-paths that are specific to a particular subset of the drawing. These PDF files use save and restore GState operators to set and reset such clipping paths so they only operate upon certain elements.

While I’m able to read the clipping paths properly, I cannot determine what should they clip because I cannot detect when the GState was saved and restored using ElementReader.

How do I do that?

TIA

You can detect when the graphics state is saved (i.e. ‘q’ operatror) with Element.Type.e_group_begin.
Similarly can detect when the graphics state is restored i.e. ‘Q’ operatror) with Element.Type.e_group_end.

For more info see: https://www.pdftron.com/pdfnet/intro.html#what_element

You can detect when the graphics state is saved (i.e. ‘q’ operatror) with Element.Type.e_group_begin.
Similarly can detect when the graphics state is restored i.e. ‘Q’ operatror) with Element.Type.e_group_end.

Ahh OK, indeed it is in the docs but I missed it.
PDF 1.4 contains the proper specification of a “group” (a group form XObject). One use case for such groups that is pertty common are transparency groups.
Can I safely assume that you produce group begin/end only from q and Q operators, and not for "actual PDF groups?
Indeed to make sure the importer doesn’t mixed one with the other.

Best

A “group XObject” is a specialized Form XObject. Form XObjects are indicated by Element.Type.e_form. Form XObjects contain another new content stream, and themselves can contain q operators, or other Form XObjects.

There are also Image XObjects, but they contain image data, and not q operators, and are indicated by Element.Type.e_image.

If you have not already, take a close look at the ElementReader sample.
https://www.pdftron.com/pdfnet/samplecode.html#ElementReaderAdv

So it sounds like you are only interested in e_group_[being|end] elements.