Skip to content
Guides

Import & Export Annotations in PDF

This sample shows how to set up the export and import of annotations. The document from which the annotations are exported is an xfdf file

C#
using ComPDFKit.PDFDocument;
using System;
using System.IO; 

namespace AnnotationImportExportTest
{
    internal class AnnotationImportExportTest
    {
        private static string outputPath = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory()))) + "\\Output\\AnnotationImportExportTest";
        private static string tempPath = outputPath + "\\temp";

        static void Main(string[] args)
        {

            Console.WriteLine("Running header and footer test sample…\r\n");

            #region Preparation work

            SDKLicenseHelper.LicenseVerify();
             
            if (!Directory.Exists(outputPath))
            {
                Directory.CreateDirectory(outputPath);
            }
            if (!Directory.Exists(tempPath))
            {
                Directory.CreateDirectory(tempPath);
            }

            #endregion

            #region Sample 1: Export Annotation 

            CPDFDocument annotationsDocument = CPDFDocument.InitWithFilePath("Annotations.pdf"); 
            if (ExportAnnotaiton(annotationsDocument))
            {
                Console.WriteLine("Export annotaiton done.");
            }
            else
            {
                Console.WriteLine("Export annotaiton failed.");
            } 
            Console.WriteLine("--------------------");
            
            #endregion

            #region Sample 2: Import Annotations

            CPDFDocument document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf"); 
            if (ImportAnnotaiton(document))
            {
                Console.WriteLine("Import annotaiton done.");
            }
            else
            {
                Console.WriteLine("Import annotaiton failed.");
            }
            Console.WriteLine("--------------------");
            
            #endregion
             
            Console.WriteLine("Done");
            Console.WriteLine("--------------------");
            Console.ReadLine();
        }

        /// <summary>
        ///  Export the annotations in the document to XFDF format.
        ///  Parameters in function "ImportAnnotationFromXFDFPath":
        ///  1. path: The path to the exported XFDF file
        ///  2. tempPath: The path for storing temporary files. 
        /// </summary>
        /// <param name="document">A document with multiple annotations</param>
        /// <returns></returns>
        static private bool ExportAnnotaiton(CPDFDocument document)
        {
            var path = outputPath + "\\ExportAnnotationTest.xfdf";
            if (!document.ExportAnnotationToXFDFPath(path, tempPath))
            {
                return false;
            }
            Console.WriteLine("Xfdf file in " + path);

            return true;
        }

        /// <summary>
        ///  Importing XFDF into the document.
        ///  Parameters in function "ImportAnnotationFromXFDFPath":
        ///  1. The path to the XFDF file for importing.
        ///  2. The path for storing temporary files.  
        /// </summary>
        /// <param name="document">A document without annotations used for importing XFDF.</param>
        /// <returns></returns>
        static private bool ImportAnnotaiton(CPDFDocument document)
        {
            var path = outputPath + "//ImportAnnotationTest.pdf";

            if (!document.ImportAnnotationFromXFDFPath("Annotations.xfdf", tempPath))
            {
                return false;
            }
            if (!document.WriteToFilePath(path))
            {
                return false;
            }
            Console.WriteLine("Browse the changed file in " + path); 
            return true;
        }
    }
}