Basic Operations
There are a few commonly used basic operations when working with documents.
Open a Document
Open a local file.
CPDFDocument document = new CPDFDocument(context);
// Open document
CPDFDocument.PDFDocumentError error = document.open(pdfUri);
if (error == CPDFDocument.PDFDocumentError.PDFDocumentErrorPassword) {
// The document is encrypted and requires a password to open.
error = document.open(pdfUri, "password");
}
if (error == CPDFDocument.PDFDocumentError.PDFDocumentErrorSuccess) {
// The document is opened successfully and data can be parsed and manipulated.
} else {
//The PDF file is failed to open. You can refer to the API file for specific error messages.
}
//Check if the document has been repaired during the opening process.
if (document.hasRepaired()) {
//The repaired document needs to be saved first and then opened, otherwise any edits or modifications made to the repaired document cannot be saved.
//Perform a save operation
...
}
Following are the descriptions of error codes in CPDFDocumentError
:
Error | Description |
---|---|
PDFDocumentErrorNoReadPermission | No reading permission. |
PDFDocumentNotVerifyLicense | The license doesn't allow the permission. |
PDFDocumentErrorSuccess | Open PDF file successfully. |
PDFDocumentErrorUnknown | Unknown error. |
PDFDocumentErrorFile | File not found or could not be opened. |
PDFDocumentErrorFormat | File not in PDF format or corrupted. |
PDFDocumentErrorPassword | Password required or incorrect password. |
PDFDocumentErrorSecurity | Security scheme not supported. |
PDFDocumentErrorPage | Page not found or content error. |
Note: This is a time-consuming process, so it needs to be executed in a sub-thread. After the document is successfully opened, the UI that renders the PDF is initialized.
Save a Document
Save the document.
//First, see whether the document has been modified. If there are any modifications, save it.
if (document.hasChanges()) {
boolean res;
//Save to original document
res = document.save();
//Save to the specified path; isRemoveSecurity indicates whether to delete the password of the document.
//res = document.saveAs(uri, isRemoveSecurity);
}
//Close the document and release resources
document.close();
Note: This is a time-consuming process, so it needs to be executed in a sub-thread. Once the process is completed, the closing process of the document should be executed in the main thread, such as destroying the Activity.
Incremental saving, which is enabled by default, is the strategy that ComPDFKit uses to ensure saving is as fast as possible. This strategy always appends changes to the end of the document, and it never deletes anything. However, even though this is the fastest way to save a document, it does come with the cost of increasing the file size with each save. You can read more about incremental saving and full saving of PDFs here.
In most cases, the increase in file size is negligible. However, in some cases, you may want to prioritize file size over saving performance. Below you'll find some strategies to prevent file size from growing unnecessarily when saving changes to a document.
If the document is saved in a new path (Not the original storage path), there is going to trigger a non-incremental document save, which will rewrite the entire document instead of appending changes at the end.