Skip to content
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:

  1. Obtain the local file path.
  2. 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 CodeDescription
PDFDocumentErrorSuccessDocument opened successfully.
PDFDocumentErrorUnknownUnknown error.
PDFDocumentErrorFileFile not found or cannot be opened.
PDFDocumentErrorFormatFormat Error: not a PDF or corrupted.
PDFDocumentErrorPasswordPassword required or incorrect password.
PDFDocumentErrorSecurityUnsupported security scheme.
PDFDocumentErrorPagePage not found or content error.
PDFDocumentNotVerifyLicenseLicense does not permit this permission.
PDFDocumentErrorNoReadPermissionNo read permission.