Guides
Save a Document
ComPDF supports saving to the current document, saving as a new document, and saving to a file descriptor or Uri.
If the document is saved to the current document and incremental saving is supported, changes are appended to the original file. If the save type is non-incremental, or if document permissions need to be removed, the SDK uses the full save workflow to write the target file.
Save Options
| Method | Description |
|---|---|
save() | Saves to the current document, equivalent to save(PDFDocumentSaveType.PDFDocumentSaveIncremental). |
save(PDFDocumentSaveType saveType) | Specifies the save type. PDFDocumentSaveNoIncremental and PDFDocumentSaveRemoveSecurity use the full save workflow. |
save(PDFDocumentSaveType saveType, boolean fontSubset) | Specifies the save type and controls whether to subset fonts. |
saveAs(String absolutePath, boolean removeSecurity) | Saves to the specified path. When removeSecurity is true, document permissions are removed. |
saveAs(String absolutePath, boolean removeSecurity, boolean saveByFile) | Saves to the specified path and controls whether to save by file. |
saveAs(String absolutePath, boolean removeSecurity, boolean saveByFile, boolean fontSubset) | Saves to the specified path and controls the save method and font subsetting. |
saveAs(Uri uri, boolean removeSecurity) | Saves to the specified Uri. |
saveAs(Uri uri, boolean removeSecurity, boolean fontSubset) | Saves to the specified Uri and controls whether to subset fonts. |
Save the Current Document
When saving the current document, check whether the document has changes before calling save().
java
try {
if (document.hasChanges()) {
// Save to the current document.
boolean res = document.save();
}
} catch (CPDFDocumentException e) {
e.printStackTrace();
} finally {
// Close the document and release resources.
document.close();
}kotlin
try {
if (document.hasChanges()) {
// Save to the current document.
val res = document.save()
}
} catch (e: CPDFDocumentException) {
e.printStackTrace()
} finally {
// Close the document and release resources.
document.close()
}Save as a New Document
When saving to a new path or Uri, use saveAs(). Set removeSecurity to true to remove document permissions.
java
String outputPath = ...;
Uri outputUri = ...;
try {
// Save as a new document and remove document permissions.
document.saveAs(outputPath, true);
// Save to a Uri.
document.saveAs(outputUri, false);
} catch (CPDFDocumentException e) {
e.printStackTrace();
}kotlin
val outputPath = ...
val outputUri = ...
try {
// Save as a new document and remove document permissions.
document.saveAs(outputPath, true)
// Save to a Uri.
document.saveAs(outputUri, false)
} catch (e: CPDFDocumentException) {
e.printStackTrace()
}If you need to save through a file descriptor, or need to control font subsetting, use save(int fd, ...) or the overloads with fontSubset.