Alternatives to using a PdfPTable

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

28th December 2015
admin-marketing

Switch code for this example

SimpleTable13.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/34480476
  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.Rectangle;
  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 SimpleTable13 {
  21.     public static final String DEST = "results/tables/simple_table13.pdf";
  22.     public static final String[][] DATA = {
  23.         {"John Edward Jr.", "AAA"},
  24.         {"Pascal Einstein W. Alfi", "BBB"},
  25.         {"St. John", "CCC"}
  26.     };
  27.  
  28.     public static void main(String[] args) throws IOException,
  29.             DocumentException {
  30.         File file = new File(DEST);
  31.         file.getParentFile().mkdirs();
  32.         new SimpleTable13().createPdf(DEST);
  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(2);
  39.         table.setWidthPercentage(50);
  40.         table.setHorizontalAlignment(Element.ALIGN_LEFT);
  41.         table.setWidths(new int[]{5, 1});
  42.         table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
  43.         table.addCell("Name: " + DATA[0][0]);
  44.         table.addCell(DATA[0][1]);
  45.         table.addCell("Surname: " + DATA[1][0]);
  46.         table.addCell(DATA[1][1]);
  47.         table.addCell("School: " + DATA[2][0]);
  48.         table.addCell(DATA[1][1]);
  49.         document.add(table);
  50.         document.close();
  51.     }
  52. }
TableTab.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/34480476
  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.Paragraph;
  11. import com.itextpdf.text.TabSettings;
  12. import com.itextpdf.text.pdf.PdfWriter;
  13.  
  14. import java.io.File;
  15. import java.io.FileNotFoundException;
  16. import java.io.FileOutputStream;
  17. import java.io.IOException;
  18.  
  19. import sandbox.WrapToTest;
  20.  
  21. @WrapToTest
  22. public class TableTab {
  23.  
  24.     public static final String DEST = "results/objects/tab_table.pdf";
  25.     public static final String[][] DATA = {
  26.         {"John Edward Jr.", "AAA"},
  27.         {"Pascal Einstein W. Alfi", "BBB"},
  28.         {"St. John", "CCC"}
  29.     };
  30.  
  31.     public static void main(String[] args) throws IOException, DocumentException {
  32.         File file = new File(DEST);
  33.         file.getParentFile().mkdirs();
  34.         new TableTab().createPdf(DEST);
  35.     }
  36.  
  37.     public void createPdf(String dest) throws FileNotFoundException, DocumentException {
  38.         Document document = new Document();
  39.  
  40.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  41.  
  42.         document.open();
  43.  
  44.         document.add(createParagraphWithTab("Name: ", DATA[0][0], DATA[0][1]));
  45.         document.add(createParagraphWithTab("Surname: ", DATA[1][0], DATA[1][1]));
  46.         document.add(createParagraphWithTab("School: ", DATA[2][0], DATA[2][1]));
  47.  
  48.         document.close();
  49.     }
  50.  
  51.     public Paragraph createParagraphWithTab(String key, String value1, String value2) {
  52.         Paragraph p = new Paragraph();
  53.         p.setTabSettings(new TabSettings(200f));
  54.         p.add(key);
  55.         p.add(value1);
  56.         p.add(Chunk.TABBING);
  57.         p.add(value2);
  58.         return p;
  59.     }
  60. }
TableSpace.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/34480476
  4.  */
  5. package sandbox.objects;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.Font;
  10. import com.itextpdf.text.Paragraph;
  11. import com.itextpdf.text.pdf.BaseFont;
  12. import com.itextpdf.text.pdf.PdfWriter;
  13.  
  14. import java.io.File;
  15. import java.io.FileOutputStream;
  16. import java.io.IOException;
  17.  
  18. import sandbox.WrapToTest;
  19.  
  20. @WrapToTest
  21. public class TableSpace {
  22.  
  23.     public static final String DEST = "results/objects/spaces_table.pdf";
  24.     public static final String FONT = "resources/fonts/PTM55FT.ttf";
  25.     public static final String[][] DATA = {
  26.         {"John Edward Jr.", "AAA"},
  27.         {"Pascal Einstein W. Alfi", "BBB"},
  28.         {"St. John", "CCC"}
  29.     };
  30.  
  31.     public static void main(String[] args) throws IOException, DocumentException {
  32.         File file = new File(DEST);
  33.         file.getParentFile().mkdirs();
  34.         new TableSpace().createPdf(DEST);
  35.     }
  36.  
  37.     public void createPdf(String dest) throws DocumentException, IOException {
  38.         Document document = new Document();
  39.  
  40.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  41.  
  42.         document.open();
  43.  
  44.         BaseFont bf = BaseFont.createFont(FONT, BaseFont.CP1250, BaseFont.EMBEDDED);
  45.         Font font = new Font(bf, 12);
  46.        
  47.         document.add(createParagraphWithSpaces(font, String.format("%s: %s", "Name", DATA[0][0]), DATA[0][1]));
  48.         document.add(createParagraphWithSpaces(font, String.format("%s: %s", "Surname", DATA[1][0]), DATA[1][1]));
  49.         document.add(createParagraphWithSpaces(font, String.format("%s: %s", "School", DATA[2][0]), DATA[2][1]));
  50.  
  51.         document.close();
  52.     }
  53.  
  54.     public Paragraph createParagraphWithSpaces(Font font, String value1, String value2) {
  55.         Paragraph p = new Paragraph();
  56.         p.setFont(font);
  57.         p.add(String.format("%-35s", value1));
  58.         p.add(value2);
  59.         return p;
  60.     }
  61. }
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