1

I want to make a table in PDFBox but I cannot insert the fetched values in the table. If I try to it just gets overridden. Here I want to insert many other tables also.

// some text 

// and table 

// some text

// table

How to add a table dynamically like the specific region I have given here?

drawTable(page, contentStream, **500**, 70, content, a);

table in pdf

My code to make the table is this:

public String printaction() {
    // Add event code here...
    try {
        PDDocument document = new PDDocument();
        PDPage page = new PDPage();
        document.addPage(page);
        
        PDFont font = PDType1Font.TIMES_ROMAN;
        PDPageContentStream contentStream =  new PDPageContentStream(document, page);
        float scale = 1f;
        
        String whereclause = "JOBID =:1 ";
        vo.setWhereClause(printlbl.getValue().toString());
        Integer count = vo.getRowCount();
        
        int fontSize = 16; // Or whatever font size you want.
        int marginTop = 30; 
        float titleWidth = font.getStringWidth("JOB DETAILS") / 1000 * fontSize;
        float titleHeight = font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
        if (count != 0) {
            Row ro = vo.getCurrentRow();
            contentStream.beginText();
            contentStream.setFont(font, 16);
            contentStream.newLineAtOffset((page.getMediaBox().getWidth() - titleWidth) / 2, page.getMediaBox().getHeight() - marginTop - titleHeight);
            contentStream.showText("JoB DeTailS");         
            contentStream.endText();

            RowSetIterator rsi = vo.createRowSetIterator(null);
            int a = vo.getRowCount();
            while(rsi.hasNext()) {
                Row ro3 = rsi.next();   
                System.out.println(ro3);
                if(ro3 != null) {                              
                    String[][] content = {
                        {"Designation:", "Salary","Hire Date"},
                        {ro3.getAttribute("Designation").toString(), ro3.getAttribute("Salary").toString(), ro3.getAttribute("HireDate").toString()}
                    };
                    drawTable(page, contentStream, 500, 70, content, a);
                }
            }
            // System.out.println("result set iteratot" + a);
            rsi.closeRowSetIterator();
        }

        contentStream.close();
        document.save("D:/Test_pdf.pdf");
        document.close();
    } catch (IOException e) {
        System.out.println("Error");
    }

    return null;
}

public static void drawTable(PDPage page, PDPageContentStream contentStream, float y, float margin, String[][] content, int a) throws IOException {
    final int rows = a;
    //content.length;
    final int cols = content[0].length;
    final float rowHeight = 20f;
    final float tableWidth = page.getMediaBox().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.moveTo(margin, nexty);
        contentStream.lineTo(margin+tableWidth, nexty);
        contentStream.stroke();
        nexty-= rowHeight;
    }

    //draw the columns
    float nextx = margin;
    for (int i = 0; i <= cols; i++) {
        contentStream.moveTo(nextx, y); 
        contentStream.lineTo( nextx, y-tableHeight);
        contentStream.stroke();
        nextx += colWidth;
    }

    //now add the text        
    contentStream.setFont( PDType1Font.TIMES_ROMAN , 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.newLineAtOffset(textx,texty);
            contentStream.showText(text);
            contentStream.endText();
            textx += colWidth;
        }
        texty-=rowHeight;
        textx = margin+cellMargin;
    }
}
Syscall
  • 19,327
  • 10
  • 37
  • 52
user2243471
  • 21
  • 2
  • 9
  • 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. – Tilman Hausherr Feb 13 '16 at 13:51
  • javadoc of newLineAtOffset: "Move to the start of the next line, offset from the start of the current line by (tx, ty).". Explanation in the PDF spec: "Move to the start of the next line, offset from the start of the current line by (tx, ty). tx and ty shall denote numbers expressed in unscaled text space units." – Tilman Hausherr Feb 13 '16 at 13:55
  • @TilmanHausherr okay.. i am using pdfbox-app 2.0.0 but the thing is i don't want to give offset upfront as the table and the text changes accordingly the value is fetched. – user2243471 Feb 13 '16 at 14:14
  • 1
    I am a little late to the party, but anyway: Recently I had a similar problem and in the end I wrote a small library to ease the process of creating tables which you can find here: https://github.com/vandeseer/easytable – also have a look at this thread for other options: http://stackoverflow.com/questions/28059563/how-to-create-table-using-apache-pdfbox – philonous Mar 08 '17 at 20:26

0 Answers0