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();
}
///
/// 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.
///
/// A document with multiple annotations
///
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;
}
///
/// 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.
///
/// A document without annotations used for importing XFDF.
///
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;
}
}
}