1let doc: PTPDFDoc = PTPDFDoc()
2let builder: PTElementBuilder = PTElementBuilder() // ElementBuilder is used to build new Element objects
3let writer: PTElementWriter = PTElementWriter() // ElementWriter is used to write Elements to the page
4
5// Create a layer
6let image_layer: PTGroup = PTGroup.create(doc, name: "Image Layer")
7let cfg: PTConfig = PTConfig.create(doc, default_config: true)
8cfg.setName("Default")
9
10// Add the new OCG to the list of layers that should appear in PDF viewer GUI.
11let layer_order_array: PTObj = doc.createIndirectArray()
12cfg.setOrder(layer_order_array)
13layer_order_array.pushBack(image_layer.getSDFObj())
14
15// Start a new page
16let page: PTPage = doc.pageCreate(PTPDFRect(x1: 0, y1: 0, x2: 612, y2: 792))
17writer.writerBegin(with: page, placement: e_ptoverlay, page_coord_sys: true, compress: true) // Begin writing to the page
18
19// Add new content to the page and associate it with one of the layers.
20let img: PTImage = PTImage.create(doc.getSDFDoc(), filename: imagename)
21img.getSDFObj().put("OC", obj: image_layer)
22writer.writePlacedElement(builder.createImage(withCornerAndScale: img, x: 300, y: 600, hscale: 200, vscale: -150)) writer.end() // save changes to the current page
23doc.pagePushBack(page)