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:
swift
let url = URL(fileURLWithPath: "File Path")
// Initialize a `CPDFDocument` object using the PDF file path.
let document = CPDFDocument(url: url)
if let error = document?.error, error._code != CPDFDocumentPasswordError {
}
// For encrypted documents, it is necessary to use a password to decrypt them.
if document?.isLocked == true {
document?.unlock(withPassword: "password")
}
objective-c
NSURL *url = [NSURL fileURLWithPath:@""];
// Initialize a `CPDFDocument` object using the PDF file path.
CPDFDocument *document = [[CPDFDocument alloc] initWithURL:url];
if (document.error &&
document.error.code == CPDFDocumentPasswordError) {
}
// For encrypted documents, it is necessary to use a password to decrypt them.
if (document.isLocked) {
[document unlockWithPassword:@"password"];
}
Create a New PDF Document
This example shows how to create a new PDF document:
swift
let url = URL(fileURLWithPath: "File Path")
let document = CPDFDocument(url: url)
objective-c
NSURL *url = [NSURL fileURLWithPath:@"File Path"];
CPDFDocument *document = [[CPDFDocument alloc] initWithURL:url];
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 |
---|---|
CPDFDocumentUnknownError | Unknown error. |
CPDFDocumentFileError | File not found or cannot be opened. |
CPDFDocumentFormatError | Format Error: not a PDF or corrupted. |
CPDFDocumentPasswordError | Password required or incorrect password. |
CPDFDocumentSecurityError | Unsupported security scheme. |
CPDFDocumentPageError | Page not found or content error. |