Changing OCGs

This is a code example of iText PDF, discover more.

31st May 2016
iText PDF

Switch code for this example

AddOCG.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * Example written by Bruno Lowagie in answer to:
  10.  * http://stackoverflow.com/questions/27956833/adding-comment-on-pdf-layer-created-using-itextsharp-in-adobe-reader
  11.  */
  12. package com.itextpdf.samples.sandbox.stamper;
  13.  
  14. import com.itextpdf.kernel.pdf.PdfDocument;
  15. import com.itextpdf.kernel.pdf.PdfReader;
  16. import com.itextpdf.kernel.pdf.PdfWriter;
  17. import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
  18. import com.itextpdf.kernel.pdf.layer.PdfLayer;
  19. import com.itextpdf.layout.Canvas;
  20. import com.itextpdf.layout.property.TextAlignment;
  21. import com.itextpdf.samples.GenericTest;
  22. import com.itextpdf.test.annotations.type.SampleTest;
  23.  
  24. import org.junit.experimental.categories.Category;
  25.  
  26. import java.io.File;
  27. import java.util.ArrayList;
  28. import java.util.List;
  29.  
  30.  
  31. @Category(SampleTest.class)
  32. public class AddOCG extends GenericTest {
  33.     public static final String DEST = "./target/test/resources/sandbox/stamper/add_ocg.pdf";
  34.     public static final String SRC = "./src/test/resources/pdfs/hello.pdf";
  35.  
  36.     public static void main(String[] args) throws Exception {
  37.         File file = new File(DEST);
  38.         file.getParentFile().mkdirs();
  39.         new AddOCG().manipulatePdf(DEST);
  40.     }
  41.  
  42.     @Override
  43.     protected void manipulatePdf(String dest) throws Exception {
  44.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
  45.         PdfCanvas canvas = new PdfCanvas(pdfDoc.getFirstPage());
  46.  
  47.         PdfLayer nested = new PdfLayer("Nested layers", pdfDoc);
  48.         PdfLayer nested_1 = new PdfLayer("Nested layer 1", pdfDoc);
  49.         PdfLayer nested_2 = new PdfLayer("Nested layer 2", pdfDoc);
  50.         nested_2.setLocked(true);
  51.         nested.addChild(nested_1);
  52.         nested.addChild(nested_2);
  53.  
  54.         Canvas canvasModel;
  55.         canvas.beginLayer(nested);
  56.         canvasModel = new Canvas(canvas, pdfDoc, pdfDoc.getDefaultPageSize());
  57.         canvasModel.showTextAligned("nested layers", 50, 755, TextAlignment.LEFT, 0);
  58.         canvas.endLayer();
  59.  
  60.         canvas.beginLayer(nested_1);
  61.         canvasModel.showTextAligned("nested layers 1", 100, 800, TextAlignment.LEFT, 0);
  62.         canvas.endLayer();
  63.  
  64.         canvas.beginLayer(nested_2);
  65.         canvasModel.showTextAligned("nested layers 2", 100, 750, TextAlignment.LEFT, 0);
  66.         canvas.endLayer();
  67.  
  68.         PdfLayer group = PdfLayer.createTitle("Grouped layers", pdfDoc);
  69.         PdfLayer layer1 = new PdfLayer("Group: layer 1", pdfDoc);
  70.         PdfLayer layer2 = new PdfLayer("Group: layer 2", pdfDoc);
  71.         group.addChild(layer1);
  72.         group.addChild(layer2);
  73.  
  74.         canvas.beginLayer(layer1);
  75.         canvasModel.showTextAligned("layer 1 in the group", 50, 700, TextAlignment.LEFT, 0);
  76.         canvas.endLayer();
  77.  
  78.         canvas.beginLayer(layer2);
  79.         canvasModel.showTextAligned("layer 2 in the group", 50, 675, TextAlignment.LEFT, 0);
  80.         canvas.endLayer();
  81.  
  82.         PdfLayer radiogroup = PdfLayer.createTitle("Radio group", pdfDoc);
  83.         PdfLayer radio1 = new PdfLayer("Radiogroup: layer 1", pdfDoc);
  84.         radio1.setOn(true);
  85.         PdfLayer radio2 = new PdfLayer("Radiogroup: layer 2", pdfDoc);
  86.         radio2.setOn(false);
  87.         PdfLayer radio3 = new PdfLayer("Radiogroup: layer 3", pdfDoc);
  88.         radio3.setOn(false);
  89.         radiogroup.addChild(radio1);
  90.         radiogroup.addChild(radio2);
  91.         radiogroup.addChild(radio3);
  92.         List options = new ArrayList();
  93.         options.add(radio1);
  94.         options.add(radio2);
  95.         options.add(radio3);
  96.         PdfLayer.addOCGRadioGroup(pdfDoc, options);
  97.  
  98.         canvas.beginLayer(radio1);
  99.         canvasModel.showTextAligned("option 1", 50, 600, TextAlignment.LEFT, 0);
  100.         canvas.endLayer();
  101.  
  102.         canvas.beginLayer(radio2);
  103.         canvasModel.showTextAligned("option 2", 50, 575, TextAlignment.LEFT, 0);
  104.         canvas.endLayer();
  105.  
  106.         canvas.beginLayer(radio3);
  107.         canvasModel.showTextAligned("option 3", 50, 550, TextAlignment.LEFT, 0);
  108.         canvas.endLayer();
  109.  
  110.         PdfLayer not_printed = new PdfLayer("not printed", pdfDoc);
  111.         not_printed.setOnPanel(false);
  112.         not_printed.setPrint("Print", false);
  113.  
  114.         canvas.beginLayer(not_printed);
  115.         canvasModel.showTextAligned("PRINT THIS PAGE", 300, 700, TextAlignment.CENTER, (float) Math.toRadians(90));
  116.         canvas.endLayer();
  117.  
  118.         PdfLayer zoom = new PdfLayer("Zoom 0.75-1.25", pdfDoc);
  119.         zoom.setOnPanel(false);
  120.         zoom.setZoom(0.75f, 1.25f);
  121.  
  122.         canvas.beginLayer(zoom);
  123.         canvasModel.showTextAligned("Only visible if the zoomfactor is between 75 and 125%", 30, 530, TextAlignment.LEFT, (float) Math.toRadians(90));
  124.         canvas.endLayer();
  125.  
  126.         pdfDoc.close();
  127.     }
  128. }
