BookmarkTest.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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.Net.NetworkInformation;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace BookmarkTest
  11. {
  12. internal class BookmarkTest
  13. {
  14. static private string outputPath = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory()))) + "\\Output\\Bookmark";
  15. static void Main(string[] args)
  16. {
  17. Console.WriteLine("Running Bookmark test sample…\r\n");
  18. SDKLicenseHelper.LicenseVerify();
  19. CPDFDocument document = CPDFDocument.InitWithFilePath("ThreeBookmark.pdf");
  20. if (!Directory.Exists(outputPath))
  21. {
  22. Directory.CreateDirectory(outputPath);
  23. }
  24. if (AccessBookmark(document))
  25. {
  26. Console.WriteLine("Check bookmark list done.");
  27. }
  28. else
  29. {
  30. Console.WriteLine("Check bookmark list failed.");
  31. Console.WriteLine("--------------------");
  32. Console.WriteLine("Stop.");
  33. Console.WriteLine("--------------------");
  34. Console.ReadLine();
  35. return;
  36. }
  37. Console.WriteLine("--------------------");
  38. document.Release();
  39. document = CPDFDocument.InitWithFilePath("ThreeBookmark.pdf");
  40. if (CreateBookmark(document))
  41. {
  42. Console.WriteLine("Add bookmark done.");
  43. }
  44. else
  45. {
  46. Console.WriteLine("Add bookmark failed.");
  47. }
  48. Console.WriteLine("--------------------");
  49. document.Release();
  50. document = CPDFDocument.InitWithFilePath("ThreeBookmark.pdf");
  51. if (RemoveBookmark(document))
  52. {
  53. Console.WriteLine("Remove bookmark done.");
  54. }
  55. else
  56. {
  57. Console.WriteLine("Remove bookmark done.");
  58. }
  59. Console.WriteLine("--------------------");
  60. Console.WriteLine("Done!");
  61. Console.WriteLine("--------------------");
  62. Console.ReadLine();
  63. }
  64. static private bool AccessBookmark(CPDFDocument document)
  65. {
  66. List<CPDFBookmark> bookmarkList = document.GetBookmarkList();
  67. if (bookmarkList.Count == 3)
  68. {
  69. Console.WriteLine("Access bookmark list done.");
  70. }
  71. else
  72. {
  73. return false;
  74. }
  75. if(document.BookmarkForPageIndex(0).Title == "Bookmark1")
  76. {
  77. Console.WriteLine("Access bookmark for a page done.");
  78. }
  79. else
  80. {
  81. return false;
  82. }
  83. return true;
  84. }
  85. static private bool CreateBookmark(CPDFDocument document)
  86. {
  87. var bookmarkCount = document.GetBookmarkList().Count;
  88. CPDFBookmark bookmark = new CPDFBookmark();
  89. bookmark.Title = "new bookmark";
  90. bookmark.PageIndex = 4;
  91. document.AddBookmark(bookmark);
  92. if(!(document.GetBookmarkList().Count - bookmarkCount == 1))
  93. {
  94. return false;
  95. }
  96. Console.WriteLine("Add bookmark in page {0}. ", bookmark.PageIndex+1);
  97. string addBookmarkPath = outputPath + "//AddBookmarkTest.pdf";
  98. if (document.WriteToFilePath(addBookmarkPath))
  99. {
  100. Console.WriteLine("Browse the changed file in " + addBookmarkPath);
  101. return true;
  102. }
  103. else
  104. {
  105. return false;
  106. }
  107. }
  108. static private bool RemoveBookmark(CPDFDocument document)
  109. {
  110. var bookmarkCount = document.GetBookmarkList().Count;
  111. document.RemoveBookmark(0);
  112. if (!(bookmarkCount - document.GetBookmarkList().Count == 1))
  113. {
  114. return false;
  115. }
  116. string removeBookmarkPath = outputPath + "//RemoveBookmarkTest.pdf";
  117. if (document.WriteToFilePath(removeBookmarkPath))
  118. {
  119. Console.WriteLine("Browse the changed file in " + removeBookmarkPath);
  120. return true;
  121. }
  122. else
  123. {
  124. return false;
  125. }
  126. }
  127. }
  128. }