Step-by-Step Guide to Adding and Removing Watermarks in PDF using C#

Jollen Moyani - May 30 '23 - - Dev Community

A watermark refers to a design, pattern, or text applied to a digital or physical document, image, or video to signify its ownership or authenticity. Typically, watermarks are transparent and strategically placed so as not to disrupt the content while still being visible enough to indicate the source of the material.

This advantageous feature can be utilized in various contexts, including branding and marketing, safeguarding against unauthorized usage, identifying and attributing the source of content, and ensuring document security, authenticity, and verification.

The Syncfusion .NET PDF Library supports adding image or text watermarks to a PDF document to indicate the authenticity or ownership of the document. The library provides APIs for programmatically adding watermarks to PDF documents, with options for customizing the watermark’s position, size, and opacity.

This article will cover creating a watermark for a PDF file using the Syncfusion .NET PDF Library.

Agenda:

Getting started with app creation

1.Create a new C# .NET console application project.

Select the Console App option

2.Install the Syncfusion.Pdf.Net.Core NuGet package as a reference to your .NET standard applications from NuGet.org.

Install the Syncfusion.Pdf.Net.Core NuGet package

Add a text watermark to a PDF using C#

Text watermarks are a practical technique for maintaining document security, intellectual property protection, and branding initiatives.

Follow these steps to add a text watermark to a PDF file using the Syncfusion PDF Library:

1.Use the PdfLoadedDocument class to load an existing PDF document.
2.Then, get the page reference (where you want to include the watermark text) in the PdfLoadedPage object.
3.Create an instance of the PdfWatermarkAnnotation class and customize the location, size, and opacity.
4.Include the watermark text using the DrawString method.
5.Add the watermark annotation to the annotations collection.
6.Finally, save the PDF file using the Save method.

The following code example adds a text watermark in a PDF document using C#.

//Load the PDF document.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputStream);

//Get the page.
PdfLoadedPage lpage = loadedDocument.Pages[0] as PdfLoadedPage;

//Creates PDF watermark annotation. 
PdfWatermarkAnnotation watermark = new PdfWatermarkAnnotation(new RectangleF(100, 300, 400, 400));

//Sets properties to the annotation. 
watermark.Opacity = 0.5f;

//Create the appearance of watermark. 
watermark.Appearance.Normal.Graphics.DrawString("Imported using Essential PDF", new PdfStandardFont(PdfFontFamily.Helvetica, 20), PdfBrushes.Red, new RectangleF(50, 50, 250, 50), new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle));

//Adds the annotation to page. 
lpage.Annotations.Add(watermark);
FileStream stream = new FileStream("Output.pdf", FileMode.Create);

//Save the modified document to file.
loadedDocument.Save(stream);

//Close the PDF document.
loadedDocument.Close(true);
stream.Close();
Enter fullscreen mode Exit fullscreen mode

Execute this code example to get a text watermarked PDF like the following screenshot.

Text watermark added in a PDF file

Text watermark added in a PDF file

Add image watermark to a PDF using C#

Image watermarks can be useful for protecting your intellectual property, establishing your brand identity, adding security features to your documents, and indicating confidentiality.

Follow these steps to add an image watermark to a PDF file using the Syncfusion PDF Library:

1.Use the PdfLoadedDocument class to load an existing PDF document.
2.Then, get the page reference (where you want to include the watermark image) in the PdfLoadedPage object.
3.Create an instance of the PdfWatermarkAnnotation class.
4.Customize the size, location, and opacity of the watermark instance.
5.Load the image from a file to the PdfBitmap object.
6.Draw the image on the graphics of the watermark instance using the DrawImage method.
7.Add the watermark annotation to the annotation collection of the page.
8.Finally, save the PDF file using the Save method.

The following code example adds an image watermark in a PDF document using C#.

//Load the PDF document.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream);

//Get the page. 
PdfLoadedPage lpage = loadedDocument.Pages[0] as PdfLoadedPage;

//Creates PDF watermark annotation. 
PdfWatermarkAnnotation watermark = new PdfWatermarkAnnotation(new RectangleF(100, 300, 400, 400));

//Sets properties to the annotation. 
watermark.Opacity = 0.5f;

//Create the appearance of watermark. 
FileStream imageStream = new FileStream(Image.jpg, FileMode.Open, FileAccess.Read);
PdfImage image = new PdfBitmap(imageStream);
watermark.Appearance.Normal.Graphics.DrawImage(image, new PointF(0, 0));

//Adds the annotation to page. 
lpage.Annotations.Add(watermark);
FileStream stream = new FileStream("output.pdf", FileMode.Create);

//Save the modified document to file.
loadedDocument.Save(stream);

//Close the PDF document.
loadedDocument.Close(true);
stream.Close();
Enter fullscreen mode Exit fullscreen mode

Execute this code example to get an image watermarked PDF document like the following screenshot.

Image watermark added in the PDF file

Image watermark added in the PDF file

Remove a watermark from a PDF using C#

Removing a watermark from a PDF document can be useful in cases where the watermark is no longer needed or when the PDF file needs to be shared without the watermark.

Follow these steps to remove a watermark from a PDF file using the Syncfusion PDF Library:

1.Use the PdfLoadedDocument class to load an existing PDF document.
2.Get the reference for the page (where you want to remove the watermark) in the PdfLoadedPage object.
3.Get the watermark annotation using the PdfLoadedAnnotation class.
4.Remove the watermark by passing the index value to the RemoveAt method.
5.Finally, save the PDF file using the Save method.

The following code example removes a watermark from a PDF using C#.

//Load the PDF document.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream);
for (int i = 0; i < loadedDocument.Pages.Count; i++)
{
  //Get the page. 
  PdfLoadedPage lpage = loadedDocument.Pages[i] as PdfLoadedPage;

  //Gets the annotation collection.
  PdfLoadedAnnotationCollection annotations = lpage.Annotations;
  for (int j = 0; j < annotations.Count; j++)
  {
      //Gets the annotation.
      PdfLoadedAnnotation annotation = lpage.Annotations[j] as PdfLoadedAnnotation;
      if (annotation != null && annotation is PdfLoadedWatermarkAnnotation)
      {
          //Removes the first annotation.
          annotations.RemoveAt(j);
      }
   }
} 
FileStream stream = new FileStream("output.pdf", FileMode.Create);

//Save the modified document to file.
loadedDocument.Save(stream);

//Close the PDF document.
loadedDocument.Close(true);
stream.Close();
Enter fullscreen mode Exit fullscreen mode

Execute this code example to remove a watermark from a PDF document like in the following screenshot.

Watermark removed from a PDF file

Watermark removed from a PDF file

GitHub reference

You can find examples for doing this in the GitHub repository.

Conclusion

Thanks for reading! In this blog post, we have learned how to add image and text watermarks in a C# .NET console application using our Syncfusion PDF Library. Try out these methods and leave your feedback in the comments section of this blog post!

In the PDF Library, there are other features such as form authentication, headers, footers, tables of contents, and automatic bookmark hierarchy based on heading style. Take a moment to peruse the documentation, where you’ll find other options and features, all with accompanying code examples.

For existing customers, the new version of Essential Studio is available for download from the License and Downloads page. If you are not a Syncfusion customer, try our 30-day free trial to check out our available features.

For questions, contact us through our support forum, support portal, or feedback portal. We are delighted to assist you!

Related blogs

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .