ColumnText and the font ascender

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

23rd October 2015
admin-marketing

Switch code for this example

ColumnTextAscender.java
  1. /*
  2.  * This example was written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/27906725/itext-placement-of-phrase-within-columntext
  4.  */
  5. package sandbox.objects;
  6.  
  7. import com.itextpdf.text.BaseColor;
  8. import com.itextpdf.text.Document;
  9. import com.itextpdf.text.DocumentException;
  10. import com.itextpdf.text.Phrase;
  11. import com.itextpdf.text.Rectangle;
  12. import com.itextpdf.text.pdf.ColumnText;
  13. import com.itextpdf.text.pdf.PdfContentByte;
  14. import com.itextpdf.text.pdf.PdfWriter;
  15.  
  16. import java.io.File;
  17. import java.io.FileOutputStream;
  18. import java.io.IOException;
  19. import sandbox.WrapToTest;
  20.  
  21. @WrapToTest
  22. public class ColumnTextAscender {
  23.  
  24.     public static final String DEST = "results/objects/column_text_ascender.pdf";
  25.    
  26.     public static void main(String[] args) throws IOException, DocumentException {
  27.         File file = new File(DEST);
  28.         file.getParentFile().mkdirs();
  29.         new ColumnTextAscender().createPdf(DEST);
  30.     }
  31.  
  32.     public void createPdf(String dest) throws IOException, DocumentException {
  33.         // step 1
  34.         Document document = new Document();
  35.         // step 2
  36.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
  37.         // step 3
  38.         document.open();
  39.         // step 4
  40.         // show the area for the column as a rectangle with red borders
  41.         Rectangle rect = new Rectangle(50, 750, 250, 800);
  42.         addColumn(writer, rect, false);
  43.         rect = new Rectangle(300, 750, 500, 800);
  44.         addColumn(writer, rect, true);
  45.         // step 5
  46.         document.close();
  47.     }
  48.    
  49.     public void addColumn(PdfWriter writer, Rectangle rect, boolean useAscender) throws DocumentException {
  50.         rect.setBorder(Rectangle.BOX);
  51.         rect.setBorderWidth(0.5f);
  52.         rect.setBorderColor(BaseColor.RED);
  53.         PdfContentByte cb = writer.getDirectContent();
  54.         cb.rectangle(rect);
  55.         Phrase p = new Phrase("This text is added at the top of the column.");
  56.         ColumnText ct = new ColumnText(cb);
  57.         ct.setSimpleColumn(rect);
  58.         ct.setUseAscender(useAscender);
  59.         ct.addText(p);
  60.         ct.go();
  61.     }
  62. }
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