0% found this document useful (0 votes)
174 views

Itext in Action 2Nd Edition: Chapter 4: Organizing Content in Tables

ColumnWidths example is part of the book iText in Action (ISBN 9781935182610) you can use this example for inspiration, but please buy the book if you don't understand something. This example only works with the AGPL version of the iText library.

Uploaded by

alinux75
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
174 views

Itext in Action 2Nd Edition: Chapter 4: Organizing Content in Tables

ColumnWidths example is part of the book iText in Action (ISBN 9781935182610) you can use this example for inspiration, but please buy the book if you don't understand something. This example only works with the AGPL version of the iText library.

Uploaded by

alinux75
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

iText in Action: example part1.chapter04.

ColumnWidths

About us | News | Contact

iText

Licenses

Support

Search

Go

You are here: Home > Support > Book > Chapter 4 > ColumnWidths

iText in Action 2nd Edition


The ColumnWidths example is part of the book iText in Action (ISBN 9781935182610). It's a small standalone application. You can use this example for inspiration, but please buy the book if there's something you don't understand about the example. Read Chapter 4 for more info.

Chapter 4: Organizing content in tables


Keywords for this example: PdfPTable, Space between tables, PdfPCell > colspan, PdfPCell > rowspan, PdfPTable > spacing before/after, PdfPTable > width If you want this example to work, you need the following jars: iText.jar

part1.chapter04.ColumnWidths
If you compile and execute this example, you'll get the following result: column_widths.pdf You can download the full source code of ColumnWidths, or read it here: /* * This class is part of the book "iText in Action - 2nd Edition" * written by Bruno Lowagie (ISBN: 9781935182610) * For more info, go to: http://itextpdf.com/examples/ * This example only works with the AGPL version of iText. */ package part1.chapter04; import java.io.FileOutputStream; import java.io.IOException; import import import import import import import com.itextpdf.text.Document; com.itextpdf.text.DocumentException; com.itextpdf.text.Phrase; com.itextpdf.text.Rectangle; com.itextpdf.text.pdf.PdfPCell; com.itextpdf.text.pdf.PdfPTable; com.itextpdf.text.pdf.PdfWriter;

public class ColumnWidths { /** The resulting PDF file. */ public static final String RESULT = "results/part1/chapter04/column_widths.pdf"; /** * Creates a PDF with five tables. * @param filename the name of the PDF file that will be created. * @throws DocumentException * @throws IOException */ public void createPdf(String filename) throws IOException, DocumentException { // step 1 Document document = new Document(); // step 2 PdfWriter.getInstance(document, new FileOutputStream(filename)); // step 3 document.open(); // step 4 PdfPTable table = createTable1(); document.add(table); table = createTable2(); table.setSpacingBefore(5); table.setSpacingAfter(5); document.add(table); table = createTable3(); document.add(table); table = createTable4(); table.setSpacingBefore(5); table.setSpacingAfter(5); document.add(table); table = createTable5(); document.add(table); // step 5 document.close(); } /** * Creates a table; widths are set with setWidths(). * @return a PdfPTable * @throws DocumentException */ public static PdfPTable createTable1() throws DocumentException { PdfPTable table = new PdfPTable(3); table.setWidthPercentage(288 / 5.23f); table.setWidths(new int[]{2, 1, 1}); PdfPCell cell; cell = new PdfPCell(new Phrase("Table 1"));

http://itextpdf.com/examples/iia.php?id=76[31/05/2012 19:13:29]

iText in Action: example part1.chapter04.ColumnWidths cell.setColspan(3); table.addCell(cell); cell = new PdfPCell(new Phrase("Cell with rowspan 2")); cell.setRowspan(2); table.addCell(cell); table.addCell("row 1; cell 1"); table.addCell("row 1; cell 2"); table.addCell("row 2; cell 1"); table.addCell("row 2; cell 2"); return table; } /** * Creates a table; widths are set with setWidths(). * @return a PdfPTable * @throws DocumentException */ public static PdfPTable createTable2() throws DocumentException { PdfPTable table = new PdfPTable(3); table.setTotalWidth(288); table.setLockedWidth(true); table.setWidths(new float[]{2, 1, 1}); PdfPCell cell; cell = new PdfPCell(new Phrase("Table 2")); cell.setColspan(3); table.addCell(cell); cell = new PdfPCell(new Phrase("Cell with rowspan 2")); cell.setRowspan(2); table.addCell(cell); table.addCell("row 1; cell 1"); table.addCell("row 1; cell 2"); table.addCell("row 2; cell 1"); table.addCell("row 2; cell 2"); return table; } /** * Creates a table; widths are set in the constructor. * @return a PdfPTable * @throws DocumentException */ public static PdfPTable createTable3() throws DocumentException { PdfPTable table = new PdfPTable(new float[]{ 2, 1, 1 }); table.setWidthPercentage(55.067f); PdfPCell cell; cell = new PdfPCell(new Phrase("Table 3")); cell.setColspan(3); table.addCell(cell); cell = new PdfPCell(new Phrase("Cell with rowspan 2")); cell.setRowspan(2); table.addCell(cell); table.addCell("row 1; cell 1"); table.addCell("row 1; cell 2"); table.addCell("row 2; cell 1"); table.addCell("row 2; cell 2"); return table; } /** * Creates a table; widths are set with special setWidthPercentage() method. * @return a PdfPTable * @throws DocumentException */ public static PdfPTable createTable4() throws DocumentException { PdfPTable table = new PdfPTable(3); Rectangle rect = new Rectangle(523, 770); table.setWidthPercentage(new float[]{ 144, 72, 72 }, rect); PdfPCell cell; cell = new PdfPCell(new Phrase("Table 4")); cell.setColspan(3); table.addCell(cell); cell = new PdfPCell(new Phrase("Cell with rowspan 2")); cell.setRowspan(2); table.addCell(cell); table.addCell("row 1; cell 1"); table.addCell("row 1; cell 2"); table.addCell("row 2; cell 1"); table.addCell("row 2; cell 2"); return table; } /** * Creates a table; widths are set with setTotalWidth(). * @return a PdfPTable * @throws DocumentException */ public static PdfPTable createTable5() throws DocumentException { PdfPTable table = new PdfPTable(3); table.setTotalWidth(new float[]{ 144, 72, 72 }); table.setLockedWidth(true); PdfPCell cell; cell = new PdfPCell(new Phrase("Table 5")); cell.setColspan(3); table.addCell(cell); cell = new PdfPCell(new Phrase("Cell with rowspan 2")); cell.setRowspan(2); table.addCell(cell); table.addCell("row 1; cell 1"); table.addCell("row 1; cell 2"); table.addCell("row 2; cell 1"); table.addCell("row 2; cell 2"); return table; } /** * Main method.

http://itextpdf.com/examples/iia.php?id=76[31/05/2012 19:13:29]

iText in Action: example part1.chapter04.ColumnWidths * @param args no arguments needed * @throws DocumentException * @throws IOException */ public static void main(String[] args) throws IOException, DocumentException { new ColumnWidths().createPdf(RESULT); } }

Content 2010-2012 1T3XT BVBA

http://itextpdf.com/examples/iia.php?id=76[31/05/2012 19:13:29]

You might also like