Skip to content

Instantly share code, notes, and snippets.

@GroupDocsGists
Last active April 9, 2022 12:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GroupDocsGists/4fa618502d15fc884614db7d7601ec53 to your computer and use it in GitHub Desktop.
Save GroupDocsGists/4fa618502d15fc884614db7d7601ec53 to your computer and use it in GitHub Desktop.
Remove watermarks from PDF File and other documents using C# .NET
// Remove the watermarks from PDF & other Documents using C#
using (Watermarker watermarker = new Watermarker("filepath/documentWithWatermarks.pdf"))
{
PossibleWatermarkCollection possibleWatermarks = watermarker.Search();
// Remove every watermark by mentioning the index within document.
for (int i = 0; i < possibleWatermarks.Count; i++)
{
possibleWatermarks.RemoveAt(i);
}
watermarker.Save("filepath/no-watermarks.pdf");
}
// Remove Hyperlink watermarks from PDF using C#
using (Watermarker watermarker = new Watermarker("path/Hyperlink-Watermarks.pdf"))
{
PossibleWatermarkCollection watermarks = watermarker.Search(new TextSearchCriteria(new Regex(@"someurl\.com")));
for (int i = 0 ; i < watermarks.Count; i++)
{
if (watermarks[i] is HyperlinkPossibleWatermark)
{
Console.WriteLine("Removing: " + watermarks[i].Text);
watermarks.RemoveAt(i);
}
}
watermarker.Save("path/no-hyperlink-watermarks.pdf");
}
// Remove the watermarks from PDF & other Documents using C#
using (Watermarker watermarker = new Watermarker("filepath/documentWithWatermarks.pdf"))
{
PossibleWatermarkCollection possibleWatermarks = watermarker.Search();
// Remove watermark at the specified index from the document.
possibleWatermarks.RemoveAt(0);
// Remove specified watermark from the document.
possibleWatermarks.Remove(possibleWatermarks[0]);
watermarker.Save("filepath/noWatermarks.pdf");
}
// Remove watermarks with specific text formatting from a PDF using C#
using (Watermarker watermarker = new Watermarker("path/Watermarks.pdf"))
{
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 possibleWatermarks = watermarker.Search(criteria);
possibleWatermarks.Clear();
watermarker.Save("path/removed-watermarks.pdf");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment