简体   繁体   English

Apache PDFBox - Adobe Acrobat 提示保存

[英]Apache PDFBox - Adobe Acrobat prompts saving

I'm using Apache PDFBox version 2.0.16 to add paging to an existing PDF file.我正在使用 Apache PDFBox 2.0.16 版向现有的 PDF 文件添加分页。 My method is working great, the generated PDF is fine.我的方法效果很好,生成的 PDF 很好。 However, when I open the file with Adobe Acrobat Reader, if I try to close the file, it prompts an alert asking me if I want to save the file even though I haven't edited anything, and the file is not editable at first.但是,当我使用 Adobe Acrobat Reader 打开文件时,如果我尝试关闭文件,它会提示我是否要保存文件,即使我没有编辑任何内容,并且文件最初不可编辑. I can't manage to understand what's happening, and how to prevent it from prompting saving我无法理解发生了什么,以及如何防止它提示保存

My code is the following:我的代码如下:

private void paging(ByteArrayOutputStream os) throws IOException {
    PDDocument doc = PDDocument.load(new ByteArrayInputStream(os.toByteArray()));
    PDFont font = getFont(doc);
    PDPageTree pages = doc.getDocumentCatalog().getPages();
    for (int i = 0; i < pages.getCount(); i++) {
        PDPage page = pages.get(i);
        PDPageContentStream contentStream = new PDPageContentStream(doc, page, PDPageContentStream.AppendMode.APPEND, true, false);
        contentStream.beginText();
        contentStream.setFont(font, FONT_SIZE);
        contentStream.setNonStrokingColor(Color.BLACK);
        contentStream.newLineAtOffset(page.getCropBox().getWidth() - 40,  15);
        contentStream.showText((i + 1) + " / " + pages.getCount());
        contentStream.endText();
        contentStream.close();
    }

    doc.save(os);
    doc.close();
}

reset 'os' before saving, so that your ByteArrayOutputStream gets cleared and positioned at the beginning.在保存之前重置“os”,以便您的ByteArrayOutputStream被清除并定位在开头。

os.reset();

Also call load() directly with the byte array:也可以直接使用字节数组调用 load():

PDDocument.load(os.toByteArray());

and update to the current version, which is 2.0.19 at this time.并更新到当前版本,此时为 2.0.19。

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

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