I'm while using Apache PDFBox java library to produce Ebooks. It is possible to way to produce a data-table using pdfbox? If there's no such API to get it done, I'd require to by hand draw the table using drawLine etc., Any suggestions on how to pull off this?

Thanks
-Keshav

Source: Creating tables with PDFBox

The next method draws a table using the specified table content. Its a little of the hack and is useful for small strings of text. It doesn't perform word wrapping, but you will get a concept of how it's done. Give it a try!

/**

 * @param page

 * @param contentStream

 * @param y the y-coordinate from the first row

 * @param margin the padding on right and left of table

 * @param content a 2d array that contains the table data

 * @throws IOException

 */

public static void drawTable(PDPage page, PDPageContentStream contentStream,

                            float y, float margin,

                            String[][] content) throws IOException 

    //draw the posts

    float nextx = margin

    for (int i =  i <= cols i++) 

    //now add the written text

    contentStream.setFont( PDType1Font.HELVETICA_BOLD , 12 )

    float textx = margin+cellMargin

    float texty = y-15

    for(int i =  i < content.length i++)

        texty-=rowHeight

        textx = margin+cellMargin

    



Usage:

PDDocument doc = new PDDocument()

PDPage page = new PDPage()

doc.addPage( page )

PDPageContentStream contentStream = new PDPageContentStream(doc, page)

String[][] content = ,

                      ,

                      ,

                      ,

                      

drawTable(page, contentStream, 700, 100, content)

contentStream.close()

doc.save("test.pdf" )

I'm not sure much about Apache PDFBox, however i realize that iText can keep it in check. It's my first option for dealing with Ebooks in Java.

Here is a tutorial which i believe shows how.