Alignment, indentation, leading and spacing in cells

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

5th November 2015
admin-marketing

Switch code for this example

SimpleTable4.java
  1. /**
  2.  * Example written by Bruno Lowagie and Nishanthi Grashia in answer to the following question:
  3.  * http://stackoverflow.com/questions/28073190/itext-maintain-identing-if-paragraph-takes-new-line-in-pdfpcell
  4.  */
  5. package sandbox.tables;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.Paragraph;
  10. import com.itextpdf.text.pdf.PdfPCell;
  11. import com.itextpdf.text.pdf.PdfPTable;
  12. import com.itextpdf.text.pdf.PdfWriter;
  13.  
  14. import java.io.File;
  15. import java.io.FileOutputStream;
  16. import java.io.IOException;
  17. import sandbox.WrapToTest;
  18.  
  19. @WrapToTest
  20. public class SimpleTable4 {
  21.     public static final String DEST = "results/tables/simple_table4.pdf";
  22.  
  23.     public static void main(String[] args) throws IOException,
  24.             DocumentException {
  25.         File file = new File(DEST);
  26.         file.getParentFile().mkdirs();
  27.         new SimpleTable4().createPdf(DEST);
  28.     }
  29.     public void createPdf(String dest) throws IOException, DocumentException {
  30.         Document document = new Document();
  31.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  32.         document.open();
  33.         PdfPTable table = new PdfPTable(1);
  34.         Paragraph wrong = new Paragraph("This is wrong, because an object that was originally a paragraph is reduced to a phrase due to the fact that it's put into a cell that uses text mode.");
  35.         wrong.setIndentationLeft(20);
  36.         PdfPCell wrongCell = new PdfPCell(wrong);
  37.         table.addCell(wrongCell);
  38.         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.");
  39.         right.setIndentationLeft(20);
  40.         PdfPCell rightCell = new PdfPCell();
  41.         rightCell.addElement(right);
  42.         table.addCell(rightCell);
  43.         document.add(table);
  44.         document.close();
  45.     }
  46.  
  47. }
LeadingInCell.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/20145742/spacing-leading-pdfpcells-elements
  4.  *
  5.  * Cell in composite mode, containing different paragraphs with a different leading.
  6.  */
  7. package sandbox.tables;
  8.  
  9. import com.itextpdf.text.Document;
  10. import com.itextpdf.text.DocumentException;
  11. import com.itextpdf.text.Paragraph;
  12. import com.itextpdf.text.pdf.PdfPCell;
  13. import com.itextpdf.text.pdf.PdfPTable;
  14. import com.itextpdf.text.pdf.PdfWriter;
  15.  
  16. import java.io.File;
  17. import java.io.FileOutputStream;
  18. import java.io.IOException;
  19.  
  20. import sandbox.WrapToTest;
  21.  
  22. @WrapToTest
  23. public class LeadingInCell {
  24.  
  25.     public static final String DEST = "results/tables/leading_in_cell.pdf";
  26.  
  27.     public static void main(String[] args) throws IOException,
  28.             DocumentException {
  29.         File file = new File(DEST);
  30.         file.getParentFile().mkdirs();
  31.         new LeadingInCell().createPdf(DEST);
  32.     }
  33.    
  34.     public void createPdf(String dest) throws IOException, DocumentException {
  35.         Document document = new Document();
  36.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  37.         document.open();
  38.         PdfPTable table = new PdfPTable(1);
  39.         PdfPCell cell = new PdfPCell();
  40.         Paragraph p;
  41.         p = new Paragraph(16, "paragraph 1: leading 16");
  42.         cell.addElement(p);
  43.         p = new Paragraph(32, "paragraph 2: leading 32");
  44.         cell.addElement(p);
  45.         p = new Paragraph(10, "paragraph 3: leading 10");
  46.         cell.addElement(p);
  47.         p = new Paragraph(18, "paragraph 4: leading 18");
  48.         cell.addElement(p);
  49.         p = new Paragraph(40, "paragraph 5: leading 40");
  50.         cell.addElement(p);
  51.         table.addCell(cell);
  52.         document.add(table);
  53.         document.close();
  54.     }
  55. }
