Overlapping optional content

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

4th August 2016
iText PDF

Switch code for this example

SortingTable.java
  1. /*
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/38761938
  4.  */
  5. package sandbox.columntext;
  6.  
  7. import com.itextpdf.text.BaseColor;
  8. import com.itextpdf.text.Chunk;
  9. import com.itextpdf.text.Document;
  10. import com.itextpdf.text.DocumentException;
  11. import com.itextpdf.text.Paragraph;
  12. import com.itextpdf.text.Phrase;
  13. import com.itextpdf.text.Rectangle;
  14. import com.itextpdf.text.pdf.ColumnText;
  15. import com.itextpdf.text.pdf.PdfAction;
  16. import com.itextpdf.text.pdf.PdfContentByte;
  17. import com.itextpdf.text.pdf.PdfLayer;
  18. import com.itextpdf.text.pdf.PdfPCell;
  19. import com.itextpdf.text.pdf.PdfPTable;
  20. import com.itextpdf.text.pdf.PdfWriter;
  21. import java.io.File;
  22. import java.io.FileOutputStream;
  23. import java.io.IOException;
  24. import java.util.ArrayList;
  25. import java.util.List;
  26. import sandbox.WrapToTest;
  27.  
  28. /**
  29.  * @author iText
  30.  */
  31. @WrapToTest
  32. public class SortingTable {
  33.        
  34.     public static final String DEST = "results/columntext/optionaltables.pdf";
  35.     public static void main(String[] args) throws IOException, DocumentException {
  36.         File file = new File(DEST);
  37.         file.getParentFile().mkdirs();
  38.         new SortingTable().createPdf(DEST);
  39.     }
  40.    
  41.     public void createPdf(String dest) throws IOException, DocumentException {
  42.         // step 1
  43.         Document document = new Document();
  44.         // step 2
  45.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
  46.         // step 3
  47.         document.open();
  48.         // step 4
  49.         ArrayList<PdfLayer> options = new ArrayList<PdfLayer>();
  50.         PdfLayer radiogroup = PdfLayer.createTitle("Table", writer);
  51.         PdfLayer radio1 = new PdfLayer("column1", writer);
  52.         radio1.setOn(true);
  53.         options.add(radio1);
  54.         radiogroup.addChild(radio1);
  55.         PdfLayer radio2 = new PdfLayer("column2", writer);
  56.         radio2.setOn(false);
  57.         options.add(radio2);
  58.         radiogroup.addChild(radio2);
  59.         PdfLayer radio3 = new PdfLayer("column3", writer);
  60.         radio3.setOn(false);
  61.         options.add(radio3);
  62.         radiogroup.addChild(radio3);
  63.         writer.addOCGRadioGroup(options);
  64.        
  65.         PdfContentByte canvas = writer.getDirectContent();
  66.         ColumnText ct = new ColumnText(canvas);
  67.        
  68.         for (int i = 1; i < 4; i++) {
  69.             canvas.beginLayer(options.get(i - 1));
  70.             ct.setSimpleColumn(new Rectangle(36, 36, 559, 806));
  71.             ct.addElement(createTable(i, options));
  72.             ct.go();
  73.             canvas.endLayer();
  74.         }
  75.        
  76.         // step 5
  77.         document.close();
  78.     }
  79.    
  80.     public PdfPTable createTable(int c, List<PdfLayer> options) {
  81.         PdfPTable table = new PdfPTable(3);
  82.         for (int j = 1; j < 4; j++) {
  83.             table.addCell(createCell(j, options));
  84.         }
  85.         for (int i = 1; i < 4; i++) {
  86.             for (int j = 1; j < 4; j++) {
  87.                 table.addCell(createCell(i, j, c));
  88.             }
  89.         }
  90.         return table;
  91.     }
  92.    
  93.     public PdfPCell createCell(int c, List<PdfLayer> options) {
  94.         Chunk chunk = new Chunk("Column " + c);
  95.         ArrayList<Object> list = new ArrayList<Object>();
  96.         list.add("ON");
  97.         list.add(options.get(c - 1));
  98.         PdfAction action = PdfAction.setOCGstate(list, true);
  99.         chunk.setAction(action);
  100.         return new PdfPCell(new Phrase(chunk));
  101.     }
  102.    
  103.     public PdfPCell createCell(int i, int j, int c) {
  104.         PdfPCell cell = new PdfPCell();
  105.         cell.addElement(new Paragraph(String.format("row %s; column %s", i, j)));
  106.         if (j == c) {
  107.             cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
  108.         }
  109.         return cell;
  110.     }
  111. }
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