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 a watermark on an opaque image in a PDF page programmatically:
- Create a new C# 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 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 a watermark on an opaque image in a PDF.
C#
// Load the PDF document
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("input.pdf");
// Get the 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 the current graphics state
PdfGraphicsState state = graphics.Save();
// Set the transparency of this graphics with the specified value for the 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 watermark 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 the 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 the current graphics state
Dim state As PdfGraphicsState = graphics.Save()
' Set the transparency of this graphics with the specified value for the 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 watermark 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.
Note:
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from a trial setup or from 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.
Conclusion
I hope you enjoyed learning about how to add a watermark to a PDF page with an opaque image.
You can refer to our WinForms PDF’s feature tour page to know about its other groundbreaking feature representations. You can also explore our WinForms PDF documentation to understand how to present and manipulate data.
For current customers, you can check out our WinForms components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our WinForms PDF and other WinForms components.
If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!