简体   繁体   中英

Apache PDFBox - Adobe Acrobat prompts saving

I'm using Apache PDFBox version 2.0.16 to add paging to an existing PDF file. My method is working great, the generated PDF is fine. 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. 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.reset();

Also call load() directly with the byte array:

PDDocument.load(os.toByteArray());

and update to the current version, which is 2.0.19 at this time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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