Centering text

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

11th September 2016
iText PDF

Switch code for this example

CenterText.java
  1. /*
  2.  * Example written in answer to a question on StackOverflow.
  3.  * http://stackoverflow.com/questions/39437838
  4.  */
  5. package com.itextpdf.sandbox.text;
  6.  
  7. import com.itextpdf.kernel.color.Color;
  8. import com.itextpdf.kernel.geom.PageSize;
  9. import com.itextpdf.kernel.geom.Rectangle;
  10. import com.itextpdf.kernel.pdf.PdfDocument;
  11. import com.itextpdf.kernel.pdf.PdfWriter;
  12. import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
  13. import com.itextpdf.kernel.pdf.canvas.draw.ILineDrawer;
  14. import com.itextpdf.layout.Document;
  15. import com.itextpdf.layout.element.Paragraph;
  16. import com.itextpdf.layout.element.Tab;
  17. import com.itextpdf.layout.element.TabStop;
  18. import com.itextpdf.layout.property.TabAlignment;
  19. import java.io.File;
  20. import java.io.IOException;
  21. import java.util.ArrayList;
  22. import java.util.List;
  23.  
  24. /**
  25.  * @author bruno
  26.  */
  27. public class CenterText {
  28.     class MyLine implements ILineDrawer {
  29.         private float lineWidth = 1;
  30.         private float offset = 5;
  31.         private Color color = Color.BLACK;
  32.         @Override
  33.         public void draw(PdfCanvas canvas, Rectangle drawArea) {
  34.             canvas.saveState()
  35.                 .setStrokeColor(color)
  36.                 .setLineWidth(lineWidth)
  37.                 .moveTo(drawArea.getX(), drawArea.getY() + lineWidth / 2 + offset)
  38.                 .lineTo(drawArea.getX() + drawArea.getWidth(), drawArea.getY() + lineWidth / 2 + offset)
  39.                 .stroke()
  40.                 .restoreState();
  41.         }
  42.  
  43.         @Override
  44.         public float getLineWidth() {
  45.             return lineWidth;
  46.         }
  47.         @Override
  48.         public void setLineWidth(float lineWidth) {
  49.             this.lineWidth = lineWidth;
  50.         }
  51.         @Override
  52.         public Color getColor() {
  53.             return color;
  54.         }
  55.         @Override
  56.         public void setColor(Color color) {
  57.             this.color = color;
  58.         }
  59.         public float getOffset() {
  60.             return offset;
  61.         }
  62.         public void setOffset(float poffset) {
  63.             this.offset = offset;
  64.         }
  65.        
  66.     }
  67.    
  68.     public static final String DEST = "results/text/center_text.pdf";
  69.    
  70.     public static void main(String[] args) throws IOException {
  71.         File file = new File(DEST);
  72.         file.getParentFile().mkdirs();
  73.         new CenterText().createPdf(DEST);
  74.     }
  75.     public void createPdf(String dest) throws IOException {
  76.         PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  77.         PageSize pagesize = PageSize.A4;
  78.         Document document = new Document(pdf, pagesize);
  79.         float w = pagesize.getWidth() - document.getLeftMargin() - document.getRightMargin();
  80.         MyLine line = new MyLine();
  81.         List<TabStop> tabstops = new ArrayList();
  82.         tabstops.add(new TabStop(w / 2, TabAlignment.CENTER, line));
  83.         tabstops.add(new TabStop(w, TabAlignment.LEFT, line));
  84.         Paragraph p = new Paragraph();
  85.         p.addTabStops(tabstops);
  86.         p.add(new Tab()).add("Text in the middle").add(new Tab());
  87.         document.add(p);
  88.         document.close();
  89.     }
  90. }
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