Vertical text

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

31st May 2016
iText PDF

Switch code for this example

CenterVertically.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.  * This example was written by Bruno Lowagie in answer to the following question:
  10.  * http://stackoverflow.com/questions/27834540/generating-pdf-structure-on-dynamic-inputs
  11.  */
  12. package com.itextpdf.samples.sandbox.objects;
  13.  
  14. import com.itextpdf.kernel.geom.Rectangle;
  15. import com.itextpdf.kernel.pdf.PdfDocument;
  16. import com.itextpdf.kernel.pdf.PdfWriter;
  17. import com.itextpdf.layout.Document;
  18. import com.itextpdf.layout.element.AreaBreak;
  19. import com.itextpdf.layout.element.Cell;
  20. import com.itextpdf.layout.element.Table;
  21. import com.itextpdf.layout.layout.LayoutArea;
  22. import com.itextpdf.layout.layout.LayoutContext;
  23. import com.itextpdf.layout.layout.LayoutResult;
  24. import com.itextpdf.layout.renderer.IRenderer;
  25. import com.itextpdf.layout.renderer.TableRenderer;
  26. import com.itextpdf.samples.GenericTest;
  27. import com.itextpdf.test.annotations.type.SampleTest;
  28.  
  29. import org.junit.experimental.categories.Category;
  30.  
  31. import java.io.File;
  32. import java.io.IOException;
  33.  
  34. @Category(SampleTest.class)
  35. public class CenterVertically extends GenericTest {
  36.     public static final String DEST = "./target/test/resources/sandbox/objects/center_vertically.pdf";
  37.  
  38.     public static void main(String[] args) throws Exception {
  39.         File file = new File(DEST);
  40.         file.getParentFile().mkdirs();
  41.         new CenterVertically().manipulatePdf(DEST);
  42.     }
  43.  
  44.     public Rectangle addTable(Document doc, Table table) {
  45.         Rectangle pageDimension = new Rectangle(36, 36, 523, 770);
  46.         Rectangle rect;
  47.         IRenderer tableRenderer = table.createRendererSubTree().setParent(doc.getRenderer());
  48.         LayoutResult tableLayoutResult = tableRenderer.layout(new LayoutContext(new LayoutArea(0, pageDimension)));
  49.         if (LayoutResult.PARTIAL == tableLayoutResult.getStatus()) {
  50.             rect = pageDimension;
  51.         } else {
  52.             rect = new Rectangle(36, ((tableLayoutResult.getOccupiedArea().getBBox().getBottom() + 36) / 2),
  53.                     523, tableLayoutResult.getOccupiedArea().getBBox().getHeight());
  54.         }
  55.         return rect;
  56.     }
  57.  
  58.     @Override
  59.     protected void manipulatePdf(String dest) throws IOException {
  60.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  61.         Document doc = new Document(pdfDoc);
  62.  
  63.         Table table;
  64.         Cell cell = new Cell();
  65.         for (int i = 1; i <= 5; i++) {
  66.             cell.add("Line " + i);
  67.         }
  68.  
  69.         table = new Table(1);
  70.         table.addCell(cell);
  71.         table.addCell(cell.clone(true));
  72.         table.addCell(cell.clone(true));
  73.         table.setNextRenderer(new CustomTableRenderer(table, addTable(doc, table)));
  74.         doc.add(table);
  75.         doc.add(new AreaBreak());
  76.  
  77.         table = new Table(1);
  78.         table.addCell(cell.clone(true));
  79.         table.addCell(cell.clone(true));
  80.         table.addCell(cell.clone(true));
  81.         table.addCell(cell.clone(true));
  82.         table.addCell(cell.clone(true));
  83.         table.addCell(cell.clone(true));
  84.         table.addCell(cell.clone(true));
  85.         table.addCell(cell.clone(true));
  86.         table.addCell(cell.clone(true));
  87.         table.addCell(cell.clone(true));
  88.         table.setNextRenderer(new CustomTableRenderer(table, addTable(doc, table)));
  89.         doc.add(table);
  90.  
  91.         doc.close();
  92.     }
  93.  
  94.  
  95.     class CustomTableRenderer extends TableRenderer {
  96.         protected Rectangle rect;
  97.  
  98.         public CustomTableRenderer(Table modelElement, Rectangle rect) {
  99.             super(modelElement);
  100.             this.rect = new Rectangle(rect);
  101.         }
  102.  
  103.         @Override
  104.         public LayoutResult layout(LayoutContext layoutContext) {
  105.             return super.layout(new LayoutContext(new LayoutArea(layoutContext.getArea().getPageNumber(), rect)));
  106.         }
  107.     }
  108. }
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