用java将我的arrraylist变成pdf文件

时间:2017-02-09 15:38:31

标签: java arraylist pdfbox

使用PDFbox将我的ArrayList的所有元素打印成pdf文件时遇到问题。问题是程序生成的pdf文件不包含ArrayList的所有元素。我不明白为什么,可能是因为ArrayList的所有元素都不适合一个页面,但是向pdf添加页面并没有解决问题。以下是我的代码的摘录:

public PDPageContentStream gettextperpage(PDDocument document, PDPageContentStream contentStream, ArrayList<Component> In, int startind) {
    int ht=740;

    try {
            contentStream.beginText();
            contentStream.setFont(font, 12);
            contentStream.newLineAtOffset(60,ht);

            int nbel=0;

            if (startind/13==8) {
                nbel=8;
            }
            else {
                nbel=13;
            }
            System.out.println(nbel);
            for(int i=0; i<nbel;i++)
            {   
                int ind=startind+i;
                Component cp=In.get(ind);
                //contentStream.beginText(); 
                contentStream.showText(cp.toString());

                contentStream.newLineAtOffset(0,-LINE_HEIGHT-30);

                //contentStream.endText(); 


            }
            //contentStream.showText( "Company:"+_asc +"  "+_desc);
            contentStream.endText();

            for (int i=0; i<13; i++)
            {
                PDImageXObject pdImage = PDImageXObject.createFromFile("4056182.jpg", document);
                contentStream.drawImage(pdImage, 550, ht-40,40,40);
                ht-=50;
            }

            contentStream.close();
        }
    catch (IOException ex) {
        ex.printStackTrace();
    }
    return contentStream;       

}
public void getoutputpdf(PDDocument document,ArrayList<Component> In) {

    for (int k=0; k<9; k++) {

        try {


            System.out.println(k);

            // Create a new blank page and add it to the document
            PDPage Page = new PDPage();
            document.addPage( Page );



            // Start a new content stream which will "hold" the to be created content
            PDPageContentStream contentStream = new PDPageContentStream(document, Page);
            contentStream=gettextperpage(document,contentStream,In,13*k);
            // Define a text content stream using the selected font, moving the cursor and drawing the text "Hello World"





            // Save the newly created document
            document.save("testtot.pdf");

            // finally make sure that the document is properly
            // closed.

        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

}

1 个答案:

答案 0 :(得分:0)

问题已经解决了。考虑到所有的评论,我想出了一个解决方案,在PDF格式的许多页面上打印ArrayList的内容。

public void gettextperpage(PDDocument document, PDPageContentStream contentStream, ArrayList<Component> In, int startind) {
    int ht=740;
    int h=0;
    try {
            contentStream.beginText();
            contentStream.setFont(font, 12);
            contentStream.newLineAtOffset(60,ht);

            int nbel=0;

            if (startind/13==8) {
                nbel=8;
                h=-280;
            }
            else {
                nbel=13;
                h=-10;
            }
            //System.out.println(nbel);
            for(int i=0; i<nbel;i++)
            {   
                int ind=startind+i;
                Component cp=In.get(ind);
                //contentStream.beginText(); 
                contentStream.showText(cp.toString());

                contentStream.newLineAtOffset(0,-LINE_HEIGHT-30);

                //contentStream.endText(); 
            }
            //contentStream.showText( "Company:"+_asc +"  "+_desc);
            contentStream.newLineAtOffset(250,h);
            contentStream.showText(Integer.toString((startind+13)/13));
            contentStream.endText();

            for (int i=0; i<nbel; i++)
            {
                int ind=startind+i;
                Component cp=In.get(ind);
                if (!cp.getimage().isEmpty()) {
                    String imagepath="photospieces/"+cp.getimage();
                    PDImageXObject pdImage = PDImageXObject.createFromFile(imagepath, document);
                    contentStream.drawImage(pdImage, 550, ht-40,40,40);

                }
                ht-=50;
            }
            contentStream.close();
        }
    catch (IOException ex) {
        ex.printStackTrace();
    }
    return contentStream;       
}

public void getoutputpdf(PDDocument document,ArrayList<Component> In) {
    headerpage(document);
    for (int k=0; k<9; k++) {
        try {
            //System.out.println(k);

            // Create a new blank page and add it to the document
            PDPage Page = new PDPage();
            document.addPage( Page );

            // Start a new content stream which will "hold" the to be created content
            PDPageContentStream contentStream = new PDPageContentStream(document, Page);
            contentStream=gettextperpage(document,contentStream,In,13*k);
            // Define a text content stream using the selected font, moving the cursor and drawing the text "Hello World"
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

函数gettextperpage在pdf的一页上写了一小块arraylist。函数的一个参数确定将在页面上写入哪个arraylist块。 函数getoutputpdf在arraylist的不同部分应用函数gettextperpage,以生成pdf文件的多个页面。