BookmarkTest.vb 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. Imports ComPDFKit.PDFDocument
  2. Imports System
  3. Imports System.IO
  4. Module BookmarkTest
  5. Private parentPath = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory())))
  6. Private outputPath As String = Path.Combine(parentPath, "Output", "VB")
  7. Sub Main()
  8. ' Preparation work
  9. Console.WriteLine("Running Bookmark test sample…" + Environment.NewLine)
  10. SDKLicenseHelper.LicenseVerify()
  11. If Not Directory.Exists(outputPath) Then
  12. Directory.CreateDirectory(outputPath)
  13. End If
  14. Dim document As CPDFDocument = CPDFDocument.InitWithFilePath("ThreeBookmark.pdf")
  15. #Region "Sample 1: Access bookmark"
  16. If AccessBookmark(document) Then
  17. Console.WriteLine("Check bookmark list done.")
  18. Else
  19. Console.WriteLine("Check bookmark list failed.")
  20. Console.WriteLine("--------------------")
  21. Console.WriteLine("Stop.")
  22. Console.WriteLine("--------------------")
  23. Console.ReadLine()
  24. Return
  25. End If
  26. document.Release()
  27. Console.WriteLine("--------------------")
  28. #End Region
  29. #Region "Sample 2: Create bookmark"
  30. document = CPDFDocument.InitWithFilePath("ThreeBookmark.pdf")
  31. If CreateBookmark(document) Then
  32. Console.WriteLine("Add bookmark done.")
  33. Else
  34. Console.WriteLine("Add bookmark failed.")
  35. End If
  36. document.Release()
  37. Console.WriteLine("--------------------")
  38. #End Region
  39. #Region "Sample 3: Remove bookmark"
  40. document = CPDFDocument.InitWithFilePath("ThreeBookmark.pdf")
  41. If RemoveBookmark(document) Then
  42. Console.WriteLine("Remove bookmark done.")
  43. Else
  44. Console.WriteLine("Remove bookmark failed.")
  45. End If
  46. document.Release()
  47. Console.WriteLine("--------------------")
  48. #End Region
  49. Console.WriteLine("Done!")
  50. Console.WriteLine("--------------------")
  51. Console.ReadLine()
  52. End Sub
  53. ''' <summary>
  54. ''' Access bookmark
  55. ''' </summary>
  56. Private Function AccessBookmark(document As CPDFDocument) As Boolean
  57. Dim bookmarkList As List(Of CPDFBookmark) = document.GetBookmarkList()
  58. ' Check if there are 3 bookmarks in the document
  59. If bookmarkList.Count = 3 Then
  60. Console.WriteLine("Access bookmark list done.")
  61. Else
  62. Return False
  63. End If
  64. ' Check the title of the bookmark for page index 0
  65. If document.BookmarkForPageIndex(0).Title = "Bookmark1" Then
  66. Console.WriteLine("Access bookmark for a page done.")
  67. Else
  68. Return False
  69. End If
  70. Return True
  71. End Function
  72. ''' <summary>
  73. ''' Create bookmark
  74. ''' </summary>
  75. Private Function CreateBookmark(document As CPDFDocument) As Boolean
  76. Dim bookmarkCount As Integer = document.GetBookmarkList().Count
  77. Dim bookmark As New CPDFBookmark()
  78. bookmark.Title = "new bookmark"
  79. bookmark.PageIndex = 4
  80. ' Add the new bookmark to the document
  81. document.AddBookmark(bookmark)
  82. ' Check if the number of bookmarks has increased by 1
  83. If document.GetBookmarkList().Count - bookmarkCount = 1 Then
  84. Console.WriteLine("Add bookmark in page {0}. ", bookmark.PageIndex + 1)
  85. Else
  86. Return False
  87. End If
  88. ' Save the modified document
  89. Dim addBookmarkPath As String = outputPath & "//AddBookmarkTest.pdf"
  90. If document.WriteToFilePath(addBookmarkPath) Then
  91. Console.WriteLine("Browse the changed file in " & addBookmarkPath)
  92. Return True
  93. Else
  94. Return False
  95. End If
  96. End Function
  97. ''' <summary>
  98. ''' Remove bookmark
  99. ''' </summary>
  100. Private Function RemoveBookmark(document As CPDFDocument) As Boolean
  101. Dim bookmarkCount As Integer = document.GetBookmarkList().Count
  102. ' Remove the first bookmark
  103. document.RemoveBookmark(0)
  104. ' Check if the number of bookmarks has decreased by 1
  105. If bookmarkCount - document.GetBookmarkList().Count = 1 Then
  106. Console.WriteLine("Bookmark removed.")
  107. Else
  108. Return False
  109. End If
  110. ' Save the modified document
  111. Dim removeBookmarkPath As String = outputPath & "//RemoveBookmarkTest.pdf"
  112. If document.WriteToFilePath(removeBookmarkPath) Then
  113. Console.WriteLine("Browse the changed file in " & removeBookmarkPath)
  114. Return True
  115. Else
  116. Return False
  117. End If
  118. End Function
  119. End Module