OutlineTest.vb 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. Imports ComPDFKit.PDFDocument
  2. Imports System
  3. Imports System.Collections.Generic
  4. Imports System.IO
  5. Module OutlineTest
  6. Private outputPath As String = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory()))) & "\Output\VB"
  7. Private outlineCounter As Integer = 0
  8. Private outlineNumber As Integer = 0
  9. Sub Main()
  10. ' Perparation work
  11. Console.WriteLine("Running Outline test sample…" & vbCrLf)
  12. SDKLicenseHelper.LicenseVerify()
  13. If Not Directory.Exists(outputPath) Then
  14. Directory.CreateDirectory(outputPath)
  15. End If
  16. ' Sample 1: Print outline
  17. Dim document As CPDFDocument = CPDFDocument.InitWithFilePath("FourOutline.pdf")
  18. If PrintOutline(document) Then
  19. Console.WriteLine("Print outline done.")
  20. Else
  21. Console.WriteLine("Print outline failed.")
  22. End If
  23. document.Release()
  24. Console.WriteLine("--------------------")
  25. ' Sample 2: Create outline
  26. document = CPDFDocument.InitWithFilePath("FourOutline.pdf")
  27. If CreateOutline(document) Then
  28. Console.WriteLine("Create outline done.")
  29. Else
  30. Console.WriteLine("Create outline failed.")
  31. End If
  32. document.Release()
  33. Console.WriteLine("--------------------")
  34. ' Sample 3: Move outline
  35. document = CPDFDocument.InitWithFilePath("FourOutline.pdf")
  36. If MoveOutline(document) Then
  37. Console.WriteLine("Move outline done.")
  38. Else
  39. Console.WriteLine("Move outline failed.")
  40. End If
  41. document.Release()
  42. Console.WriteLine("--------------------")
  43. ' Sample 4: Remove outline
  44. document = CPDFDocument.InitWithFilePath("FourOutline.pdf")
  45. If RemoveOutline(document) Then
  46. Console.WriteLine("Remove outline done.")
  47. Else
  48. Console.WriteLine("Remove outline failed.")
  49. End If
  50. document.Release()
  51. Console.WriteLine("--------------------")
  52. Console.WriteLine("Done.")
  53. Console.WriteLine("--------------------")
  54. Console.ReadLine()
  55. End Sub
  56. ' Traverse outline and print it as a list
  57. Private Sub TraverseOutline(outlineList As List(Of CPDFOutline))
  58. For Each outline In outlineList
  59. For i As Integer = 0 To outlineCounter - 1
  60. Console.Write(" ")
  61. Next
  62. Console.Write("-> " & outline.Title & vbCrLf)
  63. outlineNumber += 1
  64. Dim childList = outline.ChildList
  65. If childList IsNot Nothing AndAlso childList.Count <> 0 Then
  66. outlineCounter += 1
  67. TraverseOutline(childList)
  68. Else
  69. Dim i = outlineList.IndexOf(outline)
  70. If outlineList.IndexOf(outline) + 1 = outlineList.Count Then
  71. outlineCounter -= 1
  72. End If
  73. End If
  74. Next
  75. End Sub
  76. ' Print all outlines in the file
  77. Private Function PrintOutline(document As CPDFDocument) As Boolean
  78. Dim outlineList As List(Of CPDFOutline) = document.GetOutlineList()
  79. outlineNumber = 0
  80. outlineCounter = 0
  81. TraverseOutline(outlineList)
  82. Console.WriteLine("Outline number: {0}", outlineNumber)
  83. Return True
  84. End Function
  85. ' Create an outline at the top of the first page
  86. Private Function CreateOutline(document As CPDFDocument) As Boolean
  87. Dim outline As CPDFOutline = document.GetOutlineRoot()
  88. Dim childOutline As CPDFOutline = Nothing
  89. outline.InsertChildAtIndex(document, 0, childOutline)
  90. childOutline.SetTitle("New outline")
  91. PrintOutline(document)
  92. Return True
  93. End Function
  94. ' Move outline
  95. Private Function MoveOutline(document As CPDFDocument) As Boolean
  96. Dim outline As CPDFOutline = document.GetOutlineRoot()
  97. outline.InsertChildAtIndex(document, outline.ChildList.Count, outline)
  98. outline.SetTitle("new outline")
  99. Dim targetOutline As CPDFOutline = document.GetOutlineList()(1)
  100. targetOutline.MoveChildAtIndex(document, outline, targetOutline.ChildList.Count)
  101. Dim path As String = outputPath & "\MoveOutlineTest.pdf"
  102. If Not document.WriteToFilePath(path) Then
  103. Return False
  104. End If
  105. Dim newDocument As CPDFDocument = CPDFDocument.InitWithFilePath(path)
  106. PrintOutline(newDocument)
  107. Return True
  108. End Function
  109. ' Remove outline
  110. Private Function RemoveOutline(document As CPDFDocument) As Boolean
  111. document.GetOutlineList()(0).RemoveFromParent(document)
  112. Dim path As String = outputPath & "\RemoveOutlineTest.pdf"
  113. If Not document.WriteToFilePath(path) Then
  114. Return False
  115. End If
  116. Dim newDocument As CPDFDocument = CPDFDocument.InitWithFilePath(path)
  117. PrintOutline(newDocument)
  118. Return True
  119. End Function
  120. End Module