Alignment, indentation, leading and spacing in cells

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

31st May 2016
iText PDF

Switch code for this example

CenteredTextInCell.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.  * Example written by Bruno Lowagie in answer to:
  10.  * http://stackoverflow.com/questions/19703715/centered-text-in-itext-pdf-table-cell
  11.  */
  12. package com.itextpdf.samples.sandbox.tables;
  13.  
  14. import com.itextpdf.io.font.FontConstants;
  15. import com.itextpdf.io.font.FontProgramFactory;
  16. import com.itextpdf.kernel.font.PdfFont;
  17. import com.itextpdf.kernel.font.PdfFontFactory;
  18. import com.itextpdf.kernel.pdf.PdfDocument;
  19. import com.itextpdf.kernel.pdf.PdfWriter;
  20. import com.itextpdf.layout.Document;
  21. import com.itextpdf.layout.element.Cell;
  22. import com.itextpdf.layout.element.Paragraph;
  23. import com.itextpdf.layout.element.Table;
  24. import com.itextpdf.layout.property.VerticalAlignment;
  25. import com.itextpdf.samples.GenericTest;
  26. import com.itextpdf.test.annotations.type.SampleTest;
  27.  
  28. import org.junit.experimental.categories.Category;
  29.  
  30. import java.io.File;
  31.  
  32. @Category(SampleTest.class)
  33. public class CenteredTextInCell extends GenericTest {
  34.     public static final String DEST = "./target/test/resources/sandbox/tables/centered_text_in_cell.pdf";
  35.  
  36.     public static void main(String[] args) throws Exception {
  37.         File file = new File(DEST);
  38.         file.getParentFile().mkdirs();
  39.         new CenteredTextInCell().manipulatePdf(DEST);
  40.     }
  41.  
  42.     @Override
  43.     protected void manipulatePdf(String dest) throws Exception {
  44.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  45.         Document doc = new Document(pdfDoc);
  46.         PdfFont font = PdfFontFactory.createFont(FontProgramFactory.createFont(FontConstants.HELVETICA_BOLD));
  47.         Paragraph para = new Paragraph("Test").setFont(font);
  48.         para.setFixedLeading(0);
  49.         para.setMultipliedLeading(1);
  50.         Table table = new Table(1);
  51.         Cell cell = new Cell();
  52.         cell.setHeight(50);
  53.         cell.setVerticalAlignment(VerticalAlignment.MIDDLE);
  54.         cell.add(para);
  55.         table.addCell(cell);
  56.         doc.add(table);
  57.  
  58.         doc.close();
  59.     }
  60. }
IndentTable.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. package com.itextpdf.samples.sandbox.tables;
  9.  
  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.layout.Document;
  14. import com.itextpdf.layout.element.Cell;
  15. import com.itextpdf.layout.element.Paragraph;
  16. import com.itextpdf.layout.element.Table;
  17. import com.itextpdf.layout.property.HorizontalAlignment;
  18. import com.itextpdf.samples.GenericTest;
  19. import com.itextpdf.test.annotations.type.SampleTest;
  20.  
  21. import org.junit.experimental.categories.Category;
  22.  
  23. import java.io.File;
  24.  
  25.  
  26. @Category(SampleTest.class)
  27. public class IndentTable extends GenericTest {
  28.     public static final String DEST = "./target/test/resources/sandbox/tables/indent_table.pdf";
  29.  
  30.     public static void main(String[] args) throws Exception {
  31.         File file = new File(DEST);
  32.         file.getParentFile().mkdirs();
  33.         new IndentTable().manipulatePdf(DEST);
  34.     }
  35.  
  36.     @Override
  37.     protected void manipulatePdf(String dest) throws Exception {
  38.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  39.         Document doc = new Document(pdfDoc);
  40.  
  41.         PdfCanvas cb = new PdfCanvas(pdfDoc.addNewPage());
  42.         cb.moveTo(36, 842);
  43.         cb.lineTo(36, 0);
  44.         cb.stroke();
  45.         Table table = new Table(8);
  46.         table.setHorizontalAlignment(HorizontalAlignment.LEFT);
  47.         table.setWidth(150);
  48.         for (int aw = 0; aw < 16; aw++) {
  49.             table.addCell(new Cell().add(new Paragraph("hi")));
  50.         }
  51.         table.setMarginLeft(25);
  52.         doc.add(table);
  53.  
  54.         doc.close();
  55.     }
  56. }
IndentationInCell.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.  * Example written by Bruno Lowagie in answer to the following question:
  10.  * http://stackoverflow.com/questions/27873550/how-to-indent-text-inside-a-pdfpcell
  11.  */
  12. package com.itextpdf.samples.sandbox.tables;
  13.  
  14. import com.itextpdf.kernel.pdf.PdfDocument;
  15. import com.itextpdf.kernel.pdf.PdfWriter;
  16. import com.itextpdf.layout.Document;
  17. import com.itextpdf.layout.element.Cell;
  18. import com.itextpdf.layout.element.Paragraph;
  19. import com.itextpdf.layout.element.Table;
  20. import com.itextpdf.layout.property.TextAlignment;
  21. import com.itextpdf.samples.GenericTest;
  22. import com.itextpdf.test.annotations.type.SampleTest;
  23.  
  24. import org.junit.experimental.categories.Category;
  25.  
  26. import java.io.File;
  27.  
  28. @Category(SampleTest.class)
  29. public class IndentationInCell extends GenericTest {
  30.     public static final String DEST = "./target/test/resources/sandbox/tables/indentation_in_cell.pdf";
  31.  
  32.     public static void main(String[] args) throws Exception {
  33.         File file = new File(DEST);
  34.         file.getParentFile().mkdirs();
  35.         new IndentationInCell().manipulatePdf(DEST);
  36.     }
  37.  
  38.     @Override
  39.     protected void manipulatePdf(String dest) throws Exception {
  40.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  41.         Document doc = new Document(pdfDoc);
  42.  
  43.         Table table = new Table(1);
  44.         Cell cell;
  45.         cell = new Cell().add("TO:\n\n   name");
  46.         table.addCell(cell);
  47.         cell = new Cell().add("TO:\n\n\u00a0\u00a0\u00a0name");
  48.         table.addCell(cell);
  49.         cell = new Cell();
  50.         cell.add(new Paragraph("TO:"));
  51.         Paragraph p = new Paragraph("name");
  52.         p.setMarginLeft(10);
  53.         cell.add(p);
  54.         table.addCell(cell);
  55.         cell = new Cell();
  56.         cell.add(new Paragraph("TO:"));
  57.         p = new Paragraph("name");
  58.         p.setTextAlignment(TextAlignment.RIGHT);
  59.         cell.add(p);
  60.         table.addCell(cell);
  61.         doc.add(table);
  62.  
  63.         doc.close();
  64.     }
  65. }
LeadingInCell.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.  * Example written by Bruno Lowagie in answer to:
  10.  * http://stackoverflow.com/questions/20145742/spacing-leading-pdfpcells-elements
  11.  */
  12. package com.itextpdf.samples.sandbox.tables;
  13.  
  14. import com.itextpdf.kernel.pdf.PdfDocument;
  15. import com.itextpdf.kernel.pdf.PdfWriter;
  16. import com.itextpdf.layout.Document;
  17. import com.itextpdf.layout.element.Cell;
  18. import com.itextpdf.layout.element.Paragraph;
  19. import com.itextpdf.layout.element.Table;
  20. import com.itextpdf.samples.GenericTest;
  21. import com.itextpdf.test.annotations.type.SampleTest;
  22.  
  23. import org.junit.experimental.categories.Category;
  24.  
  25. import java.io.File;
  26.  
  27. @Category(SampleTest.class)
  28. public class LeadingInCell extends GenericTest {
  29.     public static final String DEST = "./target/test/resources/sandbox/tables/leading_in_cell.pdf";
  30.  
  31.     public static void main(String[] args) throws Exception {
  32.         File file = new File(DEST);
  33.         file.getParentFile().mkdirs();
  34.         new LeadingInCell().manipulatePdf(DEST);
  35.     }
  36.  
  37.     @Override
  38.     protected void manipulatePdf(String dest) throws Exception {
  39.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  40.         Document doc = new Document(pdfDoc);
  41.  
  42.         Table table = new Table(1);
  43.         table.setWidthPercent(60);
  44.         Cell cell = new Cell();
  45.         Paragraph p;
  46.         p = new Paragraph("paragraph 1: leading 16. Text to force a wrap and check the leading. Ha-ha").setFixedLeading(16);
  47.         cell.add(p);
  48.         p = new Paragraph("paragraph 2: leading 32. Text to force a wrap and check the leading. Ha-ha").setFixedLeading(32);
  49.         cell.add(p);
  50.         p = new Paragraph("paragraph 3: leading 10. Text to force a wrap and check the leading. Ha-ha").setFixedLeading(10);
  51.         cell.add(p);
  52.         p = new Paragraph("paragraph 4: leading 18. Text to force a wrap and check the leading. Ha-ha").setFixedLeading(18);
  53.         cell.add(p);
  54.         p = new Paragraph("paragraph 5: leading 40. Text to force a wrap and check the leading. Ha-ha").setFixedLeading(40);
  55.         cell.add(p);
  56.         table.addCell(cell);
  57.         doc.add(table);
  58.  
  59.         doc.close();
  60.     }
  61. }
