Text indentation

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

31st October 2015
admin-marketing

Switch code for this example

IndentationOptions.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/28487918/how-to-make-it-start-from-the-same-point
  4.  */
  5. package sandbox.objects;
  6.  
  7. import com.itextpdf.text.Chunk;
  8. import com.itextpdf.text.Document;
  9. import com.itextpdf.text.DocumentException;
  10. import com.itextpdf.text.Font;
  11. import com.itextpdf.text.List;
  12. import com.itextpdf.text.Paragraph;
  13. import com.itextpdf.text.Rectangle;
  14. import com.itextpdf.text.pdf.BaseFont;
  15. import com.itextpdf.text.pdf.PdfPTable;
  16. import com.itextpdf.text.pdf.PdfWriter;
  17.  
  18. import java.io.File;
  19. import java.io.FileOutputStream;
  20. import java.io.IOException;
  21. import sandbox.WrapToTest;
  22.  
  23. @WrapToTest
  24. public class IndentationOptions {
  25.  
  26.     public static final String DEST = "results/objects/indentation_options.pdf";
  27.     public static final String LABEL = "A list of stuff: ";
  28.     public static final String CONTENT = "test A, test B, coconut, coconut, watermelons, apple, oranges, many more " +
  29.         "fruites, carshow, monstertrucks thing, everything is startting on the " +
  30.         "same point in the line now";
  31.    
  32.     public static void main(String[] args) throws IOException,
  33.             DocumentException {
  34.         File file = new File(DEST);
  35.         file.getParentFile().mkdirs();
  36.         new IndentationOptions().createPdf(DEST);
  37.     }
  38.    
  39.     public void createPdf(String dest) throws IOException, DocumentException {
  40.         Document document = new Document();
  41.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  42.         document.open();
  43.         List list = new List();
  44.         list.setListSymbol(new Chunk(LABEL));
  45.         list.add(CONTENT);
  46.         document.add(list);
  47.         document.add(Chunk.NEWLINE);
  48.        
  49.         BaseFont bf = BaseFont.createFont();
  50.         Paragraph p = new Paragraph(LABEL + CONTENT, new Font(bf, 12));
  51.         float indentation = bf.getWidthPoint(LABEL, 12);
  52.         p.setIndentationLeft(indentation);
  53.         p.setFirstLineIndent(-indentation);
  54.         document.add(p);
  55.         document.add(Chunk.NEWLINE);
  56.        
  57.         PdfPTable table = new PdfPTable(2);
  58.         table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
  59.         table.setTotalWidth(new float[]{indentation + 4, 519 - indentation});
  60.         table.setLockedWidth(true);
  61.         table.addCell(LABEL);
  62.         table.addCell(CONTENT);
  63.         document.add(table);
  64.        
  65.         document.close();
  66.     }
  67. }
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