usmanazizgroupdocs
4/17/2017 - 9:50 AM

Documents-AddWatermarkToXObjects.vb

' For complete examples and data files, please go to https://github.com/groupdocs-watermark/GroupDocs.Watermark-for-.NET
Using doc As PdfDocument = Document.Load(Of PdfDocument)(Utilities.MapSourceFilePath(FilePath))
    ' Initialize image or text watermark
    Dim watermark As New TextWatermark("Protected image", New Font("Arial", 8))
    watermark.HorizontalAlignment = HorizontalAlignment.Center
    watermark.VerticalAlignment = VerticalAlignment.Center
    watermark.RotateAngle = 45
    watermark.SizingType = SizingType.ScaleToParentDimensions
    watermark.ScaleFactor = 1

    For Each page As PdfPage In doc.Pages
        For Each xObject As PdfXObject In page.XObjects
            If xObject.Image IsNot Nothing Then
                ' Add watermark to the image
                xObject.Image.AddWatermark(watermark)
            End If
        Next
    Next

    doc.Save()
End Using