CenteredTextInCell.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/19703715/centered-text-in-itext-pdf-table-cell
  4.  *
  5.  * We create a table with a single column and a single cell.
  6.  * We add some content that needs to be centered vertically.
  7.  */
  8. package sandbox.tables;
  9.  
  10. import com.itextpdf.text.Document;
  11. import com.itextpdf.text.DocumentException;
  12. import com.itextpdf.text.Element;
  13. import com.itextpdf.text.Font;
  14. import com.itextpdf.text.Font.FontFamily;
  15. import com.itextpdf.text.Paragraph;
  16. import com.itextpdf.text.pdf.PdfPCell;
  17. import com.itextpdf.text.pdf.PdfPTable;
  18. import com.itextpdf.text.pdf.PdfWriter;
  19.  
  20. import java.io.File;
  21. import java.io.FileOutputStream;
  22. import java.io.IOException;
  23.  
  24. import sandbox.WrapToTest;
  25.  
  26. @WrapToTest
  27. public class CenteredTextInCell {
  28.  
  29.     public static final String DEST = "results/tables/centered_text.pdf";
  30.    
  31.     public static void main(String[] args) throws IOException, DocumentException {
  32.         File file = new File(DEST);
  33.         file.getParentFile().mkdirs();
  34.         new CenteredTextInCell().createPdf(DEST);
  35.     }
  36.    
  37.     public void createPdf(String dest) throws IOException, DocumentException {
  38.         Document document = new Document();
  39.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  40.         document.open();
  41.         Font font = new Font(FontFamily.HELVETICA, 12, Font.BOLD);
  42.         Paragraph para = new Paragraph("Test", font);
  43.         para.setLeading(0, 1);
  44.         PdfPTable table = new PdfPTable(1);
  45.         table.setWidthPercentage(100);
  46.         PdfPCell cell = new PdfPCell();
  47.         cell.setMinimumHeight(50);
  48.         cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
  49.         cell.addElement(para);
  50.         table.addCell(cell);
  51.         document.add(table);
  52.         document.close();
  53.     }
  54. }
IndentTable.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to a question by a customer.
  3.  */
  4. package sandbox.tables;
  5.  
  6. import com.itextpdf.text.Document;
  7. import com.itextpdf.text.DocumentException;
  8. import com.itextpdf.text.Element;
  9. import com.itextpdf.text.Paragraph;
  10. import com.itextpdf.text.pdf.PdfContentByte;
  11. import com.itextpdf.text.pdf.PdfPTable;
  12. import com.itextpdf.text.pdf.PdfWriter;
  13.  
  14. import java.io.File;
  15. import java.io.FileOutputStream;
  16. import java.io.IOException;
  17. import sandbox.WrapToTest;
  18.  
  19. @WrapToTest
  20. public class IndentTable {
  21.     public static final String DEST = "results/tables/indented_table.pdf";
  22.  
  23.     public static void main(String[] args) throws IOException,
  24.             DocumentException {
  25.         File file = new File(DEST);
  26.         file.getParentFile().mkdirs();
  27.         new IndentTable().createPdf(DEST);
  28.     }
  29.     public void createPdf(String dest) throws IOException, DocumentException {
  30.         Document document = new Document();
  31.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
  32.         document.open();
  33.         PdfContentByte cb = writer.getDirectContent();
  34.         cb.moveTo(36, 842);
  35.         cb.lineTo(36, 0);
  36.         cb.stroke();
  37.         PdfPTable table = new PdfPTable(8);
  38.         table.setHorizontalAlignment(Element.ALIGN_LEFT);
  39.         table.setTotalWidth(150);
  40.         table.setLockedWidth(true);
  41.         for(int aw = 0; aw < 16; aw++){
  42.             table.addCell("hi");
  43.         }
  44.         Paragraph p = new Paragraph();
  45.         p.setIndentationLeft(36);
  46.         p.add(table);
  47.         document.add(p);
  48.         document.close();
  49.     }
  50.  
  51. }
