Monday, June 6, 2011

Create 5 Table Cell Data in Single Column in a PDF File

Jar : PDFBox from Apache

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


An Extension to the earlier program.
Create 5 Rows of Data Cells

I decided to add in 5 Cells in a Single Column

Here's how the code will look.

/*
 * 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 PDFTableSingleColumn {

    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;
       
       
        // draw 5 cells having single column
            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;
            }
            // Draw the closing of the Cell
            contentStream.drawLine(margin, y1, margin+length, y2);
           
       
                // Close the Text
                contentStream.endText();

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


    }
   
}
For Creating a Compelete Table with N Rows and N columns, See the Article  :
http://www.javaapionline.com/2011/06/create-table-with-rows-and-columns-in.html
Also Read :

No comments:

Post a Comment

Please add value. Sharing is caring