1async function main() {
2 let doc = await PDFNet.PDFDoc.create();
3 const builder = await PDFNet.ElementBuilder.create();
4 const writer = await PDFNet.ElementWriter.create();
5
6 // Create a layer
7 const imageLayer = await PDFNet.OCG.create(doc, 'Image Layer');
8 const cfg = await PDFNet.OCGConfig.create(doc, true);
9 cfg.setName('Default');
10
11 // Add the new OCG to the list of layers that should appear in PDF viewer GUI.
12 let layerOrderArray = await doc.createIndirectArray();
13 cfg.setOrder(layerOrderArray);
14 layerOrderArray.pushBack(await image_layer.getSDFObj());
15
16 // Start a new page
17 const page = await doc.pageCreate();
18 writer.beginOnPage(page); // Begin writing to the page
19
20 // Add new content to the page and associate it with one of the layers.
21 const nullEncoderHints = new PDFNet.Obj('0');
22 const img = await PDFNet.Image.createFromURL(doc, imagename, nullEncoderHints);
23 const img_obj = await img.getSDFObj();
24 img_obj.put('OC', imageLayer);
25 writer.writePlacedElement(await builder.createImageScaled(img, 300, 600, 200, -150));
26 await writer.end(); // save changes to the current page
27 doc.pagePushBack(page);
28}
29PDFNet.runWithCleanup(main);