IT源码网

java之pdfbox生成多页

daizhj 2024年08月06日 程序员 14 0

当我生成第二页时,我得到一个空白页。我有两个功能。一个生成包含文本的表格,另一个生成 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); 
                } 
            } 
         } 
    } 

请您参考如下方法:

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

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

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


评论关闭
IT源码网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!