How to add a watermark in the center of the page in a PDF document using C# and VB.NET?
The Syncfusion Essential® PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can add a watermark in the center of the page in a PDF document using C# and VB.NET.
Steps to add a watermark in the center of the page in a PDF document
- Create a new Windows Forms application project.
- Install the Syncfusion.Pdf.WinForms NuGet package as a reference to your .NET Framework application from NuGet.org.
- Include the following namespace in the Form1.cs file.
C#
using Syncfusion.Pdf; using Syncfusion.Pdf.Parsing; using Syncfusion.Pdf.Graphics; using System.Drawing;
VB.NET
Imports Syncfusion.Pdf Imports Syncfusion.Pdf.Parsing Imports Syncfusion.Pdf.Graphics Imports System.Drawing
- Add the following code in the button1_click event to add a watermark in the center of the page in a PDF document using C# and VB.NET.
C#
//Load an existing PDF document PdfLoadedDocument loadedDocument = new PdfLoadedDocument("../../Data/Barcode.pdf"); //Set the font PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 20); string watermarkText = "Imported using Essential PDF"; //Calculate the watermark text size SizeF size = font.MeasureString(watermarkText); for (int i=0;i<loadedDocument.Pages.Count;i++) { //Load PDF pages PdfLoadedPage loadedPage = loadedDocument.Pages[i] as PdfLoadedPage; //Create graphics for the existing page PdfGraphics graphics = loadedPage.Graphics; float x = loadedPage.Size.Width / 2 - size.Width / 2; float y = loadedPage.Size.Height / 2; //Insert watermark text to PDF PdfGraphicsState state = graphics.Save(); graphics.SetTransparency(0.25f); graphics.DrawString(watermarkText, font, PdfBrushes.Red, new RectangleF(x, y, size.Width, size.Height)); graphics.Restore(); //Draw string graphics.DrawString("Hello World!!!", font, PdfBrushes.Black, new PointF(0, 0)); } //Save and close the document loadedDocument.Save("Watermark.pdf"); loadedDocument.Close(true); //This will open the PDF file and the result will be seen in the default PDF Viewer Process.Start("Watermark.pdf");
VB.NET
'Load an existing PDF document Dim loadedDocument As PdfLoadedDocument = New PdfLoadedDocument("../../Data/Barcode.pdf") 'Set the font Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 20) Dim watermarkText As String = "Imported using Essential PDF" 'Calculate the watermark text size Dim size As SizeF = font.MeasureString(watermarkText) For i As Integer = 0 To loadedDocument.Pages.Count - 1 'Load PDF pages Dim loadedPage As PdfLoadedPage = TryCast(loadedDocument.Pages(i), PdfLoadedPage) 'Create graphics for the existing page Dim graphics As PdfGraphics = loadedPage.Graphics Dim x As Single = loadedPage.Size.Width / 2 - size.Width / 2 Dim y As Single = loadedPage.Size.Height / 2 'Insert watermark text to PDF Dim state As PdfGraphicsState = graphics.Save() graphics.SetTransparency(0.25F) graphics.DrawString(watermarkText, font, PdfBrushes.Red, New RectangleF(x, y, size.Width, size.Height)) graphics.Restore() 'Draw string graphics.DrawString("Hello World!!!", font, PdfBrushes.Black, New PointF(0, 0)) Next 'Save and close the document loadedDocument.Save("Watermark.pdf") loadedDocument.Close(True) 'This will open the PDF file and the result will be seen in the default PDF Viewer Process.Start("Watermark.pdf")
A complete working sample can be download from WatermarkSample.zip.
By executing the program, you will get the output document as follows,
Take a moment to peruse the documentation. You can find other options like adding image watermark in PDF document and features like images to PDF, Text to PDF, and more with code examples.
Refer to here to explore a rich set of Syncfusion Essential® PDF features.
Note:
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from the trial setup or the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering the Syncfusion® license key in your application to use the components without a trial message.
See Also:
Add watermark to PDF file with opaque image
Conclusion:
I hope you enjoyed learning about how to add a watermark in the center of the page in a PDF document using C# and VB.NET
You can refer to our Flutter PDF feature tour page to learn about its other groundbreaking features and documentation, and how to quickly get started with configuration specifications. You can also explore our Flutter PDF Flutter PDF examples to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to explore our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums or feedback portal. We are always happy to assist you!