Articles in this section
Category / Section

How to add a watermark to a PDF page with an opaque image

2 mins read

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:

  1. Create a new C# Windows Forms application project. Create a new C# Windows Forms application project.
  2. Install the Syncfusion.Pdf.WinForms NuGet package as reference to your .NET Framework application from NuGet.org. NET Framework application from NuGet.org.
  3. 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

 

  1. 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. watermark on opaque image

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 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.

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied