123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- Imports System.IO
- Imports ComPDFKit.PDFDocument
- Imports System.Runtime.InteropServices
- Imports System.Drawing
- Imports System.Drawing.Imaging
- Module BackgroundTest
- Private parentPath = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory())))
- Private outputPath As String = Path.Combine(parentPath, "Output", "VB")
- Sub Main()
- ' Preparation work
- Console.WriteLine("Running Watermark test sample..." & Environment.NewLine)
- SDKLicenseHelper.LicenseVerify()
- Dim document As CPDFDocument = CPDFDocument.InitWithFilePath("CommonFivePage.pdf")
- If Not Directory.Exists(outputPath) Then
- Directory.CreateDirectory(outputPath)
- End If
- ' Sample 1: Add color background
- If AddColorBackground(document) Then
- Console.WriteLine("Add color background done.")
- End If
- document.Release()
- Console.WriteLine("--------------------")
- ' Sample 2: Add image background
- document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf")
- If AddImageBackground(document) Then
- Console.WriteLine("Add image background done.")
- End If
- document.Release()
- Console.WriteLine("--------------------")
- ' Sample 3: Remove background
- Dim colorBgDocument As CPDFDocument = CPDFDocument.InitWithFilePath("ColorBackground.pdf")
- Dim imageBgDocument As CPDFDocument = CPDFDocument.InitWithFilePath("ImageBackground.pdf")
- If RemoveBackground(colorBgDocument, imageBgDocument) Then
- Console.WriteLine("Remove background done.")
- End If
- colorBgDocument.Release()
- imageBgDocument.Release()
- Console.WriteLine("--------------------")
- Console.WriteLine("Done!")
- Console.WriteLine("--------------------")
- Console.ReadLine()
- End Sub
- ' Add color background
- ' Parameters:
- ' - document: Regular document
- Private Function AddColorBackground(document As CPDFDocument) As Boolean
- Dim background As CPDFBackground = document.GetBackground()
- background.SetBackgroundType(C_Background_Type.BG_TYPE_COLOR)
- background.SetColor(New Byte() {255, 0, 0})
- background.SetOpacity(255) ' 0-255
- background.SetScale(1) ' 1 == 100%
- background.SetRotation(0) ' Use radians
- background.SetHorizalign(C_Background_Horizalign.BG_HORIZALIGN_CENTER)
- background.SetVertalign(C_Background_Vertalign.BG_VERTALIGN_CENTER)
- background.SetXOffset(0)
- background.SetYOffset(0)
- background.SetPages("0-2") ' Page numbering from 0
- background.Update() ' Note: update after setup is complete
- Dim path As String = outputPath & "\AddColorBackgroundTest.pdf"
- If Not document.WriteToFilePath(path) Then
- Return False
- End If
- Console.WriteLine("Browse the changed file in " & path)
- Return True
- End Function
- ' Convert the bitmap to an array that can be set as an image watermark
- ' Parameters:
- ' - bitmap: Image source to be used as an image watermark.
- ' Returns: An array for setting image
- Public Function BitmapToByteArray(bitmap As Bitmap) As Byte()
- Dim bmpdata As BitmapData = Nothing
- Try
- bmpdata = bitmap.LockBits(New Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, bitmap.PixelFormat)
- Dim numbytes As Integer = bmpdata.Stride * bitmap.Height
- Dim bytedata(numbytes - 1) As Byte
- Dim ptr As IntPtr = bmpdata.Scan0
- Marshal.Copy(ptr, bytedata, 0, numbytes)
- Return bytedata
- Finally
- If bmpdata IsNot Nothing Then
- bitmap.UnlockBits(bmpdata)
- End If
- End Try
- End Function
- Private Function AddImageBackground(document As CPDFDocument) As Boolean
- Dim background As CPDFBackground = document.GetBackground()
- background.SetBackgroundType(C_Background_Type.BG_TYPE_IMAGE)
- Dim bitmap As New Bitmap("logo.png")
- background.SetImage(BitmapToByteArray(bitmap), bitmap.Width, bitmap.Height, ComPDFKit.Import.C_Scale_Type.fitCenter)
- background.SetOpacity(128) ' 0-255
- background.SetScale(1) ' 1 == 100%
- background.SetRotation(1.0F) ' Use radians
- background.SetHorizalign(C_Background_Horizalign.BG_HORIZALIGN_CENTER)
- background.SetVertalign(C_Background_Vertalign.BG_VERTALIGN_CENTER)
- background.SetXOffset(0)
- background.SetYOffset(0)
- background.SetPages("0-2") ' Page numbering from 0
- background.Update() ' Note: update after setup is complete
- Dim filePath As String = Path.Combine(outputPath, "AddImageBackgroundTest.pdf")
- If Not document.WriteToFilePath(filePath) Then
- Return False
- End If
- Console.WriteLine("Browse the changed file in " & filePath)
- Return True
- End Function
- Private Function RemoveBackground(ByVal colorBgDocument As CPDFDocument, ByVal imageBgDocument As CPDFDocument) As Boolean
- Dim colorBackground As CPDFBackground = colorBgDocument.GetBackground()
- If colorBackground.GetBackgroundType() <> C_Background_Type.BG_TYPE_COLOR Then
- Return False
- End If
- colorBackground.Clear()
- Dim path1 As String = outputPath & "\ClearColorBgTest.pdf"
- If Not colorBgDocument.WriteToFilePath(path1) Then
- Return False
- End If
- Console.WriteLine("Browse the changed file in " & path1)
- Dim imageBackground As CPDFBackground = imageBgDocument.GetBackground()
- imageBackground.Clear()
- Dim path2 As String = outputPath & "\ClearImageBgTest.pdf"
- If Not imageBgDocument.WriteToFilePath(path2) Then
- Return False
- End If
- Console.WriteLine("Browse the changed file in " & path2)
- Return True
- End Function
- End Module
|