FlattenTest.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using ComPDFKit.PDFDocument;
  2. using ComPDFKit.PDFPage;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace FlattenTest
  10. {
  11. internal class FlattenTest
  12. {
  13. static private string outputPath = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory()))) + "\\Output\\Flatten";
  14. static void Main(string[] args)
  15. {
  16. #region Perparation work
  17. Console.WriteLine("Running Flatten test sample…\r\n");
  18. SDKLicenseHelper.LicenseVerify();
  19. if (!Directory.Exists(outputPath))
  20. {
  21. Directory.CreateDirectory(outputPath);
  22. }
  23. #endregion
  24. #region Sample1: Flatten
  25. CPDFDocument document = CPDFDocument.InitWithFilePath("Annotations.pdf");
  26. if (Flatten(document))
  27. {
  28. Console.WriteLine("Flatten done.");
  29. }
  30. else
  31. {
  32. Console.WriteLine("Flatten failed.");
  33. }
  34. Console.WriteLine("--------------------");
  35. document.Release();
  36. #endregion
  37. Console.WriteLine("Done");
  38. Console.WriteLine("--------------------");
  39. Console.ReadLine();
  40. }
  41. /// <summary>
  42. /// Flatten documentation with comments
  43. /// </summary>
  44. /// <param name="document">document with many annotation. </param>
  45. /// <returns></returns>
  46. static private bool Flatten(CPDFDocument document)
  47. {
  48. int annotationCount = 0;
  49. for (int i = 0; i < document.PageCount; i++)
  50. {
  51. CPDFPage page = document.PageAtIndex(i);
  52. annotationCount += page.GetAnnotCount();
  53. }
  54. Console.Write("{0} annotations in the file. ", annotationCount);
  55. string flattenPath = outputPath + "\\FlattenTest.pdf";
  56. if (!document.WriteFlattenToFilePath(flattenPath))
  57. {
  58. return false;
  59. }
  60. Console.WriteLine("Browse the changed file in " + flattenPath);
  61. //Verify: Check if the number of comments in the new document is 0
  62. CPDFDocument flattenDocument = CPDFDocument.InitWithFilePath(flattenPath);
  63. int newCount = 0;
  64. for (int i = 0; i < flattenDocument.PageCount; i++)
  65. {
  66. CPDFPage page = flattenDocument.PageAtIndex(i);
  67. newCount += page.GetAnnotCount();
  68. }
  69. Console.WriteLine("{0} annotations in the new file. ", newCount);
  70. if (!(newCount == 0))
  71. {
  72. return false;
  73. }
  74. return true;
  75. }
  76. }
  77. }