BackgroundTest.vb 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. Imports System.IO
  2. Imports ComPDFKit.PDFDocument
  3. Imports System.Runtime.InteropServices
  4. Imports System.Drawing
  5. Imports System.Drawing.Imaging
  6. Module BackgroundTest
  7. Private parentPath = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory())))
  8. Private outputPath As String = Path.Combine(parentPath, "Output", "VB")
  9. Sub Main()
  10. ' Preparation work
  11. Console.WriteLine("Running Watermark test sample..." & Environment.NewLine)
  12. SDKLicenseHelper.LicenseVerify()
  13. Dim document As CPDFDocument = CPDFDocument.InitWithFilePath("CommonFivePage.pdf")
  14. If Not Directory.Exists(outputPath) Then
  15. Directory.CreateDirectory(outputPath)
  16. End If
  17. ' Sample 1: Add color background
  18. If AddColorBackground(document) Then
  19. Console.WriteLine("Add color background done.")
  20. End If
  21. document.Release()
  22. Console.WriteLine("--------------------")
  23. ' Sample 2: Add image background
  24. document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf")
  25. If AddImageBackground(document) Then
  26. Console.WriteLine("Add image background done.")
  27. End If
  28. document.Release()
  29. Console.WriteLine("--------------------")
  30. ' Sample 3: Remove background
  31. Dim colorBgDocument As CPDFDocument = CPDFDocument.InitWithFilePath("ColorBackground.pdf")
  32. Dim imageBgDocument As CPDFDocument = CPDFDocument.InitWithFilePath("ImageBackground.pdf")
  33. If RemoveBackground(colorBgDocument, imageBgDocument) Then
  34. Console.WriteLine("Remove background done.")
  35. End If
  36. colorBgDocument.Release()
  37. imageBgDocument.Release()
  38. Console.WriteLine("--------------------")
  39. Console.WriteLine("Done!")
  40. Console.WriteLine("--------------------")
  41. Console.ReadLine()
  42. End Sub
  43. ' Add color background
  44. ' Parameters:
  45. ' - document: Regular document
  46. Private Function AddColorBackground(document As CPDFDocument) As Boolean
  47. Dim background As CPDFBackground = document.GetBackground()
  48. background.SetBackgroundType(C_Background_Type.BG_TYPE_COLOR)
  49. background.SetColor(New Byte() {255, 0, 0})
  50. background.SetOpacity(255) ' 0-255
  51. background.SetScale(1) ' 1 == 100%
  52. background.SetRotation(0) ' Use radians
  53. background.SetHorizalign(C_Background_Horizalign.BG_HORIZALIGN_CENTER)
  54. background.SetVertalign(C_Background_Vertalign.BG_VERTALIGN_CENTER)
  55. background.SetXOffset(0)
  56. background.SetYOffset(0)
  57. background.SetPages("0-2") ' Page numbering from 0
  58. background.Update() ' Note: update after setup is complete
  59. Dim path As String = outputPath & "\AddColorBackgroundTest.pdf"
  60. If Not document.WriteToFilePath(path) Then
  61. Return False
  62. End If
  63. Console.WriteLine("Browse the changed file in " & path)
  64. Return True
  65. End Function
  66. ' Convert the bitmap to an array that can be set as an image watermark
  67. ' Parameters:
  68. ' - bitmap: Image source to be used as an image watermark.
  69. ' Returns: An array for setting image
  70. Public Function BitmapToByteArray(bitmap As Bitmap) As Byte()
  71. Dim bmpdata As BitmapData = Nothing
  72. Try
  73. bmpdata = bitmap.LockBits(New Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, bitmap.PixelFormat)
  74. Dim numbytes As Integer = bmpdata.Stride * bitmap.Height
  75. Dim bytedata(numbytes - 1) As Byte
  76. Dim ptr As IntPtr = bmpdata.Scan0
  77. Marshal.Copy(ptr, bytedata, 0, numbytes)
  78. Return bytedata
  79. Finally
  80. If bmpdata IsNot Nothing Then
  81. bitmap.UnlockBits(bmpdata)
  82. End If
  83. End Try
  84. End Function
  85. Private Function AddImageBackground(document As CPDFDocument) As Boolean
  86. Dim background As CPDFBackground = document.GetBackground()
  87. background.SetBackgroundType(C_Background_Type.BG_TYPE_IMAGE)
  88. Dim bitmap As New Bitmap("logo.png")
  89. background.SetImage(BitmapToByteArray(bitmap), bitmap.Width, bitmap.Height, ComPDFKit.Import.C_Scale_Type.fitCenter)
  90. background.SetOpacity(128) ' 0-255
  91. background.SetScale(1) ' 1 == 100%
  92. background.SetRotation(1.0F) ' Use radians
  93. background.SetHorizalign(C_Background_Horizalign.BG_HORIZALIGN_CENTER)
  94. background.SetVertalign(C_Background_Vertalign.BG_VERTALIGN_CENTER)
  95. background.SetXOffset(0)
  96. background.SetYOffset(0)
  97. background.SetPages("0-2") ' Page numbering from 0
  98. background.Update() ' Note: update after setup is complete
  99. Dim filePath As String = Path.Combine(outputPath, "AddImageBackgroundTest.pdf")
  100. If Not document.WriteToFilePath(filePath) Then
  101. Return False
  102. End If
  103. Console.WriteLine("Browse the changed file in " & filePath)
  104. Return True
  105. End Function
  106. Private Function RemoveBackground(ByVal colorBgDocument As CPDFDocument, ByVal imageBgDocument As CPDFDocument) As Boolean
  107. Dim colorBackground As CPDFBackground = colorBgDocument.GetBackground()
  108. If colorBackground.GetBackgroundType() <> C_Background_Type.BG_TYPE_COLOR Then
  109. Return False
  110. End If
  111. colorBackground.Clear()
  112. Dim path1 As String = outputPath & "\ClearColorBgTest.pdf"
  113. If Not colorBgDocument.WriteToFilePath(path1) Then
  114. Return False
  115. End If
  116. Console.WriteLine("Browse the changed file in " & path1)
  117. Dim imageBackground As CPDFBackground = imageBgDocument.GetBackground()
  118. imageBackground.Clear()
  119. Dim path2 As String = outputPath & "\ClearImageBgTest.pdf"
  120. If Not imageBgDocument.WriteToFilePath(path2) Then
  121. Return False
  122. End If
  123. Console.WriteLine("Browse the changed file in " & path2)
  124. Return True
  125. End Function
  126. End Module