WatermarkTest.vb 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. Imports ComPDFKit.PDFDocument
  2. Imports ComPDFKit.PDFWatermark
  3. Imports System
  4. Imports System.Drawing
  5. Imports System.Drawing.Imaging
  6. Imports System.IO
  7. Imports System.Runtime.InteropServices
  8. Module WatermarkTest
  9. Private outputPath As String = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(Directory.GetCurrentDirectory()))) & "\Output\VB"
  10. Sub Main()
  11. ' Preparation work
  12. Console.WriteLine("Running Watermark test sample..." & vbCrLf)
  13. SDKLicenseHelper.LicenseVerify()
  14. Dim document As CPDFDocument = CPDFDocument.InitWithFilePath("CommonFivePage.pdf")
  15. If Not Directory.Exists(outputPath) Then
  16. Directory.CreateDirectory(outputPath)
  17. End If
  18. ' Sample 1: Add text watermark
  19. If AddTextWatermark(document) Then
  20. Console.WriteLine("Add text watermark done.")
  21. Else
  22. Console.WriteLine("Add text watermark failed.")
  23. End If
  24. document.Release()
  25. Console.WriteLine("--------------------")
  26. ' Sample 2: Add image watermark
  27. document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf")
  28. If AddImageWatermark(document) Then
  29. Console.WriteLine("Add image watermark done.")
  30. Else
  31. Console.WriteLine("Add image watermark failed.")
  32. End If
  33. document.Release()
  34. Console.WriteLine("--------------------")
  35. ' Sample 3: Delete watermark
  36. Dim textWatermarkDocument As CPDFDocument = CPDFDocument.InitWithFilePath("Watermark.pdf")
  37. If DeleteWatermark(textWatermarkDocument) Then
  38. Console.WriteLine("Delete watermark done.")
  39. Else
  40. Console.WriteLine("Delete watermark failed.")
  41. End If
  42. textWatermarkDocument.Release()
  43. Console.WriteLine("--------------------")
  44. Console.WriteLine("Done!")
  45. Console.WriteLine("--------------------")
  46. Console.ReadLine()
  47. End Sub
  48. ' Add text watermark
  49. Private Function AddTextWatermark(document As CPDFDocument) As Boolean
  50. Dim watermark As CPDFWatermark = document.InitWatermark(C_Watermark_Type.WATERMARK_TYPE_TEXT)
  51. watermark.SetText("test")
  52. watermark.SetFontName("Helvetica")
  53. watermark.SetPages("0-3")
  54. Dim color As Byte() = {255, 0, 0}
  55. watermark.SetTextRGBColor(color)
  56. watermark.SetScale(2)
  57. watermark.SetRotation(0)
  58. watermark.SetOpacity(120)
  59. watermark.SetVertalign(C_Watermark_Vertalign.WATERMARK_VERTALIGN_CENTER)
  60. watermark.SetHorizalign(C_Watermark_Horizalign.WATERMARK_HORIZALIGN_CENTER)
  61. watermark.SetVertOffset(0)
  62. watermark.SetHorizOffset(0)
  63. watermark.SetFront(True)
  64. watermark.SetFullScreen(True)
  65. watermark.SetVerticalSpacing(10)
  66. watermark.SetHorizontalSpacing(10)
  67. watermark.CreateWatermark()
  68. Dim path As String = outputPath & "\AddTextWatermarkTest.pdf"
  69. If Not document.WriteToFilePath(path) Then
  70. Return False
  71. End If
  72. Console.WriteLine("Browse the changed file in " & path)
  73. Return True
  74. End Function
  75. ' Convert the bitmap to an array that can be set as an image watermark
  76. Private Function BitmapToByteArray(bitmap As Bitmap) As Byte()
  77. Dim bmpdata As BitmapData = Nothing
  78. Try
  79. bmpdata = bitmap.LockBits(New Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, bitmap.PixelFormat)
  80. Dim numbytes As Integer = bmpdata.Stride * bitmap.Height
  81. Dim bytedata(numbytes - 1) As Byte
  82. Dim ptr As IntPtr = bmpdata.Scan0
  83. Marshal.Copy(ptr, bytedata, 0, numbytes)
  84. Return bytedata
  85. Finally
  86. If bmpdata IsNot Nothing Then
  87. bitmap.UnlockBits(bmpdata)
  88. End If
  89. End Try
  90. End Function
  91. ' Add image watermark
  92. Private Function AddImageWatermark(document As CPDFDocument) As Boolean
  93. Dim watermark As CPDFWatermark = document.InitWatermark(C_Watermark_Type.WATERMARK_TYPE_IMG)
  94. Dim bitmap As New Bitmap("logo.png")
  95. watermark.SetImage(BitmapToByteArray(bitmap), bitmap.Width, bitmap.Height)
  96. watermark.SetPages("0-3")
  97. watermark.SetScale(2)
  98. watermark.SetRotation(1)
  99. watermark.SetOpacity(128)
  100. watermark.SetVertalign(C_Watermark_Vertalign.WATERMARK_VERTALIGN_CENTER)
  101. watermark.SetHorizalign(C_Watermark_Horizalign.WATERMARK_HORIZALIGN_CENTER)
  102. watermark.SetVertOffset(0)
  103. watermark.SetHorizOffset(0)
  104. watermark.SetFront(False)
  105. watermark.SetFullScreen(True)
  106. watermark.SetVerticalSpacing(10)
  107. watermark.SetHorizontalSpacing(10)
  108. watermark.CreateWatermark()
  109. watermark.UpdateWatermark()
  110. Dim path As String = outputPath & "\AddImageWatermarkTest.pdf"
  111. If Not document.WriteToFilePath(path) Then
  112. Return False
  113. End If
  114. Console.WriteLine("Browse the changed file in " & path)
  115. Return True
  116. End Function
  117. ' Delete watermark
  118. Private Function DeleteWatermark(watermarkDocument As CPDFDocument) As Boolean
  119. watermarkDocument.DeleteWatermarks()
  120. Dim path As String = outputPath & "\DeleteWatermarkTest.pdf"
  121. If Not watermarkDocument.WriteToFilePath(path) Then
  122. Return False
  123. End If
  124. Console.WriteLine("Browse the changed file in " & path)
  125. Return True
  126. End Function
  127. End Module