PDFOne (for Java)
Create, edit, view, print & enhance PDF documents and forms in Java SE/EE
Compatibility
J2SE J2EE Windows Linux Mac (OS X)

How To Render Tables On A PDF Page Using Java

Learn how to render table cells, rows and columns using PDFOne (for Java).
By Santhanam L.

In this article, we will see how to render tabular data using the class PdfTable.

A table can be created by calling the constructor:

PdfTable(
  int columns, 
  double[] colwidth, 
  double cellHeight, 
  int measurementUnit)

In this constructor, columns is the number of columns in the table, colwidth specifies widths of all columns in the table, cellHeight specifies height of columns in the table, measurementUnit is a constant specifying the measurement unit with which the width and height are specified.

You can specify the padding levels in the cells by using the methods:

PdfTable.setCellBottomMargin(double cellBottomMargin);
PdfTable.setCellTopMargin(double cellTopMargin);
PdfTable.setCellLeftMargin(double cellLeftMargin);
PdfTable.setCellRightMargin(double cellRightMargin);

Then, individual cells can be added using the methods such as addCell(int rowSpan, int colSpan, String text, PdfFont font, Color backgroundColor). The rowSpan and colSpan arguments specify how many rows or columns the cell should span.

Here is the code snippet that puts it all together.

PdfDocument doc1 = new PdfDocument();
		
// Create a PDF page
PdfPage page = new PdfPage();

// Create a table
PdfTable tb = 
	new PdfTable(
	      3, 
	      new double[] { 1, 2, 2},
	      0.5, 
	      PdfMeasurement.MU_INCHES);
// Specify cell padding
tb.setCellBottomMargin(0.1);
tb.setCellTopMargin(0.1);
tb.setCellLeftMargin(0.1);
tb.setCellRightMargin(0.1);

PdfFont fHelveticaBold = 
    PdfFont.create("Helvetica", 
                   PdfFont.BOLD, 
                   12, 
                   PdfEncodings.CP1252);
fHelveticaBold.setColor(Color.BLACK);

// Add "Row 1" cells to the table
tb.addCell(1, 1, "S.No.", Color.LIGHT_GRAY, 
           fHelveticaBold, PdfTextFormatter.CENTER);
tb.addCell(1, 1, "First Name", Color.LIGHT_GRAY, 
           fHelveticaBold, PdfTextFormatter.CENTER);
tb.addCell(1, 1, "Second Name", Color.LIGHT_GRAY, 
           fHelveticaBold, PdfTextFormatter.CENTER);

// Add "Row 2" cells to the table		
tb.addCell(1, 1, "1.");
tb.addCell(1, 1, "Abraham");
tb.addCell(1, 1, "Lincoln");

// Add "Row 3" cells to the table
tb.addCell(1, 1, "2.");
tb.addCell(1, 1, "Thomas");
tb.addCell(1, 1, "Jefferson");

// Add "Row 4" cells to the table
tb.addCell(2, 1, "3.");
tb.addCell(1, 2, "George");
tb.addCell(1, 2, "Washington");

// Add the table to the page
page.addTable(tb, 100, 100);
	
doc1.add(page);
doc1.setOpenAfterSave(true);
doc1.save("PdfTable_example.pdf");
doc1.close();		

---o0O0o---

Our .NET Developer Tools
XtremeDocumentStudio .NET

Multi-format document-processing component suite for .NET developers.

PDFOne .NET

A .NET PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, and bookmark PDF documents in .NET applications.

Our Delphi/C++Builder developer tools
XtremeDocumentStudio Delphi

Multi-format document-processing component suite for Delphi/C++Builder developers, covering both VCL and FireMonkey platforms.

eDocEngine VCL

A Delphi/C++Builder component suite for creating documents in over 20 formats and also export reports from popular Delphi reporting tools.

PDFtoolkit VCL

A Delphi/C++Builder component suite to edit, enhance, view, print, merge, split, encrypt, annotate, and bookmark PDF documents.

Our Java developer tools
XtremeDocumentStudio (for Java)

Multi-format document-processing component suite for Java developers.

PDFOne (for Java)

A Java PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, bookmark PDF documents in Java applications.

Our Platform-Agnostic Cloud and On-Premises APIs
StarDocs

Cloud-hosted and On-Premises REST-based document-processing and document-viewing APIs

Privacy | Legal | Feedback | Newsletter | Blog | Resellers © 2002-2017 Gnostice Information Technologies Private Limited. All rights reserved.