//--------------------------------------------------------------------------------------- // Copyright (c) 2001-2008 by PDFTron Systems Inc. All Rights Reserved. // Consult legal.txt regarding legal and license information. //--------------------------------------------------------------------------------------- import pdftron.Common.Matrix2D; import pdftron.Common.PDFNetException; import pdftron.PDF.*; import pdftron.PDF.OCG.*; import pdftron.SDF.*; //----------------------------------------------------------------------------------- // This sample demonstrates how to create layers in PDF. // The sample also shows how to extract and render PDF layers in documents // that contain optional content groups (OCGs) // // With the introduction of PDF version 1.5 came the concept of Layers. // Layers, or as they are more formally known Optional Content Groups (OCGs), // refer to sections of content in a PDF document that can be selectively // viewed or hidden by document authors or consumers. This capability is useful // in CAD drawings, layered artwork, maps, multi-language documents etc. // // Couple of notes regarding this sample: // --------------------------------------- // - This sample is using CreateLayer() utility method to create new OCGs. // CreateLayer() is relatively basic, however it can be extended to set // other optional entries in the 'OCG' and 'OCProperties' dictionary. For // a complete listing of possible entries in OC dictionary please refer to // section 4.10 'Optional Content' in the PDF Reference Manual. // - The sample is grouping all layer content into separate Form XObjects. // Although using PDFNet is is also possible to specify Optional Content in // Content Streams (Section 4.10.2 in PDF Reference), Optional Content in // XObjects results in PDFs that are cleaner, less-error prone, and faster // to process. //----------------------------------------------------------------------------------- public class PDFLayersTest { // Relative path to the folder containing test files. static String input_path = "../../TestFiles/"; static String output_path = "../../TestFiles/Output/"; public static void main(String[] args) { PDFNet.initialize(); PDFNet.setResourcesPath("../../../resources"); try { PDFDoc doc=new PDFDoc(); // Create three layers... Group image_layer = createLayer(doc, "Image Layer"); Group text_layer = createLayer(doc, "Text Layer"); Group vector_layer = createLayer(doc, "Vector Layer"); // Start a new page ------------------------------------ Page page = doc.pageCreate(); ElementBuilder builder = new ElementBuilder(); // ElementBuilder is used to build new Element objects ElementWriter writer = new ElementWriter(); // ElementWriter is used to write Elements to the page writer.begin(page); // Begin writing to the page // Add new content to the page and associate it with one of the layers. Element element = builder.createForm(createGroup1(doc, image_layer.getSDFObj())); writer.writeElement(element); element = builder.createForm(createGroup2(doc, vector_layer.getSDFObj())); writer.writeElement(element); // Add the text layer to the page... if (false) // set to true to enable 'ocmd' example. { // A bit more advanced example of how to create an OCMD text layer that // is visible only if text, image and path layers are all 'ON'. // An example of how to set 'Visibility Policy' in OCMD. Obj ocgs = doc.createIndirectArray(); ocgs.pushBack(image_layer.getSDFObj()); ocgs.pushBack(vector_layer.getSDFObj()); ocgs.pushBack(text_layer.getSDFObj()); OCMD text_ocmd = OCMD.create(doc, ocgs, OCMD.e_AllOn); element = builder.createForm(createGroup3(doc, text_ocmd.getSDFObj())); } else { element = builder.createForm(createGroup3(doc, text_layer.getSDFObj())); } writer.writeElement(element); // Add some content to the page that does not belong to any layer... // In this case this is a rectangle representing the page border. element = builder.createRect(0, 0, page.getPageWidth(), page.getPageHeight()); element.setPathFill(false); element.setPathStroke(true); element.getGState().setLineWidth(40); writer.writeElement(element); writer.end(); // save changes to the current page doc.pagePushBack(page); // Set the default viewing preference to display 'Layer' tab. PDFDocViewPrefs prefs = doc.getViewPrefs(); prefs.setPageMode(PDFDocViewPrefs.e_UseOC); doc.save((output_path + "pdf_layers.pdf"), SDFDoc.e_linearized, null); doc.close(); System.out.println("Done."); } catch(Exception e) { e.printStackTrace(); } // The following is a code snippet shows how to selectively render // and export PDF layers. try { PDFDoc doc = new PDFDoc(output_path + "pdf_layers.pdf"); doc.initSecurityHandler(); if (doc.hasOC() == false) { System.out.println("The document does not contain 'Optional Content'"); } else { Config init_cfg = doc.getOCGConfig(); Context ctx = new Context(init_cfg); PDFDraw pdfdraw = new PDFDraw(); pdfdraw.setImageSize(1000, 1000); pdfdraw.setOCGContext(ctx); // Render the page using the given OCG context. Page page = doc.getPage(1); // Get the first page in the document. pdfdraw.export(page, output_path + "pdf_layers_default.png"); // Disable drawing of content that is not optional (i.e. is not part of any layer). ctx.setNonOCDrawing(false); // Now render each layer in the input document to a separate image. Obj ocgs = doc.getOCGs(); // Get the array of all OCGs in the document. if (ocgs != null) { int i, sz = (int)ocgs.size(); for (i=0; i