Ropardo Sowftware development company

Experience software development with ROPARDO S.R.L.

RSS Feed
RSS Feed
  • Home
  • About ROPARDO S.R.L
  • Our websites

iText – Constructing tables

iText is a Java library for developers looking to enhance web- and other applications with dynamic PDF document generation and/or manipulation.

Constructing tables with the iText library can be thought of as an easy task, but there is much more thought that should be put into this when generating large dynamic tables (variable number of columns and rows).  I found a few interesting solutions for some issues that may arise and I will post them here in the hope that they will be useful to others as well.

Splitting tables over several pages

If you have large tables, you cannot expect them to fit on one page. Happily, iText splits the table automatically between rows. If your table has a header that needs to be repeated on every page, you need to tell the table how many rows the header counts. This can be done with the method setHeaderRows. In the example, there is one header row:

Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream("AddBigTable.pdf"));
document.open();
 
PdfPTable table = new PdfPTable(4);
table.setHeaderRows(1);
 
String[] headerStrings = new String[] { "ROWNUM", "COL_A", "COL_B", "COL_C" };
for (String header : headerStrings) {
     PdfPCell headerCell = new PdfPCell(new Paragraph(header));
     headerCell.setGrayFill(0.7f);
     table.addCell(headerCell);
}
 
ResultSet r = s.executeQuery(" SELECT ROWNUM, COL_A, COL_B, COL_C FROM LARGE_TABLE");
while (r.next()) {
	long row = r.getLong("ROWNUM");
	PdfPCell cell = new PdfPCell(new Paragraph(String.valueOf(row)));
	table.addCell(cell);
	cell = new PdfPCell(new Paragraph(r.getString("COL_A")));
	table.addCell(cell);
	cell = new PdfPCell(new Paragraph(r.getString("COL_B")));
	table.addCell(cell);
	cell = new PdfPCell(new Paragraph(r.getString("COL_C")));
	table.addCell(cell);
}
document.add(table);
document.close();

It seems that you do not have to worry about how large your table is – iText will distribute it over different pages. However, in the example above it may be noticed that while iterating through the result set the PdfPTable-object keeps on growing, taking more and more memory that isn’t being released. In real-world applications, this can become a serious problem.

Efficient way to write very large tables

When you add objects to a Document, these objects are written to the output stream ‘as soon as possible’. The objects that have been written are made eligible for destruction. However, when you construct a PdfPTable and keep adding new cells without adding the table to the document the memory needed to store table data can exceed the memory available to your JVM. One solution is to fragment a large table into different small tables that are destroyed once they are added to the document and as the tables are glued to each other (don’t use setSpacingBefore and setSpacingAfter here) nobody will see the difference between one large table or several small tables. Nevertheless, there is a better solution: you can add a partially constructed table to the document, release the data in the table, and continue adding new cells.  For example, we can change the loop from the above example as following:

while (r.next()) {
	long row= r.getLong("ROWNUM");
	if (row % 100 == 0) {
		document.add(table);
		table.deleteBodyRows();
		table.setSkipFirstHeader(true);
	}
	PdfPCell cell = new PdfPCell(new Paragraph(String.valueOf(row)));
	table.addCell(cell);
	cell = new PdfPCell(new Paragraph(r.getString("COL_A")));
	table.addCell(cell);
	cell = new PdfPCell(new Paragraph(r.getString("COL_B")));
	table.addCell(cell);
	cell = new PdfPCell(new Paragraph(r.getString("COL_C")));
	table.addCell(cell);
}
document.add(table);

Splitting tables vertically

For tables that have a lot of columns, it may be needed to split the table vertically. To do this, you can use the PdfPTable.writeSelectedRows(int colStart, int colEnd, int rowStart, int rowEnd, float xPos, float yPos, PdfContentByte[] canvases)  method.

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
Get Shareaholic
Tags: iText Java PDF

 Posted in: Java
September 25, 2009 | Alexandru Gyulai | One Comment

One Response

  • komalam
    July 14, 2011
    1

    i want to split the pdf verically by allowing 8 columns in one page and other 8 columns in next page and so on. can u pls help me to do this.

    Thanks in advance.

Leave a Reply

 


  • « Previous post
  • Next post »
  • Recent Posts

    • Installing PyGraphviz on Windows
    • Convert python object to XML representation
    • Liferay Portlet Development
    • Norway Road Show 2011 private meeting invitation
    • Oracle OpenWorld 2011
  • Ropardo is Hiring

  • Subscribe

    • Add to Google Reader or Homepage Add to netvibes TopOfBlogs
  • Recent Comments

    • Rajkumar Pomaji on Bluetooth PC Remote Control
    • Stelian Morariu on GWT 2.1 – Uploading a file using the RPC mechanism
    • Sergio on GWT 2.1 – Uploading a file using the RPC mechanism
    • Artem on Liferay: Deployment will start in a few seconds… and how to realy start
    • rkd80 on GWT 2.1 – Uploading a file using the RPC mechanism
  • Archives

    • November 2011 (1)
    • September 2011 (4)
    • July 2011 (3)
    • June 2011 (2)
    • May 2011 (4)
    • April 2011 (4)
    • March 2011 (3)
    • February 2011 (2)
    • January 2011 (2)
    • December 2010 (1)
    • November 2010 (4)
    • October 2010 (4)
    • August 2010 (3)
    • July 2010 (3)
    • June 2010 (6)
    • May 2010 (8)
    • April 2010 (7)
    • March 2010 (9)
    • February 2010 (6)
    • January 2010 (5)
    • December 2009 (7)
    • November 2009 (9)
    • October 2009 (10)
    • September 2009 (14)
    • August 2009 (10)
    • July 2009 (1)
    • June 2009 (1)
    • May 2009 (1)
    • April 2009 (1)
    • March 2009 (1)
    • October 2008 (3)
    • October 2007 (3)
    • July 2007 (4)
    • June 2007 (1)
    • May 2007 (3)
  • Meta

    • Log in
    • Entries RSS
    • Comments RSS
    • WordPress.org
  • Categories

    • News (15)
    • Ropardo Team (8)
    • Ropardo Products (6)
      • File Tracking Client (4)
      • iManagement (2)
    • Software Development (83)
      • Microsoft.NET (22)
      • Java (40)
      • Oracle (8)
      • Power Builder (3)
      • Liferay (5)
      • Lotus Notes (9)
      • xWiki (4)
    • System Adminstration (13)
      • Linux (10)
      • Windows (3)
    • Programming (1)
    • Uncategorized (3)
    • Databases (10)
      • MSSQL (5)
      • PostgreeSQL (3)
    • Microsoft.NET (1)
    • Web Development (28)
      • ASP/ASPX (3)
      • Content Management Systems (1)
      • HTML/CSS (5)
      • Javascrip/AJAX (8)
      • PHP (7)
    • Oracle E Business Suite (6)
  • Tags

    .NET ajax blog C# certification client CMS control css database Debugging django Domino Eclipse extension file tracking filter fun gentoo google Hibernate how to html image iManagement import Java javascript jQuery liferay Linux Lotus Notes lotus script Oracle Oracle BI Publisher 11g PHP portal PostgreSQL powerbuilder Python SQL Telerik velocity xml Xwiki

© 2010 ROPARDO s.r.l..

Powered by WordPress. Styled by Ropardo