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");
1PDFDoc doc(filename);
2Page page = doc.GetPage(1);
3
4OCG::Config init_cfg = doc.GetOCGConfig();
5OCG::Context ctx(init_cfg);
6
7PDFDraw 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 = int(ocgs.Size());
17for (i=0; i<sz; ++i) {
18 OCG::Group ocg(ocgs.GetAt(i));
19 ctx.ResetStates(false);
20 ctx.SetState(ocg, true);
21 std::string fname("pdf_layers_");
22 fname += ocg.GetName().ConvertToAscii();
23 fname += ".png";
24 pdfdraw.Export(page, (output_path + fname).c_str());
25}
26
27// Now draw content that is not part of any layer...
28ctx.SetNonOCDrawing(true);
29ctx.SetOCDrawMode(OCG::Context::e_NoOC);
30pdfdraw.Export(page, (output_path + "pdf_layers_non_oc.png").c_str());
1doc := NewPDFDoc(filename)
2page = doc.GetPage(1)
3
4init_cfg := doc.GetOCGConfig()
5ctx := NewContext(init_cfg)
6
7pdfdraw := NewPDFDraw()
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.
15ocgs := doc.GetOCGs() // Get the array of all OCGs in the document.
16if ocgs != nil{
17 sz := ocgs.Size()
18 i := int64(0)
19 for i < sz{
20 ocg := NewGroup(ocgs.GetAt(i))
21 ctx.ResetStates(false)
22 ctx.SetState(ocg, true)
23 fname := "pdf_layers_" + ocg.GetName() + ".png"
24 fmt.Println(fname)
25 pdfdraw.Export(page, outputPath + fname)
26 i = i + 1
27 }
28}
29
30// Now draw content that is not part of any layer...
31ctx.SetNonOCDrawing(true)
32ctx.SetOCDrawMode(ContextE_NoOC)
33pdfdraw.Export(page, outputPath + "pdf_layers_non_oc.png")
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 = (int) ocgs.size();
17for (i = 0; i < sz; ++i) {
18 Group ocg = new Group(ocgs.getAt(i));
19 ctx.resetStates(false);
20 ctx.setState(ocg, true);
21 String fname = "pdf_layers_" + ocg.getName() + ".png";
22 pdfdraw.export(page, output_path + fname);
23}
24
25// Now draw content that is not part of any layer...
26ctx.setNonOCDrawing(true);
27ctx.setOCDrawMode(Context.e_NoOC);
28pdfdraw.export(page, output_path + "pdf_layers_non_oc.png");
1async function main() {
2 const doc = await PDFNet.PDFDoc.createFromURL(filename);
3 const page = await doc.getPage(1);
4
5 const initCfg = await doc.getOCGConfig();
6 const ctx = await PDFNet.OCGContext.createFromConfig(initCfg);
7
8 const pdfdraw = await PDFNet.PDFDraw.create();
9 pdfdraw.setImageSize(1000, 1000);
10 pdfdraw.setOCGContext(ctx);
11
12 // Disable drawing of content that is not optional (i.e. is not part of any layer).
13 ctx.setNonOCDrawing(false);
14
15 // Now render each layer in the input document to a separate image.
16 const ocgs = await doc.getOCGs(); // Get the array of all OCGs in the document.
17 let i;
18 const sz = await ocgs.size();
19 for (i = 0; i < sz; ++i) {
20 const ocg = await PDFNet.OCG.createFromObj(await ocgs.getAt(i));
21 ctx.resetStates(false);
22 ctx.setState(ocg, true);
23 let fname = 'pdf_layers_';
24 fname += await ocg.getName();
25 fname += '.png';
26 const pageBuffer = await pdfdraw.exportStream(page);
27 const imageData = new Blob([pageBuffer], {type: 'image/png'}));
28 }
29
30 // Now draw content that is not part of any layer...
31 ctx.setNonOCDrawing(true);
32 ctx.setOCDrawMode(PDFNet.OCGContext.OCDrawMode.e_NoOC);
33 const nonLayerBuffer = await pdfdraw.exportStream(page);
34 const data = new Blob([nonLayerBuffer], {type: 'image/png'}));
35}
36PDFNet.runWithCleanup(main);
1PTPDFDoc *doc = [[PTPDFDoc alloc] initWithFilepath: filename];
2PTPage *page = [doc GetPage: 1];
3
4PTConfig *init_cfg = [doc GetOCGConfig];
5PTContext *ctx = [[PTContext alloc] initWithConfig: init_cfg];
6
7PTPDFDraw *pdfdraw = [[PTPDFDraw alloc] initWithDpi: 92];
8[pdfdraw SetImageSize: 1000 height: 1000 preserve_aspect_ratio: YES];
9[pdfdraw 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).
12[ctx SetNonOCDrawing: NO];
13
14// Now render each layer in the input document to a separate image.
15PTObj * ocgs = [doc GetOCGs]; // Get the array of all OCGs in the document.
16unsigned long i, sz = [ocgs Size];
17for (i=0; i<sz; ++i) {
18 PTGroup *ocg = [[PTGroup alloc] initWithOcg: [ocgs GetAt: i]];
19 [ctx ResetStates: NO];
20 [ctx SetState: ocg state: YES];
21 NSString* fname = [output_path stringByAppendingFormat: @"pdf_layers_%@.png", [ocg GetName]];
22 [pdfdraw page filename: fname format: @"PNG"];
23}
24
25// Now draw content that is not part of any layer...
26[ctx SetNonOCDrawing: YES];
27[ctx SetOCDrawMode: e_ptNoOC];
28[pdfdraw page filename: [output_path stringByAppendingString: @"pdf_layers_non_oc.png"] format: @"PNG" ];
1$doc = new PDFDoc($filename);
2$page = $doc->GetPage(1);
3
4$init_cfg = $doc->GetOCGConfig();
5$ctx = new Context($init_cfg);
6
7$pdfdraw = new PDFDraw();
8$pdfdraw->SetImageSize(1000, 1000);
9$pdfdraw->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).
12$ctx->SetNonOCDrawing(false);
13
14// Now render each layer in the input document to a separate image.
15$ocgs = $doc->GetOCGs(); // Get the array of all OCGs in the document.
16$sz = $ocgs->Size();
17for ($i=0; $i<$sz; ++$i) {
18 $ocg = new Group($ocgs->GetAt($i));
19 $ctx->ResetStates(false);
20 $ctx->SetState($ocg, true);
21 $fname = "pdf_layers_".$ocg->GetName().".png";
22 $pdfdraw->Export($page, $output_path.$fname);
23}
24
25// Now draw content that is not part of any layer...
26$ctx->SetNonOCDrawing(true);
27$ctx->SetOCDrawMode(Context::e_NoOC);
28$pdfdraw->Export($page, $output_path."pdf_layers_non_oc.png");
1doc = PDFDoc(filename)
2page = doc.GetPage(1)
3
4init_cfg = doc.GetOCGConfig()
5ctx = Context(init_cfg)
6
7pdfdraw = 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.
15ocgs = doc.GetOCGs() # Get the array of all OCGs in the document.
16sz = ocgs.Size()
17i = 0
18while i < sz:
19 ocg = Group(ocgs.GetAt(i))
20 ctx.ResetStates(False)
21 ctx.SetState(ocg, True)
22 fname = "pdf_layers_" + ocg.GetName() + ".png"
23 pdfdraw.Export(page, output_path + fname)
24 i = i + 1
25
26# Now draw content that is not part of any layer...
27ctx.SetNonOCDrawing(True)
28ctx.SetOCDrawMode(Context.e_NoOC)
29pdfdraw.Export(page, output_path + "pdf_layers_non_oc.png")
1doc = PDFDoc.new($filename)
2page = doc.GetPage(1)
3
4init_cfg = doc.GetOCGConfig
5ctx = Context.new(init_cfg)
6
7pdfdraw = PDFDraw.new
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.
15ocgs = doc.GetOCGs # Get the array of all OCGs in the document.
16sz = ocgs.Size
17i = 0
18while i < sz do
19 ocg = Group.new(ocgs.GetAt(i))
20 ctx.ResetStates(false)
21 ctx.SetState(ocg, true)
22 fname = "pdf_layers_" + ocg.GetName + ".png"
23 pdfdraw.Export(page, $output_path + fname)
24 i = i + 1
25end
26
27# Now draw content that is not part of any layer...
28ctx.SetNonOCDrawing(true)
29ctx.SetOCDrawMode(Context::E_NoOC)
30pdfdraw.Export(page, $output_path + "pdf_layers_non_oc.png")
1Dim doc As PDFDoc = New PDFDoc(filename)
2Dim page As Page = doc.GetPage(1)
3
4Dim init_cfg As Config = doc.GetOCGConfig()
5Dim ctx As Context = New Context(init_cfg)
6
7Dim pdfdraw As 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.
15Dim ocgs As Obj = doc.GetOCGs() ' Get the array of all OCGs in the document.
16Dim i As Integer = 0
17Dim sz As Integer = ocgs.Size()
18While (i < sz)
19 Dim ocg As Group = New Group(ocgs.GetAt(i))
20 ctx.ResetStates(False)
21 ctx.SetState(ocg, True)
22 Dim fname As String = "pdf_layers_" + ocg.GetName() + ".png"
23 pdfdraw.Export(page, output_path + fname)
24 i = i + 1
25End While
26
27' Now draw content that is not part of any layer...
28ctx.SetNonOCDrawing(True)
29ctx.SetOCDrawMode(Context.OCDrawMode.e_NoOC)
30pdfdraw.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 DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales