Skip to content
On this page


If you’re interested in all of the features mentioned in Overview section, please go through our guides to quickly add PDF viewing, annotating, and editing to your application. The following sections list some examples to show you how to add document functionalities to iOS apps using our Swift and Objective-C APIs.

Basic Operations

There are a few commonly used basic operations when working with documents.

Open a Document

  • Open a Local File

    // Get the path of a PDF
    NSString *pdfPath = @"...";
    // Initialize a CPDFDocument object with the path to the PDF file
    NSURL *url = [NSURL fileURLWithPath:pdfPath];
    CPDFDocument *document = [[CPDFDocument alloc] initWithURL:url];
    if (document.error && document.error.code != CPDFDocumentPasswordError) {
  • Create a New File

    CPDFDocument *document = [[CPDFDocument alloc] init];

Following are the descriptions of error codes in CPDFDocumentErrorDomain:

CPDFDocumentUnknownErrorUnknown error.
CPDFDocumentFileErrorFile not found or could not be opened.
CPDFDocumentFormatErrorFile not in PDF format or corrupted.
CPDFDocumentPasswordErrorPassword required or incorrect password.
CPDFDocumentSecurityErrorUnsupported security scheme. / Security scheme not supported.
CPDFDocumentPageErrorPage not found or content error.

Note: This is a time-consuming process, so it needs to be executed in a sub-thread. After the document is successfully opened, the UI that renders the PDF is initialized.

Save a Document

To save a PDF document to path use CPDFDocument::writeToURL:.

If this path is the original path, the PDF document will be saved incrementally. Use the incremental mode if you are concerned about saving time. If you use this mode, any changes to the document, even deleting annotations, will result in appending to the PDF file.

Incremental saving, which is enabled by default, is the strategy that ComPDFKit uses to ensure saving is as fast as possible. This strategy always appends changes to the end of the document, and it never deletes anything. However, even though this is the fastest way to save a document, it does come with the cost of increasing the file size with each save. You can read more about incremental saving and full saving of PDFs here.

In most cases, the increase in file size is negligible. However, in some cases, you may want to prioritize file size over saving performance. Below you'll find some strategies to prevent file size from growing unnecessarily when saving changes to a document.

If the document is saved in a new path (Not the original storage path), there is going to trigger a non-incremental document save, which will rewrite the entire document instead of appending changes at the end.