Step-by-Step Guide to Adding and Removing Watermarks in PDF using C#
Tags: fileformats, dotnet, csharp, pdf
Published: 2023-05-30 02:03:14

---
title: Step-by-Step Guide to Adding and Removing Watermarks in PDF using C#
published: true
date: 2023-05-29 11:00:00 UTC
tags: fileformats, dotnet, csharp, pdf
canonical_url: https://www.syncfusion.com/blogs/post/add-remove-watermark-pdf-csharp.aspx
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o4vj3gryudl6kx7ry66r.png
---
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](https://www.syncfusion.com/pdf-framework/net/pdf-library "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](https://www.syncfusion.com/document-processing/pdf-framework/net/pdf-library "Syncfusion .NET PDF Library").

Agenda:

- [Add a text watermark to a PDF using C#](#add)
- [Add an image watermark to a PDF using C#](#image)
- [Remove a watermark from a PDF using C#](#remove)

## Getting started with app creation

1.Create a new C# .NET [console application project](https://learn.microsoft.com/en-us/visualstudio/get-started/csharp/tutorial-console?view=vs-2022 "Tutorial: Create a simple C# console app in Visual Studio").  
 ![Select the Console App option](https://www.syncfusion.com/blogs/wp-content/uploads/2023/05/Select-the-Console-App-option-1.png)

2.Install the [Syncfusion.Pdf.Net.Core](https://www.nuget.org/packages/Syncfusion.Pdf.Net.Core "Syncfusion.Pdf.Net.Core NuGet Package") NuGet package as a reference to your .NET standard applications from [NuGet.org](https://www.nuget.org/ "NuGet Gallery").  
 ![Install the Syncfusion.Pdf.Net.Core NuGet package](https://www.syncfusion.com/blogs/wp-content/uploads/2023/05/Install-the-Syncfusion.Pdf.Net_.Core-NuGet-package-1.png)

## <a name="add">Add a text watermark to a PDF using C#</a>

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:](https://www.syncfusion.com/document-processing/pdf-framework/net/pdf-library "Syncfusion .NET PDF Library")

1.Use the [PdfLoadedDocument](https://help.syncfusion.com/cr/file-formats/Syncfusion.Pdf.Parsing.PdfLoadedDocument.html "PdfLoadedDocument Class of .NET PDF Library") class to load an existing PDF document.
2.Then, get the page reference (where you want to include the watermark text) in the [PdfLoadedPage](https://help.syncfusion.com/cr/file-formats/Syncfusion.Pdf.PdfLoadedPage.html "PdfLoadedPage Class of .NET PDF Library") object.
3.Create an instance of the [PdfWatermarkAnnotation](https://help.syncfusion.com/cr/file-formats/Syncfusion.Pdf.Interactive.PdfWatermarkAnnotation.html "PdfWatermarkAnnotation Class of .NET PDF Library") 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#.

```csharp
//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();
```

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

  <img src="https://www.syncfusion.com/blogs/wp-content/uploads/2023/05/Text-watermark-added-in-a-PDF-file-3.png" alt="Text watermark added in a PDF file" style="width:100%">

  <figcaption>Text watermark added in a PDF file</figcaption>

</figure>

## <a name="image">Add image watermark to a PDF using C#</a>

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](https://help.syncfusion.com/cr/file-formats/Syncfusion.Pdf.Parsing.PdfLoadedDocument.html "PdfLoadedDocument Class of .NET PDF Library") class to load an existing PDF document.
2.Then, get the page reference (where you want to include the watermark image) in the [PdfLoadedPage](https://help.syncfusion.com/cr/file-formats/Syncfusion.Pdf.PdfLoadedPage.html "PdfLoadedPage") object.
3.Create an instance of the [PdfWatermarkAnnotation](https://help.syncfusion.com/cr/file-formats/Syncfusion.Pdf.Interactive.PdfWatermarkAnnotation.html "PdfWatermarkAnnotation Class of .NET PDF Library") class.
4.Customize the size, location, and opacity of the watermark instance.
5.Load the image from a file to the [PdfBitmap](https://help.syncfusion.com/cr/file-formats/Syncfusion.Pdf.Graphics.PdfBitmap.html "PdfBitmap Class of .NET PDF Library") object.
6.Draw the image on the graphics of the watermark instance using the [DrawImage](https://learn.microsoft.com/en-us/dotnet/api/system.drawing.graphics.drawimage?view=windowsdesktop-8.0 "Graphics.DrawImage method") 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#.

```csharp
//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();
```

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

  <img src="https://www.syncfusion.com/blogs/wp-content/uploads/2023/05/Image-watermark-added-in-the-PDF-file-4.png" alt="Image watermark added in the PDF file" style="width:100%">

  <figcaption>Image watermark added in the PDF file</figcaption>

</figure>

## <a name="remove">Remove a watermark from a PDF using C#</a>

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](https://help.syncfusion.com/cr/file-formats/Syncfusion.Pdf.Parsing.PdfLoadedDocument.html "PdfLoadedDocument Class of .NET PDF Library") class to load an existing PDF document.
2.Get the reference for the page (where you want to remove the watermark) in the [PdfLoadedPage](https://help.syncfusion.com/cr/file-formats/Syncfusion.Pdf.PdfLoadedPage.html "PdfLoadedPage Class of .NET PDF Library") object.
3.Get the watermark annotation using the [PdfLoadedAnnotation](https://help.syncfusion.com/cr/file-formats/Syncfusion.Pdf.Interactive.PdfLoadedAnnotation.html "PdfLoadedAnnotation Class of .NET PDF Library") class.
4.Remove the watermark by passing the index value to the [RemoveAt](https://help.syncfusion.com/cr/file-formats/Syncfusion.Pdf.Parsing.PdfLoadedPageCollection.html#Syncfusion_Pdf_Parsing_PdfLoadedPageCollection_RemoveAt_System_Int32_ "RemoveAt(Int32) method of .NET PDF Library") method.
5.Finally, save the PDF file using the **Save** method.

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

```csharp
//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();
```

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

  <img src="https://www.syncfusion.com/blogs/wp-content/uploads/2023/05/Watermark-removed-from-a-PDF-file-3.png" alt="Watermark removed from a PDF file" style="width:100%">

  <figcaption>Watermark removed from a PDF file</figcaption>

</figure>

## GitHub reference

You can find examples for [doing this](https://github.com/SyncfusionExamples/add-watermark-to-pdf-csharp "Add and Remove Watermarks in PDF using C# GitHub demo") 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](https://www.syncfusion.com/pdf-framework/net/pdf-library "Syncfusion .NET 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](https://help.syncfusion.com/file-formats/pdf/working-with-watermarks "Working with Watermarks in .NET PDF Library"), 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](https://www.syncfusion.com/Account/Login?ReturnUrl=%2faccount%2fdownloads "Essential Studio License and Downloads page") page. If you are not a Syncfusion customer, try our 30-day [free trial](https://www.syncfusion.com/downloads "Free Evaluation of the Essential Studio Products") to check out our available features.

For questions, contact us through our [support forum](https://www.syncfusion.com/forums "Syncfusion Support Forum"), [support portal](https://support.syncfusion.com/ "Syncfusion Support Portal"), or [feedback portal](https://www.syncfusion.com/feedback/pdf "Syncfusion Feedback Portal"). We are delighted to assist you!

## Related blogs

- [How to Convert an Image to PDF in ASP.NET Core](https://www.syncfusion.com/blogs/post/how-to-convert-image-to-pdf-in-asp-net-core.aspx "Blog: How to Convert an Image to PDF in ASP.NET Core")
- [Easy Ways to Redact PDFs Using C#](https://www.syncfusion.com/blogs/post/easy-ways-to-redact-pdfs-using-c.aspx "Blog: Easy Ways to Redact PDFs Using C#")
- [7 Ways to Compress PDF Files in C#, VB.NET](https://www.syncfusion.com/blogs/post/7-ways-to-compress-pdf-files.aspx "Blog: 7 Ways to Compress PDF Files in C#, VB.NET")
- [Digitally Sign and Verify Signatures in PDF Files Using C#](https://www.syncfusion.com/blogs/post/create-validate-pdf-digital-signatures-csharp.aspx "Blog: Digitally Sign and Verify Signatures in PDF Files Using C#")
- [Optical Character Recognition in PDF Using Tesseract Open-Source Engine](https://www.syncfusion.com/blogs/post/optical-character-recognition-in-pdf-using-tesseract-open-source-engine.aspx "Blog: Optical Character Recognition in PDF Using Tesseract Open-Source Engine")