''' <summary>
''' Adds the watermark image to the PDF document.
''' </summary>
''' <param name="inPdfFilename">The filename of input PDF document.</param>
''' <param name="outPdfFilename">The filename of output PDF document.</param>
''' <param name="watermarkImageFilename">The filename of watermark image.</param>
Public Shared Sub AddWatermarkToPdfDocument(inPdfFilename As String, outPdfFilename As String, watermarkImageFilename As String)
AddWatermarkToPdfDocument(inPdfFilename, outPdfFilename, watermarkImageFilename, 0.25F, New Vintasoft.Imaging.PaddingF(0.1F), False)
End Sub
''' <summary>
''' Adds the watermark image to the PDF document.
''' </summary>
''' <param name="inPdfFilename">The filename of input PDF document.</param>
''' <param name="outPdfFilename">The filename of output PDF document.</param>
''' <param name="watermarkImageFilename">The filename of watermark image.</param>
''' <param name="watermarkImageAlpha">The watermark image alpha.</param>
''' <param name="watermarkImagePadding">The watermark image padding.</param>
''' <param name="ignorePageRotation">Indicates that value of Vintasoft.Imaging.Pdf.Tree.PdfPage.Rotate property should be ignored.</param>
Public Shared Sub AddWatermarkToPdfDocument(inPdfFilename As String, outPdfFilename As String, watermarkImageFilename As String, watermarkImageAlpha As Single, watermarkImagePadding As Vintasoft.Imaging.PaddingF, ignorePageRotation As Boolean)
' check watermark padding
If watermarkImagePadding.Horizontal < 0 OrElse watermarkImagePadding.Vertical < 0 Then
Throw New System.ArgumentOutOfRangeException("watermarkImagePadding")
End If
' open PDF document
Using document As New Vintasoft.Imaging.Pdf.PdfDocument(inPdfFilename)
' create "watermark" resource
Dim watermakResource As Vintasoft.Imaging.Pdf.Tree.PdfResource
Using watermarkImage As New Vintasoft.Imaging.VintasoftImage(watermarkImageFilename)
' if watermarkImage is PDF page
If watermarkImage.SourceInfo.DecoderName = "Pdf" Then
' create form resource based on PDF page
Dim page As Vintasoft.Imaging.Pdf.Tree.PdfPage = Vintasoft.Imaging.Pdf.PdfDocumentController.GetPageAssociatedWithImage(watermarkImage)
watermakResource = New Vintasoft.Imaging.Pdf.Tree.PdfFormXObjectResource(document, page)
Else
' create watermark image resource with JPEG compression
Dim watermarkResourceCompression As Vintasoft.Imaging.Pdf.PdfCompression = Vintasoft.Imaging.Pdf.PdfCompression.Jpeg
watermakResource = New Vintasoft.Imaging.Pdf.Tree.PdfImageResource(document, watermarkImage, watermarkResourceCompression)
End If
End Using
' for each page in PDF document
For Each page As Vintasoft.Imaging.Pdf.Tree.PdfPage In document.Pages
' add watermark on PDF page
AddWatermarkToPdfPage(page, watermakResource, watermarkImagePadding, watermarkImageAlpha, ignorePageRotation)
Next
' pack PDF document to an output file
document.Pack(outPdfFilename)
End Using
End Sub
''' <summary>
''' Adds the watermark image to the PDF page.
''' </summary>
''' <param name="page">The PDF page.</param>
''' <param name="watermarkResource">The PDF resource that contains watermark.</param>
''' <param name="watermarkImagePadding">The watermark image padding.</param>
''' <param name="watermarkImageAlpha">The watermark image alpha.</param>
''' <param name="ignorePageRotation">Indicates that value of Vintasoft.Imaging.Pdf.Tree.PdfPage.Rotate property should be ignored.</param>
Public Shared Sub AddWatermarkToPdfPage(page As Vintasoft.Imaging.Pdf.Tree.PdfPage, watermarkResource As Vintasoft.Imaging.Pdf.Tree.PdfResource, watermarkImagePadding As Vintasoft.Imaging.PaddingF, watermarkImageAlpha As Single, ignorePageRotation As Boolean)
Dim watermarkImageResource As Vintasoft.Imaging.Pdf.Tree.PdfImageResource = TryCast(watermarkResource, Vintasoft.Imaging.Pdf.Tree.PdfImageResource)
Dim watermarkFormResource As Vintasoft.Imaging.Pdf.Tree.PdfFormXObjectResource = TryCast(watermarkResource, Vintasoft.Imaging.Pdf.Tree.PdfFormXObjectResource)
' gets the size of watermark resource
Dim watermarkResourceSize As System.Drawing.SizeF
If watermarkImageResource IsNot Nothing Then
watermarkResourceSize = New System.Drawing.SizeF(watermarkImageResource.Width, watermarkImageResource.Height)
Else
watermarkResourceSize = New System.Drawing.SizeF(watermarkFormResource.BoundingBox.Width, watermarkFormResource.BoundingBox.Height)
End If
' create PDF graphics from PDF page and specify that graphics must clear and add new content
Using graphics As Vintasoft.Imaging.Pdf.Drawing.PdfGraphics = Vintasoft.Imaging.Pdf.Drawing.PdfGraphics.FromPage(page)
' get mediabox of PDF page
Dim pageMediaBox As System.Drawing.RectangleF = page.MediaBox
' if watermark image must be rotated
If Not ignorePageRotation AndAlso page.Rotate <> 0 Then
' rotate padding
watermarkImagePadding.Rotate(page.Rotate)
End If
' calculate watermark rectangle
Dim watermarkRect As New System.Drawing.RectangleF(pageMediaBox.X + watermarkImagePadding.Left * pageMediaBox.Width, pageMediaBox.Y + watermarkImagePadding.Bottom * pageMediaBox.Height, pageMediaBox.Width - watermarkImagePadding.Horizontal * pageMediaBox.Width, pageMediaBox.Height - watermarkImagePadding.Vertical * pageMediaBox.Height)
Dim scale As Single = System.Math.Min(watermarkRect.Width / watermarkResourceSize.Width, watermarkRect.Height / watermarkResourceSize.Height)
Dim watermarkImageRect As New System.Drawing.RectangleF(watermarkRect.X + (watermarkRect.Width - watermarkResourceSize.Width * scale) / 2, watermarkRect.Y + (watermarkRect.Height - watermarkResourceSize.Height * scale) / 2, watermarkResourceSize.Width * scale, watermarkResourceSize.Height * scale)
' create graphics state parameters
Dim gsParams As New Vintasoft.Imaging.Pdf.Tree.PdfGraphicsStateParameters(page.Document)
' set blending mode to "Multiply"
gsParams.BlendMode = Vintasoft.Imaging.Pdf.Tree.GraphicsStateBlendMode.Multiply
' set alpha transparency
gsParams.AlphaForNonStrokingOperations = watermarkImageAlpha
' save graphics state
graphics.SaveGraphicsState()
' set graphics state parameters
graphics.SetGraphicsStateParameters(gsParams)
' if watermark image must be rotated
If Not ignorePageRotation AndAlso page.Rotate <> 0 Then
' apply rotation to the PdfGraphics
Dim matrix As New Vintasoft.Imaging.AffineMatrix()
matrix.RotateAt(page.Rotate, watermarkImageRect.X + watermarkImageRect.Width / 2, watermarkImageRect.Y + watermarkImageRect.Height / 2)
graphics.MultiplyTransform(matrix)
End If
' if watermark is an image
If watermarkImageResource IsNot Nothing Then
' draw watermark image
graphics.DrawImage(watermarkImageResource, watermarkImageRect)
Else
' draw watermark form
graphics.DrawForm(watermarkFormResource, watermarkImageRect)
End If
' restore graphics state
graphics.RestoreGraphicsState()
End Using
End Sub