How do I create a table in PDF document?

Category: com.lowagie, viewed: 3410 time(s).

This example shows how to create a table in a PDF document. Using the iText library we can use the PdfPTable and the PdfPCell to create table and table cell in our pdf document. The example use data in an array format as the content to be formatted in the table.

package org.kodejava.example.lowagie;

import com.lowagie.text.*;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfPCell;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.File;

public class PDFTableDemo {
    public static void main(String[] args) {
        String[] headers = new String[] {"NO", "USERNAME", "FIRST NAME", "LAST NAME"};
        String[][] data = new String[][] {
                {"1", "JDOW", "JOHN", "DOW"},
                {"2", "STIGER", "SCOTT", "TIGER"},
                {"3", "FBAR", "FOO", "BAR"}
        };

        //
        // Create a new document.
        //
        Document document = new Document(PageSize.LETTER.rotate());

        try {
            //
            // Get an instance of PdfWriter and create a Table.pdf file as an output.
            //
            PdfWriter.getInstance(document, new FileOutputStream(new File("Table.pdf")));
            document.open();

            //
            // Create an instance of PdfPTable. After that we transform the header and
            // data array into a PdfPCell object. When each table row is complete we
            // have to call the table.completeRow() method.
            //
            // For better presentation we also set the cell font name, size and weight.
            // And we also define the background fill for the cell.
            //
            PdfPTable table = new PdfPTable(headers.length);
            for (int i = 0; i < headers.length; i++) {
                String header = headers[i];
                PdfPCell cell = new PdfPCell();
                cell.setGrayFill(0.9f);
                cell.setPhrase(new Phrase(header.toUpperCase(), new Font(Font.HELVETICA, 10, Font.BOLD)));
                table.addCell(cell);
            }
            table.completeRow();

            for (int i = 0; i < data.length; i++) {
                for (int j = 0; j < data[i].length; j++) {
                    String datum = data[i][j];
                    PdfPCell cell = new PdfPCell();
                    cell.setPhrase(new Phrase(datum.toUpperCase(), new Font(Font.HELVETICA, 10, Font.NORMAL)));
                    table.addCell(cell);
                }
                table.completeRow();
            }

            document.addTitle("Table Demo");
            document.add(table);
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            document.close();
        }
    }
}
Click here to lend your support to: Kode Java Org and make a donation at www.pledgie.com !

 

Uncensored Newsgroups
Download Hundreds of Complimentary Industry Resources

Get hundreds of popular Industry magazines, white papers, webinars, podcasts, and more; all available at no cost to you. With more than 600 complimentary offers, you'll find plenty of titles to suit your professional interests and needs. Click Here and Sign up today!

Java Training

Sponsored Links

Our Friends

Statistics

Locations of visitors to this page
visitor stats