39

We are planning to migrate our pdf generation utilities from iText to PDFBox (Due to licensing issues in iText). With some effort, I was able to write and position text, draw lines etc. But creating Tables with text embedded in Table cells is a challenge, I went through the documentation, examples, Google, Stackoverflow couldn't find a thing. Was wondering if PDFBox provides native support for creating Tables with embedded text. My last resort would be to use this link https://github.com/eduardohl/Paginated-PDFBox-Table-Sample

Anil
  • 1,735
  • 2
  • 20
  • 28
  • 1
    As far as document creation is concerned, PDFBox mainly is an equivalent to the low level API of iText. What you are missing is something replacing iText's high level API on top of that. I'm not aware of something like that available to the public. – mkl Jan 21 '15 at 06:25
  • Yeah, you nailed it. High level API for PDFBox for creating all these tables is what is missing. A lot of dev community is migrating from iText to other open source pdf libraries and I am sure someone will have an elegant solution. – Anil Jan 21 '15 at 06:33
  • 4
    Here's another: https://github.com/dhorions/boxable – Tilman Hausherr Jan 21 '15 at 07:11
  • And another: https://stackoverflow.com/questions/3871879/apache-pdfbox-java-library-is-there-an-api-for-creating-tables – Tilman Hausherr Jan 21 '15 at 07:30
  • @TilmanHausherr I'm afraid all those samples IMO meely are proofs of concept, probably of use in limited use cases but by far not for generic use. PDFBox has its strengths, e.g. a quite versatile content extraction framework and a content rendering capability, but the absence a proper layouting API is a serious weakness. – mkl Jan 21 '15 at 08:22
  • I know... I just don't want to create another iText. We're not the Samwer brothers. But I'll write something about that topic on the dev list tonight. – Tilman Hausherr Jan 21 '15 at 09:35
  • Thanks Tilman for the suggestions. I'll go through them and update my comments here. iText is an awesome library but their licensing really sucks. I myself am writing a lot of wrappers around pdfbox, when time permits, I'll publish the same. – Anil Jan 21 '15 at 12:55
  • I've posted my thoughts not in the dev list but in a JIRA issue here: https://issues.apache.org/jira/browse/PDFBOX-2618 – Tilman Hausherr Jan 21 '15 at 21:11
  • @Anil `I am sure someone will have an elegant solution` 7 years later and we still don't have that elegant solution – late1 Aug 22 '22 at 18:11

3 Answers3

109

Since I also needed table drawing functionality for a side project, I implemented a small "table drawer" library myself, which I uploaded to github.

In order to produce such a table – for instance – ... enter image description here

... you would need this code. In the same file you find the code for that table as well:

enter image description here

The current "feature list" includes:

  • set font and font size on table level as well as on cell level
  • define single cells with bottom-, top-, left- and right-border width separately
  • define the background color on row or cell level
  • define padding (top, bottom, left, right) on cell level
  • define border color (on table, row or cell level)
  • specify text alignment (vertical and horizontal)
  • cell spanning and row spanning
  • text wrapping and line spacing

Also it should not be too hard to add missing stuff like having different border colors for borders on top, bottom, left and right-borders, if needed.

philonous
  • 3,621
  • 3
  • 27
  • 24
  • Tried to give this a shot.. are you missing a class `AWTColor` in the source? – cklab Apr 19 '17 at 16:32
  • Indeed! Thanks for your feedback! :) Obviously I didn't carefully check the last pull request. Please git pull again and it should be working now. – philonous Apr 20 '17 at 05:43
  • what about pagination? – Sandun Chathuranga Feb 21 '19 at 09:29
  • 1
    You mean a table spanning over several pages? If so, have a look at this code which produces a table over three pages: https://github.com/vandeseer/easytable/blob/master/src/test/java/org/vandeseer/integrationtest/TableOverSeveralPagesTest.java – philonous Feb 21 '19 at 09:45
  • @philonous does it support adding multiple tables one after another over several pages? – Sagar Kadu Aug 05 '19 at 11:47
  • Well, if you want to draw a table over multiple pages, you can find an example here: [TableOverSeveralPagesTest.java](https://github.com/vandeseer/easytable/blob/master/src/test/java/org/vandeseer/integrationtest/TableOverSeveralPagesTest.java) – philonous Aug 05 '19 at 11:58
  • @philonous example shows only 1 table which is drawn over several pages..My use case is to have different tables which are next to each other & they could span across multiple pages – Sagar Kadu Aug 05 '19 at 12:11
  • I think you would need to do at least some calculations yourself: You would need to keep track of the point where the next table should start to be drawn if another one just has finished. That shouldn't be too hard though. – philonous Aug 05 '19 at 12:32
  • @philonous do you happen to have example of it?I tried but seems like pagination is not happening properly with multiple tables – Sagar Kadu Aug 07 '19 at 11:42
  • I don't have one yet, but just open an issue (tagged as question) on github and we can continue there. – philonous Aug 07 '19 at 15:22
30

Thanks to the links provided by Tilman. Using the boxable API (https://github.com/dhorions/boxable) I was able to create the table I wanted to. Just an FYI I wanted to create the table with variable number of cells. For example row 1 would have 2 cells, row 2 could have 5 cells and row 3 could have just 3 cells. I was able to do with ease. I followed Example1.java in the link mentioned above.

Anil
  • 1,735
  • 2
  • 20
  • 28
  • 1
    I couldnt find Example1.java you mentioned in your answer. WIll you please paste your code or Example1.java. – Darshan Puranik Feb 16 '17 at 11:45
  • 1
    @DarshanPuranik: perhaps this [test](https://github.com/dhorions/boxable/blob/master/src/test/java/be/quodlibet/boxable/TableTest.java) may help you. – Mohammad Faisal Oct 10 '17 at 07:33
0

This example code works for me. I think this would be helpful to you

public static void creteTablePdf() throws IOException {
    PDDocument document = new PDDocument();
    PDPage page = new PDPage();
    document.addPage(page);

    int pageWidth = (int)page.getTrimBox().getWidth(); //get width of the page
    int pageHeight = (int)page.getTrimBox().getHeight(); //get height of the page

    PDPageContentStream contentStream = new PDPageContentStream(document,page);
    contentStream.setStrokingColor(Color.DARK_GRAY);
    contentStream.setLineWidth(1);

    int initX = 50;
    int initY = pageHeight-50;
    int cellHeight = 20;
    int cellWidth = 100;

    int colCount = 3;
    int rowCount = 3;

    for(int i = 1; i<=rowCount;i++){
        for(int j = 1; j<=colCount;j++){
            if(j == 2){
                contentStream.addRect(initX,initY,cellWidth+30,-cellHeight);

                contentStream.beginText();
                contentStream.newLineAtOffset(initX+30,initY-cellHeight+10);
                contentStream.setFont(PDType1Font.TIMES_ROMAN,10);
                contentStream.showText("Dinuka");
                contentStream.endText();

                initX+=cellWidth+30;
            }else{
                contentStream.addRect(initX,initY,cellWidth,-cellHeight);

                contentStream.beginText();
                contentStream.newLineAtOffset(initX+10,initY-cellHeight+10);
                contentStream.setFont(PDType1Font.TIMES_ROMAN,10);
                contentStream.showText("Dinuka");
                contentStream.endText();

                initX+=cellWidth;
            }
        }
        initX = 50;
        initY -=cellHeight;
    }

    contentStream.stroke();
    contentStream.close();


    document.save("C:\\table.pdf");
    document.close();
    System.out.println("table pdf created");
}