如何在PDFBox中创建动态表

时间:2016-02-13 13:15:06

标签: java oracle-adf pdfbox

我想在PDFBox中创建一个表,但我无法在表中插入获取的值。如果我尝试它只是被覆盖。在这里,我还要插入许多其他表格。

// some text 

// and table 

// some text

// table

如何像我在这里给出的特定区域一样动态添加表格?

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

table in pdf

制作表格的代码是:

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;
    }
}

0 个答案:

没有答案