PdfBox-Android添加页面文本叠加

时间:2015-12-28 13:26:28

标签: java android pdfbox

坚持使用PDFBox-Android,pdfbox-android:1.8.9.0

我加载pdf文件的第一页,写入文本并将此页面导入最终文档的新页面。

问题是在创建新页面时,它使用包含以前文本的最后一页...
所以,第一页是好的,但是nexts有文字叠加..

private File writeReport() {
    File fileSource = new File(getActivity().getApplicationContext().getExternalCacheDir(), "fileSource.pdf");

    // get file model in assets
    InputStream inputAsset = null;
    try {
        inputAsset = getActivity().getApplicationContext().getResources().getAssets().open("file_model.pdf");
    } catch (IOException e) {
        e.printStackTrace();
    }

    // copy file model in fileSource
    try {
        OutputStream outputStream = new FileOutputStream(file);
        byte buffer[] = new byte[1024];
        int length = 0;

        while ((length = inputAsset.read(buffer)) > 0) {
            outputStream.write(buffer, 0, length);
        }
        outputStream.close();
        inputAsset.close();
    } catch (IOException e) {
        System.out.print("Copy assets : IOException" + e.getMessage());
    }

    File fileTarget = new File(getActivity().getApplicationContext().getCacheDir(), "fileTarget.pdf");


    try {
        PDFBoxResourceLoader.init(getActivity().getApplicationContext());       // init lib

        PDDocument documentSource = PDDocument.load(fileSource);
        PDDocument documentTarget = new PDDocument();

        // iteration == a new page
        for(int i=0 ; i < 3 ; i++)
        {
            PDPage page = documentSource.getDocumentCatalog().getPages().get(0);
            PDPageContentStream contentStream = new PDPageContentStream(documentSource, page, true, true);

            PDFont font1 = PDType1Font.HELVETICA;

            float startY = page.getMediaBox().getUpperRightY();
            float factor = 2.83f;

            page.getStream();

            // add text
            contentStream.beginText();
            contentStream.setFont(font1, 10);
            contentStream.newLineAtOffset(factor * 60, startY - (factor * 53));
            contentStream.showText("test text" + i);
            contentStream.endText();

            contentStream.close();

            // import source page to output file->  Problem here ! new page contain old overlay text... 
            documentTarget.importPage(page);
        }
        documentTarget.save(fileTarget);
        documentTarget.close();
        documentSource.close();

    } catch (IOException e) {
        e.printStackTrace();
    }
    return fileTarget;
}

有没有办法在每次迭代时都有新页面? 谢谢!

1 个答案:

答案 0 :(得分:0)

问题是您重用了相同的PDPage对象,这导致它包含上一次迭代的文本。解决方案:使用

的结果
documentTarget.importPage(page)

并与之合作。因为这是一个克隆了所有内容的新PDPage对象。所以你的新代码将是这样的:

    // iteration == a new page
    for(int i=0 ; i < 3 ; i++)
    {
        PDPage page = documentSource.getDocumentCatalog().getPages().get(0);
        page = documentTarget.importPage(page); // this is now a new PDPage object
        PDPageContentStream contentStream = new PDPageContentStream(documentSource, page, true, true);

        PDFont font1 = PDType1Font.HELVETICA;

        float startY = page.getMediaBox().getUpperRightY();
        float factor = 2.83f;

        page.getStream();

        // add text
        contentStream.beginText();
        contentStream.setFont(font1, 10);
        contentStream.newLineAtOffset(factor * 60, startY - (factor * 53));
        contentStream.showText("test text" + i);
        contentStream.endText();

        contentStream.close();            
    }