简体   繁体   English

pdfbox生成多页

[英]pdfbox generate multi page

When I generate second page, I get a blank page. 当我生成第二页时,我得到一个空白页。 I have two functions. 我有两个功能。 One generates a table with text, and another generates a PDF. 一个生成带有文本的表格,另一个生成PDF。 When end place in first page, I add another page and I want write in new page. 当在第一页结束时,我添加另一页,我想在新页中写。 When I open generated PDF file, the second page is blank: 当我打开生成的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);
    } 
}                                      

function to generate table in pdf 生成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);
                }
            }
         }
    }

It could probably be your constructor. 它可能是您的构造函数。 I think if you use : 我认为如果您使用:

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

it should work as expected. 它应该按预期工作。 The final parameter corresponds to the resetContext value in the constructor and decides whether the current graphic context should be reset or not ( Source ) . 最后一个参数对应于构造函数中的resetContext值,并决定是否应重置当前图形上下文( )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM