Open a Document
ComPDF supports opening PDF documents from a local path or Uri, and creating new documents.
Open a PDF Document from a Local Path
If the absolute path of a local file is available, use open(String) to open the document. For encrypted documents, pass a password.
The following example shows how to open a local PDF document:
CPDFDocument document = new CPDFDocument(context);
// Open the document.
CPDFDocument.PDFDocumentError error = document.open(pdfPath);
if (error == CPDFDocument.PDFDocumentError.PDFDocumentErrorPassword) {
// Open an encrypted document with a password.
error = document.open(pdfPath, "password");
}
if (error == CPDFDocument.PDFDocumentError.PDFDocumentErrorSuccess) {
// The document is opened successfully and can be parsed and manipulated.
} else {
// Unable to open the PDF file. See the API documentation for the specific error message.
}
// Check whether the document was repaired during the opening process.
if (document.hasRepaired()) {
// A repaired document must be saved before reopening. Otherwise, edits or modifications cannot be saved.
// Save the document.
...
}val document = CPDFDocument(context)
val error = document.open(pdfPath)
// Open the document.
when (error) {
PDFDocumentError.PDFDocumentErrorPassword -> {
// Open an encrypted document with a password.
error = document.open(pdfPath, "password")
}
PDFDocumentError.PDFDocumentErrorSuccess -> {
// The document is opened successfully and can be parsed and manipulated.
}
else -> {
// Unable to open the PDF file. See the API documentation for the specific error message.
}
}
// Check whether the document was repaired during the opening process.
if (document.hasRepaired()) {
// A repaired document must be saved before reopening. Otherwise, edits or modifications cannot be saved.
// Save the document.
...
}Open a PDF Document from a Uri
When a document comes from the system file picker, SAF, Google Drive, or OneDrive, use open(Uri) or open(Uri, String).
The SDK selects path-based or stream-based opening based on Uri permissions, whether the absolute path can be resolved, and whether the file is virtual.
The following example shows how to open a document from a Uri:
Uri uri = ...;
CPDFDocument document = new CPDFDocument(context);
CPDFDocument.PDFDocumentError error = document.open(uri);
if (error == CPDFDocument.PDFDocumentError.PDFDocumentErrorPassword) {
error = document.open(uri, "password");
}val uri: Uri = ...
val document = CPDFDocument(context)
var error = document.open(uri)
if (error == PDFDocumentError.PDFDocumentErrorPassword) {
error = document.open(uri, "password")
}If the document is writable, the SDK preserves write access. If the document is opened as a stream, saving must follow the stream-based workflow.
Create a New PDF Document
The following example shows how to create a new PDF document:
CPDFDocument document = CPDFDocument.createDocument(context);val document = CPDFDocument.createDocument(context)By default, a newly created document does not contain any pages. Refer to "Document Editing" to learn how to create new pages and add existing pages to the document.
Explanation of Open Document Status
The following table describes the status codes returned when opening a document:
| Status Code | Description |
|---|---|
| PDFDocumentErrorSuccess | Document opened successfully. |
| PDFDocumentErrorUnknown | Unknown error. |
| PDFDocumentErrorFile | File not found or cannot be opened. |
| PDFDocumentErrorFormat | The file is not a PDF or is corrupted. |
| PDFDocumentErrorPassword | Password required or incorrect password. |
| PDFDocumentErrorSecurity | Unsupported security scheme. |
| PDFDocumentErrorPage | Page not found or content error. |
| PDFDocumentNotVerifyLicense | The license does not permit this permission. |
| PDFDocumentErrorNoReadPermission | No read permission. |