AnnotationImportExportTest.cs 3.8 KB

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