SimpleTable4.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.  * Example written by Bruno Lowagie and Nishanthi Grashia in answer to the following question:
  10.  * http://stackoverflow.com/questions/28073190/itext-maintain-identing-if-paragraph-takes-new-line-in-pdfpcell
  11.  */
  12. package com.itextpdf.samples.sandbox.tables;
  13.  
  14. import com.itextpdf.kernel.pdf.PdfDocument;
  15. import com.itextpdf.kernel.pdf.PdfWriter;
  16. import com.itextpdf.layout.Document;
  17. import com.itextpdf.layout.element.Cell;
  18. import com.itextpdf.layout.element.Paragraph;
  19. import com.itextpdf.layout.element.Table;
  20. import com.itextpdf.samples.GenericTest;
  21. import com.itextpdf.test.annotations.type.SampleTest;
  22.  
  23. import org.junit.experimental.categories.Category;
  24.  
  25. import java.io.File;
  26.  
  27. @Category(SampleTest.class)
  28. public class SimpleTable4 extends GenericTest {
  29.     public static final String DEST = "./target/test/resources/sandbox/tables/simple_table4.pdf";
  30.  
  31.     public static void main(String[] args) throws Exception {
  32.         File file = new File(DEST);
  33.         file.getParentFile().mkdirs();
  34.         new SimpleTable4().manipulatePdf(DEST);
  35.     }
  36.  
  37.     @Override
  38.     protected void manipulatePdf(String dest) throws Exception {
  39.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  40.         Document doc = new Document(pdfDoc);
  41.  
  42.         // Here in itext7 there is only one way of adding paragraph to table. See itext5 example.
  43.         Table table = new Table(1);
  44.         Paragraph right = new Paragraph("This is right, because we create a paragraph with an indentation to the left and as we are adding the paragraph in composite mode, all the properties of the paragraph are preserved.");
  45.         right.setMarginLeft(20);
  46.         Cell rightCell = new Cell().add(right);
  47.         table.addCell(rightCell);
  48.         doc.add(table);
  49.  
  50.         doc.close();
  51.     }
  52. }
TableWithTab.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 is written by Bruno Lowagie in answer to the following question:
  10.  * http://stackoverflow.com/questions/32593183/itextsharp-is-it-possible-to-set-a-different-alignment-in-the-same-cell-for-te
  11.  */
  12. package com.itextpdf.samples.sandbox.tables;
  13.  
  14. import com.itextpdf.kernel.pdf.PdfDocument;
  15. import com.itextpdf.kernel.pdf.PdfWriter;
  16. import com.itextpdf.layout.Document;
  17. import com.itextpdf.layout.element.Paragraph;
  18. import com.itextpdf.layout.element.Tab;
  19. import com.itextpdf.layout.element.TabStop;
  20. import com.itextpdf.layout.element.Table;
  21. import com.itextpdf.layout.property.TabAlignment;
  22. import com.itextpdf.samples.GenericTest;
  23. import com.itextpdf.test.annotations.type.SampleTest;
  24.  
  25. import java.io.File;
  26.  
  27. import org.junit.experimental.categories.Category;
  28.  
  29.  
  30. @Category(SampleTest.class)
  31. public class TableWithTab extends GenericTest {
  32.     public static final String DEST = "./target/test/resources/sandbox/tables/table_with_tab.pdf";
  33.  
  34.     public static void main(String[] args) throws Exception {
  35.         File file = new File(DEST);
  36.         file.getParentFile().mkdirs();
  37.         new TableWithTab().manipulatePdf(DEST);
  38.     }
  39.  
  40.     @Override
  41.     protected void manipulatePdf(String dest) throws Exception {
  42.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  43.         Document doc = new Document(pdfDoc);
  44.  
  45.         Table table = new Table(1);
  46.         Paragraph p = new Paragraph();
  47.         p.add("Left");
  48.         p.addTabStops(new TabStop(1000, TabAlignment.RIGHT));
  49.         p.add(new Tab());
  50.         p.add("Right");
  51.         table.addCell(p);
  52.         doc.add(table);
  53.         doc.close();
  54.     }
  55. }
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