FlattenTest.cs 2.7 KB

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