PDFPageTest.vb 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. Imports ComPDFKit.PDFDocument
  2. Imports System
  3. Imports System.Collections
  4. Imports System.Collections.Generic
  5. Imports System.IO
  6. Module PDFPageTest
  7. Private outputPath As String = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory()))) & "\Output\VB"
  8. Sub Main()
  9. ' Preparation work
  10. Console.WriteLine("Running PDFPage test sample…" & vbCrLf)
  11. SDKLicenseHelper.LicenseVerify()
  12. Dim document As CPDFDocument = CPDFDocument.InitWithFilePath("CommonFivePage.pdf")
  13. If Not Directory.Exists(outputPath) Then
  14. Directory.CreateDirectory(outputPath)
  15. End If
  16. ' Sample 1: Insert blank page
  17. If InsertBlankPage(document) Then
  18. Console.WriteLine("Insert blank page done.")
  19. Else
  20. Console.WriteLine("Insert blank page failed.")
  21. End If
  22. document.Release()
  23. Console.WriteLine("--------------------")
  24. ' Sample 2: Insert PDF page
  25. document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf")
  26. If InsertPDFPPage(document) Then
  27. Console.WriteLine("Insert PDF page done.")
  28. Else
  29. Console.WriteLine("Insert PDF page failed.")
  30. End If
  31. document.Release()
  32. Console.WriteLine("--------------------")
  33. ' Sample 3: Split pages
  34. document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf")
  35. If SplitPages(document) Then
  36. Console.WriteLine("Split page done.")
  37. Else
  38. Console.WriteLine("Split failed.")
  39. End If
  40. document.Release()
  41. Console.WriteLine("--------------------")
  42. ' Sample 4: Remove pages
  43. document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf")
  44. If RemovePages(document) Then
  45. Console.WriteLine("Delete even page done.")
  46. Else
  47. Console.WriteLine("Delete even page failed.")
  48. End If
  49. document.Release()
  50. Console.WriteLine("--------------------")
  51. ' Sample 5: Rotate page
  52. document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf")
  53. If RotatePage(document) Then
  54. Console.WriteLine("Rotate page done.")
  55. Else
  56. Console.WriteLine("Rotate page failed.")
  57. End If
  58. document.Release()
  59. Console.WriteLine("--------------------")
  60. ' Sample 6: Replace pages
  61. document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf")
  62. If RepalcePages(document) Then
  63. Console.WriteLine("Repalce page done.")
  64. Else
  65. Console.WriteLine("Repalce page failed.")
  66. End If
  67. document.Release()
  68. Console.WriteLine("--------------------")
  69. ' Sample 7: Extract pages
  70. document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf")
  71. If ExtractPages(document) Then
  72. Console.WriteLine("Extract page done.")
  73. Else
  74. Console.WriteLine("Extract page failed.")
  75. End If
  76. document.Release()
  77. Console.WriteLine("--------------------")
  78. Console.WriteLine("Done")
  79. Console.WriteLine("--------------------")
  80. Console.ReadLine()
  81. End Sub
  82. ' Insert a new page of A4 size at the second page
  83. Private Function InsertBlankPage(document As CPDFDocument) As Boolean
  84. Dim pageIndex As Integer = 1
  85. Dim pageWidth As Integer = 595
  86. Dim pageHeight As Integer = 842
  87. document.InsertPage(pageIndex, pageWidth, pageHeight, "")
  88. Console.WriteLine("Insert PageIndex: {0}", pageIndex)
  89. Console.WriteLine("Size: {0}*{1}", pageWidth, pageHeight)
  90. Dim path As String = outputPath & "\InsertBlankPageTest.pdf"
  91. If Not document.WriteToFilePath(path) Then
  92. Return False
  93. End If
  94. Console.WriteLine("Browse the changed file in " & path)
  95. Return True
  96. End Function
  97. ' Select pages from other PDF files and insert them into the current document
  98. Private Function InsertPDFPPage(document As CPDFDocument) As Boolean
  99. Dim documentForInsert As CPDFDocument = CPDFDocument.InitWithFilePath("Text.pdf")
  100. document.ImportPagesAtIndex(documentForInsert, "1", 1)
  101. Dim path As String = outputPath & "\InsertPDFPPageTest.pdf"
  102. If Not document.WriteToFilePath(path) Then
  103. Return False
  104. End If
  105. Console.WriteLine("Browse the changed file in " & path)
  106. Return True
  107. End Function
  108. ' Split the current document into two documents according to the first 2 pages and the last 3 pages
  109. Private Function SplitPages(document As CPDFDocument) As Boolean
  110. ' Split 1-2 pages
  111. Dim documentPart1 As CPDFDocument = CPDFDocument.CreateDocument()
  112. documentPart1.ImportPagesAtIndex(document, "1-2", 0)
  113. Dim pathPart1 As String = outputPath & "\SplitPart1Test.pdf"
  114. If Not documentPart1.WriteToFilePath(pathPart1) Then
  115. Return False
  116. End If
  117. Console.WriteLine("Browse the changed file in " & pathPart1)
  118. ' Split 3-5 pages
  119. Dim documentPart2 As CPDFDocument = CPDFDocument.CreateDocument()
  120. documentPart2.ImportPagesAtIndex(document, "3-5", 0)
  121. Dim pathPart2 As String = outputPath & "\SplitPart2Test.pdf"
  122. If Not documentPart2.WriteToFilePath(pathPart2) Then
  123. Return False
  124. End If
  125. Console.WriteLine("Browse the changed file in " & pathPart2)
  126. Return True
  127. End Function
  128. ' Remove even-numbered pages from a document
  129. Private Function RemovePages(document As CPDFDocument) As Boolean
  130. Dim arr As New ArrayList()
  131. For i As Integer = 1 To document.PageCount - 1 Step 2
  132. arr.Add(i)
  133. Next
  134. document.RemovePages(DirectCast(arr.ToArray(GetType(Integer)), Integer()))
  135. Dim path As String = outputPath & "\RemoveEvenPagesTest.pdf"
  136. If Not document.WriteToFilePath(path) Then
  137. Return False
  138. End If
  139. Console.WriteLine("Browse the changed file in " & path)
  140. Return True
  141. End Function
  142. ' Rotate the first page 90 degrees clockwise
  143. Private Function RotatePage(document As CPDFDocument) As Boolean
  144. document.RotatePage(0, 1) ' Rotation: Rotate 90 degrees per unit
  145. Dim path As String = outputPath & "\RotatePageTest.pdf"
  146. If Not document.WriteToFilePath(path) Then
  147. Return False
  148. End If
  149. Console.WriteLine("Browse the changed file in " & path)
  150. Return True
  151. End Function
  152. ' Replace the first page of the current document with a page from another document
  153. ' Delete the pages that need to be replaced first
  154. ' Insert the required pages into the document
  155. Private Function RepalcePages(document As CPDFDocument) As Boolean
  156. Dim pageArr(0) As Integer
  157. pageArr(0) = 0
  158. document.RemovePages(pageArr)
  159. Dim documentForInsert As CPDFDocument = CPDFDocument.InitWithFilePath("Text.pdf")
  160. document.ImportPagesAtIndex(documentForInsert, "1", 0)
  161. Dim path As String = outputPath & "\RepalcePagesTest.pdf"
  162. If Not document.WriteToFilePath(path) Then
  163. Return False
  164. End If
  165. Console.WriteLine("Browse the changed file in " & path)
  166. Return True
  167. End Function
  168. ' Extract pages from a document
  169. ' Create a new document
  170. ' Insert the required pages into a new document
  171. Private Function ExtractPages(document As CPDFDocument) As Boolean
  172. Dim extractDocument As CPDFDocument = CPDFDocument.CreateDocument()
  173. extractDocument.ImportPagesAtIndex(document, "1", 0)
  174. Dim path As String = outputPath & "\ExtractPagesTest.pdf"
  175. If Not extractDocument.WriteToFilePath(path) Then
  176. Return False
  177. End If
  178. Console.WriteLine("Browse the changed file in " & path)
  179. Return True
  180. End Function
  181. End Module