IndentationInCell.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/27873550/how-to-indent-text-inside-a-pdfpcell
  4.  */
  5. package sandbox.tables;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.Element;
  10. import com.itextpdf.text.Paragraph;
  11. import com.itextpdf.text.Phrase;
  12. import com.itextpdf.text.pdf.PdfPCell;
  13. import com.itextpdf.text.pdf.PdfPTable;
  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 IndentationInCell {
  23.     public static final String DEST = "results/tables/indentation_in_cell.pdf";
  24.  
  25.     public static void main(String[] args) throws IOException,
  26.             DocumentException {
  27.         File file = new File(DEST);
  28.         file.getParentFile().mkdirs();
  29.         new IndentationInCell().createPdf(DEST);
  30.     }
  31.     public void createPdf(String dest) throws IOException, DocumentException {
  32.         Document document = new Document();
  33.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  34.         document.open();
  35.         PdfPTable table = new PdfPTable(1);
  36.         PdfPCell cell;
  37.         cell = new PdfPCell(new Phrase("TO:\n\n   name"));
  38.         table.addCell(cell);
  39.         cell = new PdfPCell(new Phrase("TO:\n\n\u00a0\u00a0\u00a0name"));
  40.         table.addCell(cell);
  41.         cell = new PdfPCell();
  42.         cell.addElement(new Paragraph("TO:"));
  43.         Paragraph p = new Paragraph("name");
  44.         p.setIndentationLeft(10);
  45.         cell.addElement(p);
  46.         table.addCell(cell);
  47.         cell = new PdfPCell();
  48.         cell.addElement(new Paragraph("TO:"));
  49.         p = new Paragraph("name");
  50.         p.setAlignment(Element.ALIGN_RIGHT);
  51.         cell.addElement(p);
  52.         table.addCell(cell);
  53.         document.add(table);
  54.         document.close();
  55.     }
  56.  
  57. }
TableWithTab.java
  1. /**
  2.  * This example is written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/32593183/itextsharp-is-it-possible-to-set-a-different-alignment-in-the-same-cell-for-te
  4.  */
  5. package sandbox.tables;
  6.  
  7. import com.itextpdf.text.Chunk;
  8. import com.itextpdf.text.Document;
  9. import com.itextpdf.text.DocumentException;
  10. import com.itextpdf.text.Phrase;
  11. import com.itextpdf.text.pdf.PdfPTable;
  12. import com.itextpdf.text.pdf.PdfWriter;
  13. import com.itextpdf.text.pdf.draw.VerticalPositionMark;
  14. import java.io.File;
  15. import java.io.FileOutputStream;
  16. import java.io.IOException;
  17. import sandbox.WrapToTest;
  18.  
  19. /**
  20.  * @author iText
  21.  */
  22. @WrapToTest
  23. public class TableWithTab {
  24.     public static final String DEST = "results/tables/table_with_tab.pdf";
  25.    
  26.     public static void main(String[] args) throws IOException,
  27.             DocumentException {
  28.         File file = new File(DEST);
  29.         file.getParentFile().mkdirs();
  30.         new TableWithTab().createPdf(DEST);
  31.     }
  32.    
  33.     public void createPdf(String dest) throws IOException, DocumentException {
  34.         Document document = new Document();
  35.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  36.         document.open();
  37.         Chunk glue = new Chunk(new VerticalPositionMark());
  38.         PdfPTable table = new PdfPTable(1);
  39.         Phrase p = new Phrase();
  40.         p.add("Left");
  41.         p.add(glue);
  42.         p.add("Right");
  43.         table.addCell(p);
  44.         document.add(table);
  45.         document.close();
  46.     }
  47.    
  48. }
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