ChangeOCG.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * Example written by Bruno Lowagie in answer to:
  10.  * http://stackoverflow.com/questions/23280083/itextsharp-change-order-of-optional-content-groups
  11.  */
  12. package com.itextpdf.samples.sandbox.stamper;
  13.  
  14. import com.itextpdf.kernel.pdf.PdfDocument;
  15. import com.itextpdf.kernel.pdf.PdfName;
  16. import com.itextpdf.kernel.pdf.PdfReader;
  17. import com.itextpdf.kernel.pdf.PdfWriter;
  18. import com.itextpdf.kernel.pdf.layer.PdfLayer;
  19. import com.itextpdf.samples.GenericTest;
  20. import com.itextpdf.test.annotations.type.SampleTest;
  21.  
  22. import org.junit.experimental.categories.Category;
  23.  
  24. import java.io.File;
  25. import java.util.List;
  26.  
  27. @Category(SampleTest.class)
  28. public class ChangeOCG extends GenericTest {
  29.     public static final String DEST = "./target/test/resources/sandbox/stamper/change_ocg.pdf";
  30.     public static final String SRC = "./src/test/resources/pdfs/ocg.pdf";
  31.  
  32.     public static void main(String[] args) throws Exception {
  33.         File file = new File(DEST);
  34.         file.getParentFile().mkdirs();
  35.         new ChangeOCG().manipulatePdf(DEST);
  36.     }
  37.  
  38.     @Override
  39.     protected void manipulatePdf(String dest) throws Exception {
  40.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
  41.         List layers = pdfDoc.getCatalog().getOCProperties(true).getLayers();
  42.         for (PdfLayer layer : layers) {
  43.             if ("Nested layer 1".equals(layer.getPdfObject().get(PdfName.Name).toString())) {
  44.                 layer.setOn(false);
  45.                 break;
  46.             }
  47.         }
  48.         pdfDoc.close();
  49.     }
  50. }
ChangeOCGOrder.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * Example written by Bruno Lowagie in answer to:
  10.  * http://stackoverflow.com/questions/23280083/itextsharp-change-order-of-optional-content-groups
  11.  */
  12. package com.itextpdf.samples.sandbox.stamper;
  13.  
  14. import com.itextpdf.kernel.pdf.*;
  15. import com.itextpdf.samples.GenericTest;
  16. import com.itextpdf.test.annotations.type.SampleTest;
  17.  
  18. import org.junit.experimental.categories.Category;
  19.  
  20. import java.io.File;
  21.  
  22. @Category(SampleTest.class)
  23. public class ChangeOCGOrder extends GenericTest {
  24.     public static final String DEST = "./target/test/resources/sandbox/stamper/change_ocg_order.pdf";
  25.     public static final String SRC = "./src/test/resources/pdfs/ocg.pdf";
  26.  
  27.     public static void main(String[] args) throws Exception {
  28.         File file = new File(DEST);
  29.         file.getParentFile().mkdirs();
  30.         new ChangeOCGOrder().manipulatePdf(DEST);
  31.     }
  32.  
  33.     @Override
  34.     protected void manipulatePdf(String dest) throws Exception {
  35.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
  36.         PdfCatalog catalog = pdfDoc.getCatalog();
  37.         PdfDictionary ocProps = (PdfDictionary) catalog.getPdfObject().get(PdfName.OCProperties);
  38.         PdfDictionary occd = (PdfDictionary) ocProps.get(PdfName.D);
  39.         PdfArray order = occd.getAsArray(PdfName.Order);
  40.         PdfObject nestedLayers = order.get(0);
  41.         PdfObject nestedLayerArray = order.get(1);
  42.         PdfObject groupedLayers = order.get(2);
  43.         PdfObject radioGroup = order.get(3);
  44.         order.set(0, radioGroup);
  45.         order.set(1, nestedLayers);
  46.         order.set(2, nestedLayerArray);
  47.         order.set(3, groupedLayers);
  48.         pdfDoc.close();
  49.     }
  50. }
Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now