Skip to content
On this page

Guides

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 Windows apps using our C# APIs.

Basic Operations

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

Open a Document

  • Open a Local File

    C#
    // Get the path of a PDF
    string filePath ="";
    var dlg = new OpenFileDialog();
    dlg.Filter = "PDF Files (*.pdf)|*.pdf";
    if (dlg.ShowDialog() == true)
    {
        filePath = dlg.FileName;
    }
    else
    {
        return;
    }
    
    // Initialize a CPDFDocument object with the path to the PDF file
    CPDFDocument document = CPDFDocument.InitWithFilePath(filePath);
    if(document==null)
    {
        return;
    }
    
    if(document.ErrorType != CPDFDocumentError.CPDFDocumentErrorSuccess
      && document.ErrorType != CPDFDocumentError.CPDFDocumentPasswordError)
    {
        return;
    }
  • Create a New File

    c#
    CPDFDocument document = CPDFDocument.CreateDocument();

Following are the descriptions of error codes in CPDFDocumentError:

ErrorDescription
CPDFDocumentErrorSuccessOpen PDF file successfully.
CPDFDocumentUnknownErrorUnknown error.
CPDFDocumentFileErrorFile not found or could not be opened.
CPDFDocumentFormatErrorFile not in PDF format or corrupted.
CPDFDocumentPasswordErrorPassword required or incorrect password.
CPDFDocumentSecurityErrorSecurity scheme not supported.
CPDFDocumentPageErrorPage not found or content error.

Save a Document

To save a PDF document to path use CPDFDocument.WriteToLoadedPath(), the PDF document will be saved incrementally. Use this method if you are concerned about saving time. If you use this method, 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 file size increase is negligible. However, there might be some cases in which you want to prioritize file size over saving performance. Below you’ll find a strategy to prevent the file size from growing unnecessarily when saving changes to a document.

To save as a PDF document to path use CPDFDocument.WriteToFilePath(string filePath), the PDF document will be saved non-incremental. If you use this method, will rewrite the entire document instead of appending changes at the end.