Monday, September 8, 2008

Getting started with iText - Tables

A reader named Turnbull was asking about PDFTables and I realized I did not have any examples on tables posted. So here are the first sections from from the iText tutorial on tables.

Lather, rinse, repeat

The java examples are nice and simple. They demonstrate how easy it is to create a basic table. Simply define a table object, add cells to it. Then add the table to your document. You can apply formats and advanced settings to the cells. But the basic construct consists of those three steps.


Documentation: iText Tutorial - Tables
Source Code: MyFirstTable.java , TableWidthAlignment.java , TableSpacing.java
Requirements:
Some of the examples use Mark Mandel's JavaLoader.cfc to load a newer version of iText


My First Table

Use a PdfPTable to add a table to a PDF document


<h1>My First PdfPTable (MX7 compatible)</h1>
Use a PdfPTable to add a table to a PDF document
<cfscript>
savedErrorMessage = "";


pathToOutputFile = ExpandPath("MyFirstTable.pdf");

try {
// step 1: creation of a document-object
document = createObject("java", "com.lowagie.text.Document").init();

// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter = createObject("java", "com.lowagie.text.pdf.PdfWriter");
FileOutputStream = createObject("java", "java.io.FileOutputStream");
outStream = FileOutputStream.init( pathToOutputFile );
writer = PdfWriter.getInstance(document, outStream);

// step 3: we open the document
document.open();

// create reusable object
Paragraph = createObject("java", "com.lowagie.text.Paragraph");
PdfPCell = createObject("java", "com.lowagie.text.pdf.PdfPCell");
Color = createObject("java", "java.awt.Color");

// create a table with three columns
PdfPTable = createObject("java", "com.lowagie.text.pdf.PdfPTable");
table = PdfPTable.init( javacast("int", 3) );

// create a header cell
cell = PdfPCell.init( Paragraph.init("header with colspan 3") );
// force header cell to span all three columns
cell.setColspan( javacast("int", 3) );
// add the header to the table
table.addCell( cell );

// add some cells with plain data
table.addCell("1.1");
table.addCell("2.1");
table.addCell("3.1");
table.addCell("1.2");
table.addCell("2.2");
table.addCell("3.2");

// add some formatted cells
cell = PdfPCell.init( Paragraph.init("cell test1") );
cellColor = Color.init( javacast("int", 255), javacast("int", 0), javacast("int", 0) );
cell.setBorderColor( cellColor );
table.addCell( cell );
cell = PdfPCell.init( Paragraph.init("cell test2") );
cell.setColspan( javacast("int", 2) );

// converted java values to decimal RGB values using:
// value = inputBaseN("0xC0", 16);
cellColor = Color.init( javacast("int", 192), javacast("int", 192), javacast("int", 192) );
cell.setBackgroundColor( cellColor );
table.addCell( cell );

// Finally, add the table to the document
document.add(table);

}
catch (com.lowagie.text.DocumentException de) {
savedErrorMessage = de;
}
catch (java.io.IOException ioe) {
savedErrorMessage = ioe;
}

// step 5: always close document and output stream
document.close();
if ( IsDefined("outStream") ) {
outStream.close();
}

WriteOutput("<hr>Finished!");
</cfscript>


<!--- show any errors --->
<cfif len(savedErrorMessage)>
Error creating document
<cfdump var="#savedErrorMessage#">
</cfif>


Table Width Alignment
Changing the width and the alignment of the complete table


<h1>TableWidthAlignment (MX7 compatible)</h1>
Changing the width and the alignment of the complete table
<cfscript>
savedErrorMessage = "";

pathToOutputFile = ExpandPath("TableWidthAlignment.pdf");

try {
// step 1: creation of a document-object
PageSize = createObject("java", "com.lowagie.text.PageSize");
document = createObject("java", "com.lowagie.text.Document").init(PageSize.A4);

// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter = createObject("java", "com.lowagie.text.pdf.PdfWriter");
FileOutputStream = createObject("java", "java.io.FileOutputStream");
outStream = FileOutputStream.init( pathToOutputFile );
writer = PdfWriter.getInstance(document, outStream);

// step 3: we open the document
document.open();

// create reusable object
Paragraph = createObject("java", "com.lowagie.text.Paragraph");
PdfPCell = createObject("java", "com.lowagie.text.pdf.PdfPCell");
Color = createObject("java", "java.awt.Color");
PdfTable = createObject("java", "com.lowagie.text.pdf.PdfPTable");

// step4
// create a table with three columns
table = PdfTable.init( javacast("int", 3) );
// create a cell to contain the header
cell = PdfPCell.init( Paragraph.init("header with colspan 3") );
// set the header to span all three columns
cell.setColspan( javacast("int", 3) );
// add the header to the table
table.addCell( cell );

// add some cells with plain data
table.addCell("1.1");
table.addCell("2.1");
table.addCell("3.1");
table.addCell("1.2");
table.addCell("2.2");
table.addCell("3.2");

// add some formatted cells
cell = PdfPCell.init( Paragraph.init("cell test1") );
cellColor = Color.init( javacast("int", 255), javacast("int", 0), javacast("int", 0) );
cell.setBorderColor( cellColor );
table.addCell( cell );
cell = PdfPCell.init( Paragraph.init("cell test2") );
cell.setColspan( javacast("int", 2) );
cellColor = Color.init( javacast("int", 192), javacast("int", 192), javacast("int", 192) );
cell.setBackgroundColor( cellColor );
table.addCell( cell );

// step 5: add multiple copies of the table
// to the document to demonstrate different widths and alignment

// add the table to the document at its original size
document.add( table );

// add another copy of the table at 100% width
table.setWidthPercentage( javacast("float", 100) );
document.add( table );
// add another copy at 50% width, and align it to the right
table.setWidthPercentage( javacast("float", 50) );
table.setHorizontalAlignment( PdfTable.ALIGN_RIGHT);
document.add( table );
// add another copy and align it to the left
// note, the table width is still set at 50%
table.setHorizontalAlignment( PdfTable.ALIGN_LEFT );
document.add( table );
}
catch (java.lang.Exception de) {
savedErrorMessage = de;
}

// step 5: always close document and output stream
document.close();
if ( IsDefined("outStream") ) {
outStream.close();
}

WriteOutput("<hr>Finished!");
</cfscript>


<!--- show any errors --->
<cfif len(savedErrorMessage)>
Error creating document
<cfdump var="#savedErrorMessage#">
</cfif>


Table Spacing
Defining the spacing between the table and other content

<h1>TableSpacing (requires newer version of iText)</h1>
Defining the spacing between the table and other content
<cfscript>
savedErrorMessage = "";

pathToOutputFile = ExpandPath("TableSpacing.pdf");
javaLoader = server[application.MyUniqueKeyForJavaLoader];

try {
// step 1: creation of a document-object
PageSize = javaLoader.create("com.lowagie.text.PageSize");
document = javaLoader.create("com.lowagie.text.Document").init(PageSize.A4);

// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter = javaLoader.create("com.lowagie.text.pdf.PdfWriter");
FileOutputStream = createObject("java", "java.io.FileOutputStream");
outStream = FileOutputStream.init( pathToOutputFile );
writer = PdfWriter.getInstance(document, outStream);

// step 3: we open the document
document.open();

// create reusable object
Paragraph = javaLoader.create("com.lowagie.text.Paragraph");
PdfPCell = javaLoader.create("com.lowagie.text.pdf.PdfPCell");
Color = createObject("java", "java.awt.Color");
PdfTable = javaLoader.create("com.lowagie.text.pdf.PdfPTable");

// step4
// create a table with three columns
table = PdfTable.init( javacast("int", 3) );
// create a cell to contain the header
cell = PdfPCell.init( Paragraph.init("header with colspan 3") );
// set the header to span all three columns
cell.setColspan( javacast("int", 3) );
// add the header to the table
table.addCell( cell );

// add some cells with plain data
table.addCell("1.1");
table.addCell("2.1");
table.addCell("3.1");
table.addCell("1.2");
table.addCell("2.2");
table.addCell("3.2");

// add some formatted cells
cell = PdfPCell.init( Paragraph.init("cell test1") );
cellColor = Color.init( javacast("int", 255), javacast("int", 0), javacast("int", 0) );
cell.setBorderColor( cellColor );
table.addCell( cell );
cell = PdfPCell.init( Paragraph.init("cell test2") );
cell.setColspan( javacast("int", 2) );
cellColor = Color.init( javacast("int", 192), javacast("int", 192), javacast("int", 192) );
cell.setBackgroundColor( cellColor );
table.addCell( cell );

// step 5: add multiple copies of the table to the document
// to demonstrate different spacing options
table.setWidthPercentage( javacast("float", 50) );
document.add( Paragraph.init("We add 2 tables:") );
document.add( table );
document.add( table );

document.add( Paragraph.init("They are glued to eachother") );
document.add(table );
document.add( Paragraph.init("This is not very nice. Turn to the next page to see how we solved this") );
document.newPage();
document.add( Paragraph.init("We add 2 tables, but with a certain 'SpacingBefore':") );
table.setSpacingBefore( javacast("float", 15) );
document.add( table );
document.add( table );
document.add( Paragraph.init("Unfortunately, there was no spacing after.") );
table.setSpacingAfter( javacast("float", 15) );
document.add( table );
document.add( Paragraph.init("This is much better, don't you think so?") );

}
catch (java.lang.Exception de) {
savedErrorMessage = de;
}

// step 5: always close document and output stream
document.close();
if ( IsDefined("outStream") ) {
outStream.close();
}

WriteOutput("<hr>Finished!");
</cfscript>


<!--- show any errors --->
<cfif len(savedErrorMessage)>
Error creating document
<cfdump var="#savedErrorMessage#">
</cfif>

0 comments:

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Header image adapted from atomicjeep