FlattenTest.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. Console.WriteLine("Running Flatten test sample…\r\n");
  17. SDKLicenseHelper.LicenseVerify();
  18. CPDFDocument document = CPDFDocument.InitWithFilePath("Annotations.pdf");
  19. if (!Directory.Exists(outputPath))
  20. {
  21. Directory.CreateDirectory(outputPath);
  22. }
  23. if (Flatten(document))
  24. {
  25. Console.WriteLine("Flatten done.");
  26. }
  27. else
  28. {
  29. Console.WriteLine("Flatten failed.");
  30. }
  31. Console.WriteLine("--------------------");
  32. Console.WriteLine("Done");
  33. Console.WriteLine("--------------------");
  34. Console.ReadLine();
  35. }
  36. static private bool Flatten(CPDFDocument document)
  37. {
  38. int annotationCount = 0;
  39. for (int i = 0; i < document.PageCount; i++)
  40. {
  41. CPDFPage page = document.PageAtIndex(i);
  42. annotationCount += page.GetAnnotCount();
  43. }
  44. Console.Write("{0} annotations in the file. ", annotationCount);
  45. string flattenPath = outputPath + "\\FlattenTest.pdf";
  46. if (document.WriteFlattenToFilePath(flattenPath))
  47. {
  48. Console.WriteLine("Browse the changed file in " + flattenPath);
  49. }
  50. else
  51. {
  52. return false;
  53. }
  54. CPDFDocument flattenDocument = CPDFDocument.InitWithFilePath(flattenPath);
  55. int newCount = 0;
  56. for (int i = 0; i < flattenDocument.PageCount; i++)
  57. {
  58. CPDFPage page = flattenDocument.PageAtIndex(i);
  59. newCount += page.GetAnnotCount();
  60. }
  61. Console.WriteLine("{0} annotations in the new file. ", newCount);
  62. if (newCount == 0)
  63. {
  64. return true;
  65. }
  66. else
  67. {
  68. return false;
  69. }
  70. }
  71. }
  72. }