Extract layers from PDF on Server/Desktop

To extract PDF layers or OCG (optional content group) from a document.

1PDFDoc doc = new PDFDoc(filename);
2Page page = doc.GetPage(1);
3
4Config init_cfg = doc.GetOCGConfig();
5Context ctx = new Context(init_cfg);
6
7PDFDraw pdfdraw = new PDFDraw();
8pdfdraw.SetImageSize(1000, 1000);
9pdfdraw.SetOCGContext(ctx); // Render the page using the given OCG context.
10
11// Disable drawing of content that is not optional (i.e. is not part of any layer).
12ctx.SetNonOCDrawing(false);
13
14// Now render each layer in the input document to a separate image.
15Obj ocgs = doc.GetOCGs(); // Get the array of all OCGs in the document.
16int i, sz = ocgs.Size();
17for (i=0; i<sz; ++i)
18{
19 Group ocg = new Group(ocgs.GetAt(i));
20 ctx.ResetStates(false);
21 ctx.SetState(ocg, true);
22 string fname = "pdf_layers_" + ocg.GetName() + ".png";
23 pdfdraw.Export(page, output_path + fname);
24}
25
26// Now draw content that is not part of any layer...
27ctx.SetNonOCDrawing(true);
28ctx.SetOCDrawMode(Context.OCDrawMode.e_NoOC);
29pdfdraw.Export(page, output_path + "pdf_layers_non_oc.png");

PDF layers (OCG)
Full sample code which demonstrates how to create, extract, render PDF layers.

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales