Sunday, June 5, 2011

Create a Table Data into Cell in PDF

Now that i have been able to create text into the document. I thought entering Table Data into PDF could be of more help.

Just in Case You missed out the related article, you can check it out here
http://www.javaapionline.com/2011/06/create-pdf-document-using-java-pdfbox.html

Writing the entire code for creating Table like data Could be too complex to understand, so I have narrowed down the problem such that
  • I create a single cell in a PDF document and display text within the cell. You could extend and advance the logic to fit it to your needs.

/*
 * Author : Ajith Moni
 * Date : June 5th 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 PDFTest1 {

    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=50;
       
           
        // draw from left to right
        // ______________
       
        contentStream.drawLine(margin, y1, margin+length, y2);
       
        //draw from right to down - using breadth of rectangle
        //    ______________
        //                                |
       
        contentStream.drawLine(margin+length, y2, margin+length, y2-breadth);
        //draw line from left to right
        //    ______________
        //    ______________|
       
        contentStream.drawLine(margin,y1-breadth,margin+length,y2-breadth);
        // draw line from the remaining line
        //     ______________
        //    |______________|
        contentStream.drawLine(margin, y1, margin, y2-breadth);
   
       
        /*
         *  Draw text within the cell
         *  int length=100;
         *  int breadth=50;
         */
        // Setting Font is very important, else the text will not be displayed.
        contentStream.setFont(PDType1Font.HELVETICA_BOLD,12);
        contentStream.moveTextPositionByAmount(margin+30, y1-30);
        contentStream.drawString("Text");
        // Close the Text
        contentStream.endText();

        // Closing the Connection is Important
        // especially when using multiple pages
        contentStream.close();

        document.save("D:\\Trash\\PDFTableCell.pdf");
       
        document.close();   


    }
   
}

No comments:

Post a Comment

Please add value. Sharing is caring