BookmarkTest.cs 5.0 KB

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