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);