Skip to content
ComPDF
Guides

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:

java
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.
  ...
}
kotlin
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:

java
Uri uri = ...;
CPDFDocument document = new CPDFDocument(context);
CPDFDocument.PDFDocumentError error = document.open(uri);
if (error == CPDFDocument.PDFDocumentError.PDFDocumentErrorPassword) {
  error = document.open(uri, "password");
}
kotlin
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:

java
CPDFDocument document = CPDFDocument.createDocument(context);
kotlin
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 CodeDescription
PDFDocumentErrorSuccessDocument opened successfully.
PDFDocumentErrorUnknownUnknown error.
PDFDocumentErrorFileFile not found or cannot be opened.
PDFDocumentErrorFormatThe file is not a PDF or is corrupted.
PDFDocumentErrorPasswordPassword required or incorrect password.
PDFDocumentErrorSecurityUnsupported security scheme.
PDFDocumentErrorPagePage not found or content error.
PDFDocumentNotVerifyLicenseThe license does not permit this permission.
PDFDocumentErrorNoReadPermissionNo read permission.