Skip to content

docs: correct the JavaDoc of the PdfStamper append flag - #1819

Open
Inf1mum wants to merge 1 commit into
LibrePDF:masterfrom
Inf1mum:fix/pdfstamper-append-javadoc-1598
Open

Inf1mum wants to merge 1 commit into
LibrePDF:masterfrom
Inf1mum:fix/pdfstamper-append-javadoc-1598

Conversation

@Inf1mum

@Inf1mum Inf1mum commented Dec 5, 2026 •

Copy link
Copy Markdown

Description of the new Feature/Bugfix

Issue #1598 reports that PdfStamper with append = true is dramatically faster than the default constructor, while its JavaDoc claims the opposite ("nothing is gained in speed or memory").

The JavaDoc is outdated, and this PR corrects it. What append really does, measured on master with a generated 45-page document holding about 23,000 objects (Java 25, best of three runs):

scenario default constructor append = true
stamp a small text on all 45 pages (~23k objects, ~72 MB) ~250 ms ~140 ms
stamp a small text on a small document ~10 ms ~10 ms

The gain grows with document size and object count. The reason is structural: in append mode the original bytes are copied unchanged to the output and only the new and modified objects are written as an incremental revision, while the default constructors re-read and re-serialize every object of the document.

The JavaDoc of PdfStamper(PdfReader, OutputStream, String, boolean) and the bare @param append of PdfStamperImp now describe:

  • that the output keeps the original revision intact, followed by an incremental revision with only new and modified objects;
  • that this is faster and uses less memory than rewriting the whole document, and keeps existing signatures valid (the usual reason for using it);
  • the side effects of append mode: it requires an error-free document, cannot change the encryption status and does not support flattening of form fields or free text annotations (all three are enforced by the existing code).

Fixes #1598

Unit-Tests for the new Feature/Bugfix

  • Unit-Tests added: PdfStamperAppendModeTest verifies that append mode keeps the original bytes unchanged as a prefix of the output and produces a readable document containing the added content, and that the default mode rewrites the whole document (not an incremental revision). No timing assertions, so the tests stay stable in CI.

Compatibilities Issues

None. Documentation-only change plus a new test; no method signatures or behaviour are changed. The changelog entry is in changelogs/4.0.1.md. The same JavaDoc text exists on the 3.0-java21 branch and can be ported there if wanted.

Your real name

Vadim Lazuk

Testing details

  • ./mvnw -pl openpdf-core test: 2595 tests, 0 failures, 0 errors, 3 skipped.
  • ./mvnw -pl openpdf-core checkstyle:check: passes.
  • Java 25 (Temurin 25.0.5.1+2), Windows 11 x64.

The JavaDoc of PdfStamper(PdfReader, OutputStream, String, boolean) claimed
that appending the changes as a new revision gains nothing in speed or
memory. That is not how it works: in append mode the original bytes are
copied unchanged and only the new and modified objects are written, while
the default constructors re-read and re-serialize every object of the
document. On a 45-page document with about 23,000 objects, stamping a
small text on every page measured roughly 250 ms without append and
140 ms with append = true (issue LibrePDF#1598).

The JavaDoc now describes the incremental revision, the speed and memory
gain, and the restrictions of append mode (error-free document required,
encryption status cannot be changed, flattening not supported). A test
verifies that append mode keeps the original bytes as a prefix of the
output and that the default mode rewrites the document.
@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 6 complexity · 3 duplication

Metric Results
Complexity 6
Duplication 3

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@sonarqubecloud

sonarqubecloud Bot commented Dec 5, 2026

Copy link
Copy Markdown

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PdfStamper with append = true drastically improves performance, even though JavaDoc says "nothing is gained in speed or memory"?

1 participant