Save PDFs to local storage in Java
This guide explains how to save a document to local storage.
Save and save as
To save any changes made to a PdfDocument
, use save
or saveAs
.
save
will overwrite the original document:
document.save(new DocumentSaveOptions.Builder().build());
Use saveAs
to write to a specific new location. Use the FileDataProvider
for saving to the file system:
File file = new File("/path/to/new/save-location.pdf"); document.saveAs(new FileDataProvider(file), new DocumentSaveOptions.Builder().build());
Document save options
Use DocumentSaveOptions
for more control over a document’s saving behavior. The following options are supported:
-
applyRedactionAnnotations
— Applies any redaction annotations present. A full save will be performed, as it isn’t possible to apply redactions incrementally. -
flattenAnnotations
— Flattens any annotations present. Theincremental
flag will be ignored when flattening. -
forceSave
— Forces a save even if there were no modifications. -
incremental
— Instead of rewriting a PDF, changes will be appended to it. This typically results in a significantly faster save if the document is large, as only the changes are added. It isn’t possible to save incrementally when theflattenAnnotations
orapplyRedactionsAnnotations
options are turned on.
Example
DocumentSaveOptions
can be created using the save options Builder
. Here’s an example showing how to flatten annotations when saving:
document.save(new DocumentSaveOptions.Builder().flattenAnnotations(true).build());