Working with PDF layers (OCGs)

Q: Does PDFNet support for PDF layers?

Also when reading through the geometry in a PDF document... using an
ElementReader. Is there a way to find out what layer this geom is
on.... or alternatively while looping through the layers... is there a
way to get access to the geom on it ?
A: PDFNet SDK supports PDF layers (OCG). Using ElementReader you can
determine to what layer the element is associated with. This is
illustrated in the following C# pseudocode:

- When you encounter Form XObject, you can obtain for dictionary using
- If the dictionary contains OC entry, the form content is part of
some optional content group/layer.
- For simple groups you can obtain the layer name using

// C# pseudocode (JAVA, VB, or C++ is essentially the same)

string cur_layer_name;

void ProcessElements(ElementReader reader) {
Element element;
while ((element = reader.Next()) != null) {
   switch (element.GetType()) {
     case Element.Type.e_path:
     case Element.Type.e_text:
     case Element.Type.e_form: {
       Obj xobject = element.GetXObject();
       Obj oc = xobject.FindObj("OC");
       if (oc != null) {
         // Check if this is OC Membership dictionary or OCG
         pdftron.PDF.OCG.OCMD md = new pdftron.PDF.OCG.OCMD(oc);
         if (md.IsValid()) {
           // cur_layer_name = traverse the array of OCGs using
         else {
           pdftron.PDF.OCG.Group grp = new pdftron.PDF.OCG.Group(oc);
           if (grp.IsValid()) {
             cur_layer_name = grp.GetName();


For examples of how to tell PDFNet rasterizer to turn 'on' and 'off'
specific layers during content rendering please take a look at the
following projects:

For an example of how to programmatically create PDF documents with
layers, please see PDFLayers (

You may also want to refer to 'pdftron.PDF.OCG' namespace in PDFNet
SDK API Reference (
namespacepdftron_1_1PDF_1_1OCG.html or
for Java developers) for more details on specific methods and classes
related to PDF layers and Optional Content Groups (OCGs).

The code sample here does not do what you claim it does.
first, the string shall not be "OC" but "Group". took me a while to realize that.
second, how exactly does it allow us to associate an element with a layer???
On the file I tried, both IsValid() calls returned false always, and it is really not clear what do those calls (on md and grp) should do.

Can you please clarify or post a code sample that decides to which layer every element in the document belongs?
Are the layers belong to a page or to a document?


For elements of type e_form, you want to check for the ‘OC’ object. This is the layer for which it is attached to.

Also note that OCG layers can be specified in the content stream in other areas.

The following code will find other ocg layers.


if element.GetType() == Element.e_marked_content_begin:
mc_prop_dict = element.GetMCPropertyDict()
if mc_prop_dict:

this is the OCG object (aka layer)


Note, all the following elements, until you hit Element.e_marked_content_end type, are under the same layer.

You might have a malformed PDF document, so if the above doesn’t help, you can try sending the PDF to support.

hi Rayn,
Thanks for your reply, it indeed represents the layer.
But yet:

  1. how can I get the OCG from the dictionary mc_prop_dict?
  2. how can I check if a specific Annotation (not element) is included in a given layer?


Hi Rayn,
Another question about pdf layers (OCG): does a layer belong to a page or to a document?


Layers are part of the document itself, and can span multiple pages.

Hi Ryan,
Can you please elaborate (and preferably send a code snippet) on creating and getting annotations fron a specific layer?
This is very important for our application, and we examine the solution of PdfTron.


Hi Ryan,
Thanks for your replies in the past.
I am now left with the following major problems:

  1. how to create an annotation on a given layer?
  2. how to extract the name of the layer which the annotation belons to?


  1. how to create an annotation on a given layer?

You want to call Annot.SetOptionalContent(Obj). See the link for details on acceptable Obj object. Typically, you would pass in one of the objs from the array you get from doc.GetOCGS().

  1. how to extract the name of the layer which the annotation belongs to?

Group group(annot.GetOptionalContent()`);

string layer_name =group.GetName();