On this page
Guides
Open a Document
ComPDFKit supports opening local PDF documents or creating new ones.
Open a Local PDF Document
The steps to open a local PDF document using a file path are as follows:
- Obtain the local file path.
- Initialize a
CDFDocument
object using the file path.
This example shows how to open a local PDF document:
java
CPDFDocument document = new CPDFDocument(context);
// Open the document
CPDFDocument.PDFDocumentError error = document.open(pdfPath);
if (error == CPDFDocument.PDFDocumentError.PDFDocumentErrorPassword) {
// For encrypted documents, it is necessary to use a password to decrypt them.
error = document.open(pdfPath, "password");
}
if (error == CPDFDocument.PDFDocumentError.PDFDocumentErrorSuccess) {
// The document has been successfully opened, and data can be parsed and manipulated.
} else {
// Unable to open the PDF file. You can refer to the API documentation for specific error messages.
}
// Check whether the document has been repaired during the opening process.
if (document.hasRepaired()) {
// The repaired document needs to be saved first before reopening. Otherwise, any edits or modifications made to the repaired document cannot be saved.
// Perform the save operation.
...
}
kotlin
val document = CPDFDocument(context)
val error = document.open(pdfPath)
// Open the document
when(error){
PDFDocumentError.PDFDocumentErrorPassword ->{
// For encrypted documents, it is necessary to use a password to decrypt them.
error = document.open(pdfPath, "password");
}
PDFDocumentError.PDFDocumentErrorSuccess ->{
// The document has been successfully opened, and data can be parsed and manipulated.
}
else ->{
// Unable to open the PDF file. You can refer to the API documentation for specific error messages.
}
}
// Check whether the document has been repaired during the opening process.
if (document.hasRepaired()) {
// The repaired document needs to be saved first before reopening. Otherwise, any edits or modifications made to the repaired document cannot be saved.
// Perform the save operation.
...
}
Create a New PDF Document
This example shows how to create a new PDF document:
java
CPDFDocument document = CPDFDocument.createDocument(context);
kotlin
val document = CPDFDocument.createDocument(context)
By default, a newly created document doesn't contain any pages. Please refer to the "Document Editing" functionality to learn how to create new pages and add existing pages to the document.
Explanation of Open Document Status
This is the explanation of opening document status:
Status Code | Description |
---|---|
PDFDocumentErrorSuccess | Document opened successfully. |
PDFDocumentErrorUnknown | Unknown error. |
PDFDocumentErrorFile | File not found or cannot be opened. |
PDFDocumentErrorFormat | Format Error: not a PDF or corrupted. |
PDFDocumentErrorPassword | Password required or incorrect password. |
PDFDocumentErrorSecurity | Unsupported security scheme. |
PDFDocumentErrorPage | Page not found or content error. |
PDFDocumentNotVerifyLicense | License does not permit this permission. |
PDFDocumentErrorNoReadPermission | No read permission. |