BookmarkTest.cs 4.8 KB

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