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:

C#
// Initialize a `CPDFDocument` object using the PDF file path.
CPDFDocument document = CPDFDocument.InitWithFilePath(myFilePath);

if(document.ErrorType != CPDFDocumentError.CPDFDocumentErrorSuccess
  && document.ErrorType != CPDFDocumentError.CPDFDocumentPasswordError)
{
    return;
}

// 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:

C#
CPDFDocument document = CPDFDocument.CreateDocument();

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:

Error CodeDescription
CPDFDocumentErrorSuccessDocument opened successfully.
CPDFDocumentUnknownErrorUnknown error.
CPDFDocumentFileErrorFile not found or cannot be opened.
CPDFDocumentFormatErrorFormat Error: not a PDF or corrupted.
CPDFDocumentPasswordErrorPassword required or incorrect password.
CPDFDocumentSecurityErrorUnsupported security scheme.
CPDFDocumentPageErrorPage not found or content error.