How to add a watermark to a PDF page with an opaque image
Syncfusion Essential PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can add watermark to a PDF page with an opaque image using C# and VB.NET.
Steps to add watermark on opaque image in PDF page programmatically:
- Create a new C# Windows Forms application project.
- Install the Syncfusion.Pdf.WinForms NuGet package as reference to your .NET Framework application from NuGet.org.
- Include the following namespaces in the Form1.Designer.cs file.
C#
using Syncfusion.Pdf; using Syncfusion.Pdf.Parsing; using Syncfusion.Pdf.Graphics; using System.Drawing;
VB.NET
Imports System.Drawing Imports Syncfusion.Pdf Imports Syncfusion.Pdf.Graphics Imports Syncfusion.Pdf.Parsing
- Use the following code snippet to add watermark on opaque image in PDF.
C#
//Load the PDF document PdfLoadedDocument loadedDocument = new PdfLoadedDocument("input.pdf"); //Get first page PdfPageBase loadedPage = loadedDocument.Pages[0]; //Get the graphics of the PDF page PdfGraphics graphics = loadedPage.Graphics; //Get the font PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 20); //Save current graphics state PdfGraphicsState state = graphics.Save(); //Sets the transparency of this graphics with the specified value for pen graphics.SetTransparency(0.25f); //Draw watermark text with transparency graphics.DrawString("Transparent watermark on opaque image", font, PdfPens.Red, PdfBrushes.Red, new PointF(100, 170)); //Restore the saved graphics state graphics.Restore(state); //Draw water text without transparency graphics.DrawString("Watermark on opaque image", font, PdfPens.Red, PdfBrushes.Red, new PointF(150, 200)); //Save and close the document loadedDocument.Save("WatermarkOnOpaqueImage.pdf"); loadedDocument.Close(true);
VB.NET
'Load the PDF document Dim loadedDocument As New PdfLoadedDocument("input.pdf") 'Get first page Dim loadedPage As PdfPageBase = loadedDocument.Pages(0) 'Get the graphics of the PDF page Dim graphics As PdfGraphics = loadedPage.Graphics 'Get the font Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 20) 'Save current graphics state Dim state As PdfGraphicsState = graphics.Save() 'Sets the transparency of this Graphics with the specified value for pen graphics.SetTransparency(0.25F) 'Draw watermark text with transparency graphics.DrawString("Transparent watermark on opaque image", font, PdfPens.Red, PdfBrushes.Red, New PointF(100, 170)) 'Restore the saved graphics state graphics.Restore(state) 'Draw water text without transparency graphics.DrawString("Watermark on opaque image", font, PdfPens.Red, PdfBrushes.Red, New PointF(150, 200)) 'Save and close the document loadedDocument.Save("WatermarkOnOpaqueImage.pdf") loadedDocument.Close(True)
A complete working sample can be downloaded from AddWatermarkToPDFWithOpaqueImage.zip.
By executing the program, you will get the PDF document as follows.
Take a moment to peruse the documentation for working with watermarks, where you will find other options like add text and image watermarks in PDF document.
Refer here to explore the rich set of Syncfusion Essential PDF features.
An online sample link to add watermark to PDF document.
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.