BatesTest.vb 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. Imports System.IO
  2. Imports ComPDFKit.PDFDocument
  3. Module BatesTest
  4. Private parentPath = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory())))
  5. Private outputPath As String = Path.Combine(parentPath, "Output", "VB")
  6. Private IntToLocationDic As New Dictionary(Of Integer, String)() From
  7. {
  8. {0, "Top Left"},
  9. {1, "Top Middle"},
  10. {2, "Top Right"},
  11. {3, "Bottom Left"},
  12. {4, "Bottom Middle"},
  13. {5, "Bottom Right"}
  14. }
  15. Sub Main()
  16. #Region "Preparation work"
  17. Console.WriteLine("Running bates test sample..." & vbCrLf)
  18. SDKLicenseHelper.LicenseVerify()
  19. If Not Directory.Exists(outputPath) Then
  20. Directory.CreateDirectory(outputPath)
  21. End If
  22. #End Region
  23. #Region "Sample 1: Add bates"
  24. Dim document As CPDFDocument = CPDFDocument.InitWithFilePath("CommonFivePage.pdf")
  25. If AddBates(document) Then
  26. Console.WriteLine("Add bates done.")
  27. End If
  28. document.Release()
  29. Console.WriteLine("--------------------")
  30. #End Region
  31. #Region "Samples 2: Edit bates"
  32. document = CPDFDocument.InitWithFilePath("Bates.pdf")
  33. If EditBates(document) Then
  34. Console.WriteLine("Edit bates done.")
  35. End If
  36. document.Release()
  37. Console.WriteLine("--------------------")
  38. #End Region
  39. #Region "Sample 3: Clear bates"
  40. document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf")
  41. If ClearBates(document) Then
  42. Console.WriteLine("Clear bates done.")
  43. End If
  44. document.Release()
  45. Console.WriteLine("--------------------")
  46. #End Region
  47. Console.WriteLine("Done!")
  48. Console.WriteLine("--------------------")
  49. Console.ReadLine()
  50. End Sub
  51. Private Function AddBates(document As CPDFDocument) As Boolean
  52. Dim addBatesPath As String = outputPath & "\AddBates.pdf"
  53. Dim bates As CPDFBates = document.GetBates()
  54. Dim color As Byte() = {255, 0, 0}
  55. bates.SetPages("0-" & (document.PageCount - 1)) ' Page numbering from 0
  56. For i As Integer = 0 To 5
  57. bates.SetText(i, "<<#3#5#Prefix-#-Suffix>>") ' 3 digits, starting from 5
  58. bates.SetTextColor(i, color)
  59. bates.SetFontSize(i, 14)
  60. Console.WriteLine("Text: {0}", bates.GetText(i))
  61. Console.WriteLine("Location: {0}" & vbCrLf, IntToLocationDic(i))
  62. Next
  63. bates.Update()
  64. If Not document.WriteToFilePath(addBatesPath) Then
  65. Return False
  66. End If
  67. Console.WriteLine("Browse the changed file in " & addBatesPath)
  68. Return True
  69. End Function
  70. Private Function EditBates(document As CPDFDocument) As Boolean
  71. Dim bates As CPDFBates = document.GetBates()
  72. ' Get the old Bates text from index 0
  73. If Not String.IsNullOrEmpty(bates.GetText(0)) Then
  74. Console.WriteLine("Get old Bates 0 done, text is {0}", bates.GetText(0))
  75. Else
  76. Console.WriteLine("Get Bates 0 failed, or it does not exist")
  77. Return False
  78. End If
  79. ' Edit the Bates text at index 0
  80. bates.SetText(0, "<<#3#1#ComPDFKit-#-ComPDFKit>>")
  81. ' Update the Bates text
  82. bates.Update()
  83. Console.WriteLine("Change Bates 0 done, new text is {0}", bates.GetText(0))
  84. Dim editBatesPath As String = outputPath & "\EditBatesTest.pdf"
  85. If document.WriteToFilePath(editBatesPath) Then
  86. Console.WriteLine("Browse the changed file in " & editBatesPath)
  87. Return True
  88. Else
  89. Return False
  90. End If
  91. End Function
  92. ''' <summary>
  93. ''' Clear bates.
  94. ''' </summary>
  95. ''' <param name="document">Document with bates</param>
  96. Private Function ClearBates(document As CPDFDocument) As Boolean
  97. Dim bates As CPDFBates = document.GetBates()
  98. bates.Clear()
  99. Dim clearBatesPath As String = outputPath & "\ClearBatesTest.pdf"
  100. If document.WriteToFilePath(clearBatesPath) Then
  101. Console.WriteLine("Browse the changed file in " & clearBatesPath)
  102. Return True
  103. Else
  104. Return False
  105. End If
  106. End Function
  107. End Module