Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache PDFBox Java library - Is there an API for creating tables?

Tags:

I am using the Apache PDFBox java library to create PDFs. Is there a way to create a data-table using pdfbox? If there is no such API to do it, I would require to manually draw the table using drawLine etc., Any suggestions on how to go about this?

like image 493
Keshav Avatar asked Oct 06 '10 11:10

Keshav


People also ask

How do I create a dynamic table in PDFBox?

Please change the code so that it is complete, i.e. simulate your database input with some array for the drawTable() call. Also mention what PDFBox version you are using. javadoc of newLineAtOffset: "Move to the start of the next line, offset from the start of the current line by (tx, ty).".

Which is better iText or PDFBox?

One major difference is that PDFBox always processes text glyph by glyph while iText normally processes it chunk (i.e. single string parameter of text drawing operation) by chunk; that reduces the required resources in iText quite a lot.

Is PDFBox free for commercial use?

Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative ...


1 Answers

Source: Creating tables with PDFBox

The following method draws a table with the specified table content. Its a bit of a hack and will work for small strings of text. It does not perform word wrapping, but you can get an idea of how it is done. Give it a go!

/**  * @param page  * @param contentStream  * @param y the y-coordinate of the first row  * @param margin the padding on left and right of table  * @param content a 2d array containing the table data  * @throws IOException  */ public static void drawTable(PDPage page, PDPageContentStream contentStream,                              float y, float margin,                              String[][] content) throws IOException {     final int rows = content.length;     final int cols = content[0].length;     final float rowHeight = 20f;     final float tableWidth = page.findMediaBox().getWidth() - margin - margin;     final float tableHeight = rowHeight * rows;     final float colWidth = tableWidth/(float)cols;     final float cellMargin=5f;      //draw the rows     float nexty = y ;     for (int i = 0; i <= rows; i++) {         contentStream.drawLine(margin, nexty, margin+tableWidth, nexty);         nexty-= rowHeight;     }      //draw the columns     float nextx = margin;     for (int i = 0; i <= cols; i++) {         contentStream.drawLine(nextx, y, nextx, y-tableHeight);         nextx += colWidth;     }      //now add the text             contentStream.setFont( PDType1Font.HELVETICA_BOLD , 12 );              float textx = margin+cellMargin;     float texty = y-15;             for(int i = 0; i < content.length; i++){         for(int j = 0 ; j < content[i].length; j++){             String text = content[i][j];             contentStream.beginText();             contentStream.moveTextPositionByAmount(textx,texty);             contentStream.drawString(text);             contentStream.endText();             textx += colWidth;         }         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 = {{"a","b", "1"},                        {"c","d", "2"},                        {"e","f", "3"},                        {"g","h", "4"},                        {"i","j", "5"}} ;  drawTable(page, contentStream, 700, 100, content); contentStream.close(); doc.save("test.pdf" ); 
like image 77
dogbane Avatar answered Sep 24 '22 17:09

dogbane