Monday, June 6, 2011

Create Table with Rows and Columns in a PDF File

Jar Used : PDFBox from Apache

http://www.javaapionline.com/2011/06/create-pdf-document-using-java-pdfbox.html

The following code is used to Create a Table in PDF with 5 Rows and 3 Columns.




/*
 * Author : Ajith Moni
 * Date : June 6th 2011
 */
import java.io.IOException;

import org.apache.pdfbox.exceptions.COSVisitorException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType1Font;


public class PDFTableComplete {

    public static void main(String[] args)
            throws IOException, COSVisitorException {
       
        // Create the PDF Document
        PDDocument document =new PDDocument();
        // Create a Page of the PDF Document
        PDPage page1=new PDPage();
        // Add the created page to the PDF Document
        document.addPage( page1 );
       
        // Set Font Type
        PDFont font = PDType1Font.HELVETICA_BOLD;
       
        // Create a Stream for the Document and page
        PDPageContentStream contentStream = new PDPageContentStream(document, page1);
       
        // Begin Text
        contentStream.beginText();
        // The X and Y co-ordinates start from the end of the page
        // Use the following methods to get the results -
        // might vary on your machine.
        // 612.0
        // 792.0
        System.out.println(page1.getArtBox().getUpperRightX());
        System.out.println(page1.getArtBox().getUpperRightY());
       
        // Margin of the Document - X Value
        int margin=50;
       
        // Max val - 792 - Set Margin from top - Y Value
        int y1=750;
        int y2=750;
       
        // Dimensions of the cell to draw
        // length  - length of the line
        // breadth - height of the cell
        int length=100; 
        int breadth=30;
       
       
       
            // For Each Column Draw the length and Width
            for(int cols=0;cols<3;cols++)
            {
                // draw length line
                // draw breadth
               
                // draw rows
                for(int rows=0;rows<5;rows++)
                {
                    // draw the horizontal lines
                    contentStream.drawLine(margin, y1, margin+length, y2);
                    // breadth
                    contentStream.drawLine(margin, y1, margin, y1-breadth);
                    contentStream.drawLine(margin+length, y1, margin+length, y2-breadth);
                    y2-=breadth;
                    y1=y2;
                }
                contentStream.drawLine(margin, y1, margin+length, y2);
                margin=margin+length;
                y1=y2=750;
            }
       

                // Close the Text
                contentStream.endText();

                // Closing the Connection is Important
                // especially when using multiple pages
                contentStream.close();
       
        document.save("D:\\Trash\\PDFTableComplete.pdf");
       
        document.close();   


    }
   
}

No comments:

Post a Comment

Please add value. Sharing is caring