ColumnText and the font ascender

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

31st May 2016
admin-marketing

Switch code for this example

ColumnTextAscender.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/27906725/itext-placement-of-phrase-within-columntext
  11.  */
  12. package com.itextpdf.samples.sandbox.objects;
  13.  
  14. import com.itextpdf.kernel.color.Color;
  15. import com.itextpdf.kernel.geom.Rectangle;
  16. import com.itextpdf.kernel.pdf.PdfDocument;
  17. import com.itextpdf.kernel.pdf.PdfWriter;
  18. import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
  19. import com.itextpdf.layout.ColumnDocumentRenderer;
  20. import com.itextpdf.layout.Document;
  21. import com.itextpdf.layout.element.Paragraph;
  22. import com.itextpdf.samples.GenericTest;
  23. import com.itextpdf.test.annotations.type.SampleTest;
  24.  
  25. import org.junit.experimental.categories.Category;
  26.  
  27. import java.io.File;
  28. import java.io.IOException;
  29.  
  30. @Category(SampleTest.class)
  31. public class ColumnTextAscender extends GenericTest {
  32.     public static final String DEST = "./target/test/resources/sandbox/objects/column_text_ascender.pdf";
  33.  
  34.     public static void main(String[] args) throws Exception {
  35.         File file = new File(DEST);
  36.         file.getParentFile().mkdirs();
  37.         new ColumnTextAscender().manipulatePdf(DEST);
  38.     }
  39.  
  40.     public void manipulatePdf(String dest) throws IOException {
  41.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  42.         Document doc = new Document(pdfDoc);
  43.         Rectangle[] areas = new Rectangle[]{new Rectangle(50, 750, 200, 50), new Rectangle(300, 750, 200, 50)};
  44.         // for canvas usage one should create a page
  45.         pdfDoc.addNewPage();
  46.         for (Rectangle rect : areas) {
  47.             new PdfCanvas(pdfDoc.getFirstPage()).setLineWidth(0.5f).setStrokeColor(Color.RED).rectangle(rect).stroke();
  48.         }
  49.         doc.setRenderer(new ColumnDocumentRenderer(doc, areas));
  50.         addColumn(doc, false);
  51.         addColumn(doc, true);
  52.         doc.close();
  53.     }
  54.  
  55.     public void addColumn(Document doc, boolean useAscender) {
  56.         Paragraph p = new Paragraph("This text is added at the top of the column.");
  57.         // No setUseAscender(boolean). We can change leading instead. This is a bit different, but
  58.         // for now we do not see the need to implement this method in iText7
  59.         if (useAscender) {
  60.             p.setMargin(0);
  61.             p.setMultipliedLeading(1);
  62.         }
  63.  
  64.         doc.add(p);
  65.     }
  66. }
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