Alternatives to using a PdfPTable

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

31st May 2016
admin-marketing

Switch code for this example

SimpleTable13.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/34480476
  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.border.Border;
  18. import com.itextpdf.layout.element.Cell;
  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 java.io.File;
  25.  
  26. import org.junit.experimental.categories.Category;
  27.  
  28. @Category(SampleTest.class)
  29. public class SimpleTable13 extends GenericTest {
  30.     public static final String DEST = "./target/test/resources/sandbox/tables/simple_table13.pdf";
  31.     public static final String[][] DATA = {
  32.             {"John Edward Jr.", "AAA"},
  33.             {"Pascal Einstein W. Alfi", "BBB"},
  34.             {"St. John", "CCC"}
  35.     };
  36.  
  37.     public static void main(String[] args) throws Exception {
  38.         File file = new File(DEST);
  39.         file.getParentFile().mkdirs();
  40.         new SimpleTable13().manipulatePdf(DEST);
  41.     }
  42.  
  43.     @Override
  44.     protected void manipulatePdf(String dest) throws Exception {
  45.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  46.         Document doc = new Document(pdfDoc);
  47.  
  48.         Table table = new Table(new float[]{5, 1});
  49.         table.setWidthPercent(50);
  50.         table.setTextAlignment(TextAlignment.LEFT);
  51.         table.addCell(new Cell().add("Name: " + DATA[0][0]).setBorder(Border.NO_BORDER));
  52.         table.addCell(new Cell().add(DATA[0][1]).setBorder(Border.NO_BORDER));
  53.         table.addCell(new Cell().add("Surname: " + DATA[1][0]).setBorder(Border.NO_BORDER));
  54.         table.addCell(new Cell().add(DATA[1][1]).setBorder(Border.NO_BORDER));
  55.         table.addCell(new Cell().add("School: " + DATA[2][0]).setBorder(Border.NO_BORDER));
  56.         table.addCell(new Cell().add(DATA[1][1]).setBorder(Border.NO_BORDER));
  57.         doc.add(table);
  58.         doc.close();
  59.     }
  60. }
TableSpace.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/34480476
  11.  */
  12. package com.itextpdf.samples.sandbox.objects;
  13.  
  14. import com.itextpdf.io.font.PdfEncodings;
  15. import com.itextpdf.kernel.font.PdfFont;
  16. import com.itextpdf.kernel.font.PdfFontFactory;
  17. import com.itextpdf.kernel.pdf.PdfDocument;
  18. import com.itextpdf.kernel.pdf.PdfWriter;
  19. import com.itextpdf.layout.Document;
  20. import com.itextpdf.layout.element.Paragraph;
  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. import java.io.IOException;
  28.  
  29. @Category(SampleTest.class)
  30. public class TableSpace extends GenericTest {
  31.     public static final String DEST = "./target/test/resources/sandbox/objects/table_space.pdf";
  32.     public static final String FONT = "./src/test/resources/font/PTM55FT.ttf";
  33.     public static final String[][] DATA = {
  34.             {"John Edward Jr.", "AAA"},
  35.             {"Pascal Einstein W. Alfi", "BBB"},
  36.             {"St. John", "CCC"}
  37.     };
  38.  
  39.     public static void main(String[] args) throws IOException {
  40.         File file = new File(DEST);
  41.         file.getParentFile().mkdirs();
  42.         new TableSpace().manipulatePdf(DEST);
  43.     }
  44.  
  45.     public Paragraph createParagraphWithSpaces(PdfFont font, String value1, String value2) {
  46.         Paragraph p = new Paragraph();
  47.         p.setFont(font);
  48.         p.add(String.format("%-35s", value1));
  49.         p.add(value2);
  50.         return p;
  51.     }
  52.  
  53.     public void manipulatePdf(String dest) throws IOException {
  54.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(DEST));
  55.         Document doc = new Document(pdfDoc);
  56.         PdfFont font = PdfFontFactory.createFont(FONT, PdfEncodings.CP1250, true);
  57.  
  58.         doc.add(createParagraphWithSpaces(font, String.format("%s: %s", "Name", DATA[0][0]), DATA[0][1]));
  59.         doc.add(createParagraphWithSpaces(font, String.format("%s: %s", "Surname", DATA[1][0]), DATA[1][1]));
  60.         doc.add(createParagraphWithSpaces(font, String.format("%s: %s", "School", DATA[2][0]), DATA[2][1]));
  61.  
  62.         doc.close();
  63.     }
  64. }
TableTab.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/34480476
  11.  */
  12. package com.itextpdf.samples.sandbox.objects;
  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.property.TabAlignment;
  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. import java.io.IOException;
  28.  
  29. @Category(SampleTest.class)
  30. public class TableTab extends GenericTest {
  31.     public static final String DEST = "./target/test/resources/sandbox/objects/table_tab.pdf";
  32.     public static final String FONT = "./src/test/resources/font/PTM55FT.ttf";
  33.     public static final String[][] DATA = {
  34.             {"John Edward Jr.", "AAA"},
  35.             {"Pascal Einstein W. Alfi", "BBB"},
  36.             {"St. John", "CCC"}
  37.     };
  38.  
  39.     public static void main(String[] args) throws IOException {
  40.         File file = new File(DEST);
  41.         file.getParentFile().mkdirs();
  42.         new TableTab().manipulatePdf(DEST);
  43.     }
  44.  
  45.     public Paragraph createParagraphWithTab(String key, String value1, String value2) {
  46.         Paragraph p = new Paragraph();
  47.         p.addTabStops(new TabStop(200f, TabAlignment.LEFT));
  48.         p.add(key);
  49.         p.add(value1);
  50.         p.add(new Tab());
  51.         p.add(value2);
  52.         return p;
  53.     }
  54.  
  55.     public void manipulatePdf(String dest) throws IOException {
  56.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(DEST));
  57.         Document doc = new Document(pdfDoc);
  58.  
  59.         doc.add(createParagraphWithTab("Name: ", DATA[0][0], DATA[0][1]));
  60.         doc.add(createParagraphWithTab("Surname: ", DATA[1][0], DATA[1][1]));
  61.         doc.add(createParagraphWithTab("School: ", DATA[2][0], DATA[2][1]));
  62.  
  63.         doc.close();
  64.     }
  65. }
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