1let doc: PTPDFDoc = PTPDFDoc(filepath: filename)
2let page: PTPage = doc.getPage(1)
3
4let init_cfg: PTConfig = doc.getOCGConfig()
5let ctx: PTContext = PTContext(config: init_cfg)
6
7let pdfdraw: PTPDFDraw = PTPDFDraw(dpi: 92)
8pdfdraw.setImageSize(1000, height: 1000, preserve_aspect_ratio: true)
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.
15let ocgs: PTObj = doc.getOCGs() { // Get the array of all OCGs in the document.
16var _: UInt = 0
17let sz: UInt = ocgs.size()
18for i in 0..<sz {
19 let ocg: PTGroup = PTGroup(ocg: ocgs.getAt(i))
20 ctx.resetStates(false)
21 ctx.setState(ocg, state: true)
22 let fname: String = output_path + "pdf_layers_\(ocg.getName()).png")
23 pdfdraw.export(page, filename: fname, format: "PNG")
24}
25
26// Now draw content that is not part of any layer...
27ctx.setNonOCDrawing(true)
28ctx.setOCDrawMode(e_ptNoOC)
29pdfdraw.export(page, filename: output_path + "pdf_layers_non_oc.png", format: "PNG")