How to add multiple watermark in PDF document using C# and VB.Net?
The Syncfusion Essential® PDF is a feature rich and high-performance .NET PDF library used to create, read, and edit PDF documents programmatically without Adobe dependencies. This library also offers functionality to merge, split, stamp, forms, compress, and secure PDF files.
You can add multiple watermarks in a PDF document using this library.
Steps to add multiple Watermark in PDF document using C# and VB.Net:
- Create a new C# console application project.
- Install the Syncfusion.Pdf.WinForms NuGet package as a reference to your .NET standard applications from NuGet.org.
- Include the following namespaces in Form.cs file:
C#:
using Syncfusion.Pdf; using Syncfusion.Pdf.Parsing;
VB.Net:
Imports Syncfusion.Pdf Imports Syncfusion.Pdf.Parsing
- Include the following namespaces in the Program.cs file:
C#:
//Load the PDF document PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Barcode.pdf"); //Set the font PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 20); foreach (var page in loadedDocument.Pages) { //Get the loaded page PdfLoadedPage loadedPage = page as PdfLoadedPage; //Create graphics PdfGraphics graphics = loadedPage.Graphics; //Draw watermark text PdfGraphicsState state = graphics.Save(); graphics.SetTransparency(0.25f); graphics.RotateTransform(-80); int y = 100; for (int i = 0; i < 4; i++) { graphics.DrawString("WaterMark", font, PdfPens.Red, PdfBrushes.Red, new PointF(-150, y)); graphics.DrawString("WaterMark", font, PdfPens.Red, PdfBrushes.Red, new PointF(-300, y)); y = y + 100; } graphics.Restore(); } //Save and close the document loadedDocument.Save("Watermark.pdf"); loadedDocument.Close(true);
VB.Net:
Private Sub SurroundingSub() Dim loadedDocument As PdfLoadedDocument = New PdfLoadedDocument("Barcode.pdf") Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 20) For Each page In loadedDocument.Pages Dim loadedPage As PdfLoadedPage = TryCast(page, PdfLoadedPage) Dim graphics As PdfGraphics = loadedPage.Graphics Dim state As PdfGraphicsState = graphics.Save() graphics.SetTransparency(0.25F) graphics.RotateTransform(-80) Dim y As Integer = 100 For i As Integer = 0 To 4 - 1 graphics.DrawString("WaterMark", font, PdfPens.Red, PdfBrushes.Red, New PointF(-150, y)) graphics.DrawString("WaterMark", font, PdfPens.Red, PdfBrushes.Red, New PointF(-300, y)) y = y + 100 Next graphics.Restore() Next loadedDocument.Save("Watermark.pdf") loadedDocument.Close(True) End Sub
- By executing the program, you will get the PDF document as follows:
A complete working sample can be downloaded from WatermarkSample.zip
Take a moment to peruse the documentation, where you can find the options like adding text and image watermark to the PDF document.
Refer here to explore the rich set of Syncfusion Essential® PDF features.
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from trial setup or from the NuGet feed,include a license key in your projects. Refer to link to learn about generating and registering Syncfusion® license key in your application to use the components without trail message.