AnnotationImportExportTest.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using ComPDFKit.PDFDocument;
  2. using System;
  3. using System.IO;
  4. namespace AnnotationImportExportTest
  5. {
  6. internal class AnnotationImportExportTest
  7. {
  8. private static string outputPath = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory()))) + "\\Output\\AnnotationImportExportTest";
  9. private static string tempPath = outputPath + "\\temp";
  10. static void Main(string[] args)
  11. {
  12. Console.WriteLine("Running header and footer test sample…\r\n");
  13. #region Preparation work
  14. SDKLicenseHelper.LicenseVerify();
  15. if (!Directory.Exists(outputPath))
  16. {
  17. Directory.CreateDirectory(outputPath);
  18. }
  19. if (!Directory.Exists(tempPath))
  20. {
  21. Directory.CreateDirectory(tempPath);
  22. }
  23. #endregion
  24. #region Sample 1: Export Annotation
  25. CPDFDocument annotationsDocument = CPDFDocument.InitWithFilePath("Annotations.pdf");
  26. if (ExportAnnotaiton(annotationsDocument))
  27. {
  28. Console.WriteLine("Export annotaiton done.");
  29. }
  30. else
  31. {
  32. Console.WriteLine("Export annotaiton failed.");
  33. }
  34. Console.WriteLine("--------------------");
  35. #endregion
  36. #region Sample 2: Import Annotations
  37. CPDFDocument document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf");
  38. if (ImportAnnotaiton(document))
  39. {
  40. Console.WriteLine("Import annotaiton done.");
  41. }
  42. else
  43. {
  44. Console.WriteLine("Import annotaiton failed.");
  45. }
  46. Console.WriteLine("--------------------");
  47. #endregion
  48. Console.WriteLine("Done");
  49. Console.WriteLine("--------------------");
  50. Console.ReadLine();
  51. }
  52. /// <summary>
  53. /// Export the annotations in the document to XFDF format.
  54. /// Parameters in function "ImportAnnotationFromXFDFPath":
  55. /// 1. path: The path to the exported XFDF file
  56. /// 2. tempPath: The path for storing temporary files.
  57. /// </summary>
  58. /// <param name="document">A document with multiple annotations</param>
  59. /// <returns></returns>
  60. static private bool ExportAnnotaiton(CPDFDocument document)
  61. {
  62. var path = outputPath + "\\ExportAnnotationTest.xfdf";
  63. if (!document.ExportAnnotationToXFDFPath(path, tempPath))
  64. {
  65. return false;
  66. }
  67. Console.WriteLine("Xfdf file in " + path);
  68. return true;
  69. }
  70. /// <summary>
  71. /// Importing XFDF into the document.
  72. /// Parameters in function "ImportAnnotationFromXFDFPath":
  73. /// 1. The path to the XFDF file for importing.
  74. /// 2. The path for storing temporary files.
  75. /// </summary>
  76. /// <param name="document">A document without annotations used for importing XFDF.</param>
  77. /// <returns></returns>
  78. static private bool ImportAnnotaiton(CPDFDocument document)
  79. {
  80. var path = outputPath + "//ImportAnnotationTest.pdf";
  81. if (!document.ImportAnnotationFromXFDFPath("Annotations.xfdf", tempPath))
  82. {
  83. return false;
  84. }
  85. if (!document.WriteToFilePath(path))
  86. {
  87. return false;
  88. }
  89. Console.WriteLine("Browse the changed file in " + path);
  90. return true;
  91. }
  92. }
  93. }