pdfbox生成多页

时间:2013-11-25 10:57:26

标签: java pdf pdfbox

当我生成第二页时,我得到一个空白页面。我有两个功能。一个生成带有文本的表,另一个生成PDF。当在第一页结束时,我添加另一页,我想在新页面写。当我打开生成的PDF文件时,第二页是空白的:

    //global variables
PDPage nowa=null;
PDPageContentStream contentStream1 = null;

//function to generate table
private void print_sumActionPerformed(java.awt.event.ActionEvent evt){
        try {
            PDDocument doc = null;
            PDPage page = null;
            int max_row=55;
            int suma=0;
            int pozycja=0;
            final int starty=760;
            final int startx=30;
        try{
                doc = new PDDocument();
                page = new PDPage(PDPage.PAGE_SIZE_A4);
                doc.addPage(page);
                PDFont font = PDType1Font.HELVETICA;
                PDPageContentStream content = new PDPageContentStream(doc, page,true,true);
                //some code to generate table
                drawTable(page, content, starty, startx, content1,doc);
                content.close();
                doc.save("path");
                doc.close();
                Thread.sleep(500);
            } catch (Exception e){
                System.out.println(e);
            }
        } catch (IOException ex) {
            Logger.getLogger(Okno.class.getName()).log(Level.SEVERE, null, ex);
        } catch (PrinterException ex) {
            Logger.getLogger(Okno.class.getName()).log(Level.SEVERE, null, ex);
    } 
}                                      

以PDF格式生成表格的函数

private void drawTable(PDPage page, PDPageContentStream contentStream,float y, float margin, String[][] content,PDDocument doc) throws IOException, InterruptedException {
        final int rows = content.length;
        final int cols = content[0].length;
        final float rowHeight = 13f;
        final float marginCell=5;
        final float startx=30;
        final int tableWidth3=200;
        final int tableWidth5=250;
        boolean new_kol;
        if (margin<230){
            new_kol=false;
        }else {
            new_kol=true;
        }
        float textx;
        final float odst=20;
        final float starty = y;
        float texty=starty-rowHeight+3;
        //width table
        int tableWidth;
        if(cols==5)tableWidth=tableWidth5;
        else tableWidth=tableWidth3;
        //start print table in pdf
        if(!new_kol){
            contentStream.drawLine(startx,starty,tableWidth+startx+marginCell,starty);
            textx=startx+marginCell;
        }else {
            contentStream.drawLine(startx+tableWidth5+odst, starty, 2*tableWidth5+(startx)+odst+marginCell,starty);
            textx=startx+marginCell+tableWidth+odst;
        }
         for(int i = 0; i < content.length; i++){
            for(int j = 0 ; j < content[i].length; j++){
                //linia pionowa
                contentStream.drawLine(textx-marginCell,texty-3,textx-marginCell,texty-3+rowHeight);
                String text=content[i][j];
                 if(text.contains("AB")){
                    contentStream.setFont(PDType1Font.HELVETICA_BOLD,10);
                }else contentStream.setFont(PDType1Font.HELVETICA,8);
                contentStream.beginText();
                contentStream.moveTextPositionByAmount(textx,texty);
                contentStream.drawString(text);
                contentStream.endText();
                switch(j){
                    case 0: textx+=30;
                        break;
                    case 1:
                        if (cols==5) textx += 120;
                        else textx+=150;
                        break;
                    case 2: textx+=15;
                        if(cols==3 ) contentStream.drawLine(textx+marginCell,texty-3,textx+marginCell,texty-3+rowHeight);
                        break;
                    case 3: textx+=40;
                        break;
                    case 4: textx+=40;
                    contentStream.drawLine(textx+marginCell,texty-3,textx+marginCell,texty-3+rowHeight);
                        break;
                }
            }
            if(new_kol){
                textx=tableWidth+startx+odst+marginCell;
                contentStream.drawLine(textx-marginCell,texty-3,textx+tableWidth,texty-3);
            }
            else {
                textx=startx+marginCell;
                contentStream.drawLine(textx-marginCell,texty-3,textx+tableWidth,texty-3);
            }
            if((texty-=rowHeight)<50){
                if(!new_kol){
                    new_kol=true;
                    contentStream.drawLine(startx,texty+10,startx+tableWidth+marginCell,texty+10);
                    texty=760-10;
                    textx=tableWidth+startx+odst+marginCell;
                    contentStream.drawLine(textx-marginCell,texty+10,textx+tableWidth,texty+10);
                }else{
                    new_kol=false;
                    contentStream.drawLine(startx+tableWidth5+odst,texty+10,startx+2*tableWidth,texty+10);
                    texty=760-10;
                    textx=startx+marginCell;
                    //here i add new page when end page height
                    //i get blank page
                    contentStream.close();
                    page=null;
                    contentStream=null;
                    nowa=new PDPage(PDPage.PAGE_SIZE_A4);
                    page=null;
                    page=nowa;
                    doc.addPage(nowa);
                    PDFont font = PDType1Font.HELVETICA;
                    contentStream1 = new PDPageContentStream(doc, nowa,false,true);
                    contentStream=contentStream1;
                    contentStream1=null;
                    contentStream.drawLine(textx,texty+10,textx+tableWidth,texty+10);
                }
            }
         }
    }

1 个答案:

答案 0 :(得分:1)

它可能是你的构造函数。我想如果你使用:

PDPageContentStream content = new PDPageContentStream(doc, page,true,true, false);

它应该按预期工作。最后一个参数对应于构造函数中的resetContext值,并决定是否应重置当前图形上下文(Source)。