How To Remove Watermark from PDF Documents using C#

Remove Watermark from PDF in C# – If you are looking to create an application to delete watermark from a PDF document. This article is a good learning source for C# programming developers.

Significance of Removing Watermarks from PDF documents

Remove Watermark from PDF

Watermark importance is very high for all those who intend to ascertain their copyrights if in case their data gets copied somewhere. The information contained within the watermark designates the authority of the owner and prevents other users from deleting the copyright. But at times, it really becomes a troublesome for many users out there as you need the information on an urgent basis.

Although, a variety of utilities, paid or free online tools might be available to get your problem resolved. The following section shows how C# Programming developers can do it through their coding editors.

Remove Watermark using C#

The GroupDocs.Watermark is an API to perform watermarking operations on the images or the documents of different file formats. If you are making a watermark remover app, It provides you some useful ways to:

Let’s learn how a C# developer can remove watermarks from PDF using GroupDocs.Watermark for .NET API.

Remove all occurrences of Watermarks from a PDF document using C#

The API provides you the ability to easily find and remove a particular watermark from a document. Following code serves this purpose.

// C# code to remove any existence of watermark from PDF
// Constants.InDocumentPdf is an absolute or relative path to your document. Ex: @"C:\Docs\document.pdf"
using (Watermarker watermarker = new Watermarker(Constants.InDocumentPdf))
{
    PossibleWatermarkCollection possibleWatermarks = watermarker.Search(); 
    // Remove watermark at the specified index from the document.
    possibleWatermarks.RemoveAt(0); 
    // Remove possible watermarks from the document.
    possibleWatermarks.Remove(possibleWatermarks[0]);
 
    watermarker.Save(Constants.OutDocumentPdf);
}

Remove Watermark with particular Text Formatting using C#

Using API you can search and remove the watermarks on the basis of text formatting. You can provide a search criterion containing name, font, size, color, etc and the API will find the watermarks with matching properties. The following code snippet demonstrates how to search and remove watermarks with a specific text formatting.

// C# code to remove watermark existence with particular text formatting.
// Constants.InDocumentPdf is an absolute or relative path to your document. Ex: @"C:\Docs\document.pdf"
using (Watermarker watermarker = new Watermarker(Constants.InDocumentPdf))
{
    TextFormattingSearchCriteria criteria = new TextFormattingSearchCriteria();
    criteria.ForegroundColorRange = new ColorRange();
    criteria.ForegroundColorRange.MinHue = -5;
    criteria.ForegroundColorRange.MaxHue = 10;
    criteria.ForegroundColorRange.MinBrightness = 0.01f;
    criteria.ForegroundColorRange.MaxBrightness = 0.99f;
    criteria.BackgroundColorRange = new ColorRange();
    criteria.BackgroundColorRange.IsEmpty = true;
    criteria.FontName = "Arial";
    criteria.MinFontSize = 19;
    criteria.MaxFontSize = 42;
    criteria.FontBold = true;
 
    PossibleWatermarkCollection watermarks = watermarker.Search(criteria);
    watermarks.Clear(); 
    watermarker.Save(Constants.OutDocumentPdf);
}

Hyperlink Watermark Remover in .NET

The document watermarking .NET API allows you to search and remove hyperlinks in a document of any supported format. The following code sample shows how to find and remove hyperlinks with a particular URL from a PDF document using C#.

// C# code to remove hyperlink watermarks
// Constants.InDocumentPdf is an absolute or relative path to your document. Ex: @"C:\Docs\document.pdf"
using (Watermarker watermarker = new Watermarker(Constants.InDocumentPdf))
{
    PossibleWatermarkCollection watermarks = watermarker.Search(new TextSearchCriteria(new Regex(@"someurl\.com")));
    for (int i = watermarks.Count - 1; i >= 0; i--)
    {
        // Ensure that only hyperlinks will be removed.
        if (watermarks[i] is HyperlinkPossibleWatermark)
        {
            // Output the full URL of the hyperlink
            Console.WriteLine(watermarks[i].Text); 
            // Remove hyperlink from the document
            watermarks.RemoveAt(i);
        }
    }
    watermarker.Save(Constants.OutDocumentPdf);
}

The complete code sample is available on GitHub.