Changing OCGs

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

30th October 2015
admin-marketing

Switch code for this example

AddOCG.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/27956833/adding-comment-on-pdf-layer-created-using-itextsharp-in-adobe-reader
  4.  */
  5. package sandbox.stamper;
  6.  
  7. import com.itextpdf.text.DocumentException;
  8. import com.itextpdf.text.Element;
  9. import com.itextpdf.text.Phrase;
  10. import com.itextpdf.text.pdf.ColumnText;
  11. import com.itextpdf.text.pdf.PdfContentByte;
  12. import com.itextpdf.text.pdf.PdfLayer;
  13. import com.itextpdf.text.pdf.PdfReader;
  14. import com.itextpdf.text.pdf.PdfStamper;
  15. import com.itextpdf.text.pdf.PdfWriter;
  16.  
  17. import java.io.File;
  18. import java.io.FileOutputStream;
  19. import java.io.IOException;
  20. import java.util.ArrayList;
  21. import java.util.Map;
  22. import sandbox.WrapToTest;
  23.  
  24. @WrapToTest
  25. public class AddOCG {
  26.  
  27.     public static final String SRC = "resources/pdfs/hello.pdf";
  28.     public static final String DEST = "results/stamper/hello_ocg.pdf";
  29.  
  30.     public static void main(String[] args) throws IOException, DocumentException {
  31.         File file = new File(DEST);
  32.         file.getParentFile().mkdirs();
  33.         new AddOCG().manipulatePdf(SRC, DEST);
  34.     }
  35.  
  36.  
  37.     public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
  38.         PdfReader reader = new PdfReader(src);
  39.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  40.         Map layers = stamper.getPdfLayers();
  41.         PdfWriter writer = stamper.getWriter();
  42.         PdfContentByte cb = stamper.getOverContent(1);
  43.        
  44.         PdfLayer nested = new PdfLayer("Nested layers", writer);
  45.         PdfLayer nested_1 = new PdfLayer("Nested layer 1", writer);
  46.         PdfLayer nested_2 = new PdfLayer("Nested layer 2", writer);
  47.         nested.addChild(nested_1);
  48.         nested.addChild(nested_2);
  49.         writer.lockLayer(nested_2);
  50.         cb.beginLayer(nested);
  51.         ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase(
  52.                 "nested layers"), 50, 775, 0);
  53.         cb.endLayer();
  54.         cb.beginLayer(nested_1);
  55.         ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase(
  56.                 "nested layer 1"), 100, 800, 0);
  57.         cb.endLayer();
  58.         cb.beginLayer(nested_2);
  59.         ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase(
  60.                 "nested layer 2"), 100, 750, 0);
  61.         cb.endLayer();
  62.  
  63.         PdfLayer group = PdfLayer.createTitle("Grouped layers", writer);
  64.         PdfLayer layer1 = new PdfLayer("Group: layer 1", writer);
  65.         PdfLayer layer2 = new PdfLayer("Group: layer 2", writer);
  66.         group.addChild(layer1);
  67.         group.addChild(layer2);
  68.         cb.beginLayer(layer1);
  69.         ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase(
  70.                 "layer 1 in the group"), 50, 700, 0);
  71.         cb.endLayer();
  72.         cb.beginLayer(layer2);
  73.         ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase(
  74.                 "layer 2 in the group"), 50, 675, 0);
  75.         cb.endLayer();
  76.  
  77.         PdfLayer radiogroup = PdfLayer.createTitle("Radio group", writer);
  78.         PdfLayer radio1 = new PdfLayer("Radiogroup: layer 1", writer);
  79.         radio1.setOn(true);
  80.         PdfLayer radio2 = new PdfLayer("Radiogroup: layer 2", writer);
  81.         radio2.setOn(false);
  82.         PdfLayer radio3 = new PdfLayer("Radiogroup: layer 3", writer);
  83.         radio3.setOn(false);
  84.         radiogroup.addChild(radio1);
  85.         radiogroup.addChild(radio2);
  86.         radiogroup.addChild(radio3);
  87.         ArrayList options = new ArrayList();
  88.         options.add(radio1);
  89.         options.add(radio2);
  90.         options.add(radio3);
  91.         writer.addOCGRadioGroup(options);
  92.         cb.beginLayer(radio1);
  93.         ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase(
  94.                 "option 1"), 50, 600, 0);
  95.         cb.endLayer();
  96.         cb.beginLayer(radio2);
  97.         ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase(
  98.                 "option 2"), 50, 575, 0);
  99.         cb.endLayer();
  100.         cb.beginLayer(radio3);
  101.         ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase(
  102.                 "option 3"), 50, 550, 0);
  103.         cb.endLayer();
  104.  
  105.         PdfLayer not_printed = new PdfLayer("not printed", writer);
  106.         not_printed.setOnPanel(false);
  107.         not_printed.setPrint("Print", false);
  108.         cb.beginLayer(not_printed);
  109.         ColumnText.showTextAligned(cb, Element.ALIGN_CENTER, new Phrase(
  110.                 "PRINT THIS PAGE"), 300, 700, 90);
  111.         cb.endLayer();
  112.  
  113.         PdfLayer zoom = new PdfLayer("Zoom 0.75-1.25", writer);
  114.         zoom.setOnPanel(false);
  115.         zoom.setZoom(0.75f, 1.25f);
  116.         cb.beginLayer(zoom);
  117.         ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase(
  118.                 "Only visible if the zoomfactor is between 75 and 125%"), 30,
  119.                 530, 90);
  120.         cb.endLayer();
  121.        
  122.         stamper.close();
  123.         reader.close();
  124.     }
  125.  
  126. }
ChangeOCG.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/23280083/itextsharp-change-order-of-optional-content-groups
  4.  */
  5. package sandbox.stamper;
  6.  
  7. import com.itextpdf.text.DocumentException;
  8. import com.itextpdf.text.pdf.PdfLayer;
  9. import com.itextpdf.text.pdf.PdfReader;
  10. import com.itextpdf.text.pdf.PdfStamper;
  11.  
  12. import java.io.File;
  13. import java.io.FileOutputStream;
  14. import java.io.IOException;
  15. import java.util.Map;
  16. import sandbox.WrapToTest;
  17.  
  18. @WrapToTest
  19. public class ChangeOCG {
  20.  
  21.     public static final String SRC = "resources/pdfs/ocg.pdf";
  22.     public static final String DEST = "results/stamper/ocg_changed.pdf";
  23.  
  24.     public static void main(String[] args) throws IOException, DocumentException {
  25.         File file = new File(DEST);
  26.         file.getParentFile().mkdirs();
  27.         new ChangeOCG().manipulatePdf(SRC, DEST);
  28.     }
  29.  
  30.  
  31.     public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
  32.         PdfReader reader = new PdfReader(src);
  33.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  34.         Map layers = stamper.getPdfLayers();
  35.         PdfLayer layer = layers.get("Nested layer 1");
  36.         layer.setOn(false);
  37.         stamper.close();
  38.         reader.close();
  39.     }
  40.  
  41. }
ChangeOCGOrder.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/23280083/itextsharp-change-order-of-optional-content-groups
  4.  */
  5. package sandbox.stamper;
  6.  
  7. import com.itextpdf.text.DocumentException;
  8. import com.itextpdf.text.pdf.PdfArray;
  9. import com.itextpdf.text.pdf.PdfDictionary;
  10. import com.itextpdf.text.pdf.PdfName;
  11. import com.itextpdf.text.pdf.PdfObject;
  12. import com.itextpdf.text.pdf.PdfReader;
  13. import com.itextpdf.text.pdf.PdfStamper;
  14.  
  15. import java.io.File;
  16. import java.io.FileOutputStream;
  17. import java.io.IOException;
  18. import sandbox.WrapToTest;
  19.  
  20. @WrapToTest
  21. public class ChangeOCGOrder {
  22.  
  23.     public static final String SRC = "resources/pdfs/ocg.pdf";
  24.     public static final String DEST = "results/stamper/ocg_reordered.pdf";
  25.  
  26.     public static void main(String[] args) throws IOException, DocumentException {
  27.         File file = new File(DEST);
  28.         file.getParentFile().mkdirs();
  29.         new ChangeOCGOrder().manipulatePdf(SRC, DEST);
  30.     }
  31.  
  32.  
  33.     public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
  34.         PdfReader reader = new PdfReader(src);
  35.         PdfDictionary catalog = reader.getCatalog();
  36.         PdfDictionary ocProps = catalog.getAsDict(PdfName.OCPROPERTIES);
  37.         PdfDictionary occd = ocProps.getAsDict(PdfName.D);
  38.         PdfArray order = occd.getAsArray(PdfName.ORDER);
  39.         PdfObject nestedLayers = order.getPdfObject(0);
  40.         PdfObject nestedLayerArray = order.getPdfObject(1);
  41.         PdfObject groupedLayers = order.getPdfObject(2);
  42.         PdfObject radiogroup = order.getPdfObject(3);
  43.         order.set(0, radiogroup);
  44.         order.set(1, nestedLayers);
  45.         order.set(2, nestedLayerArray);
  46.         order.set(3, groupedLayers);
  47.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  48.         stamper.close();
  49.         reader.close();
  50.     }
  51.  
  52. }
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