Skip to content

Instantly share code, notes, and snippets.

How to Remove Watermark from PDF using C#. For more details: https://kb.conholdate.com/total/net/how-to-remove-watermark-from-pdf-using-csharp/
using System;
using System.Collections.Generic;
using Aspose.Pdf;
namespace AsposeProjects
{
class Program
{
static void Main(string[] args) // Main function to remove watermark in C#
{
// Initialize license
License lic = new License();
lic.SetLicense("Aspose.Total.lic");
// Load the source PDF file with a watermark on it
Document document = new Document("watermark.pdf");
// Instantiate a list to save the watermark-type artifacts
List<Artifact> artifactsToBeDeleted = new List<Artifact>();
// Parse through all the pages of the loaded PDF file
foreach (var page in document.Pages)
{
// Parse through all the artifacts in the current page
foreach (var item in page.Artifacts)
{
// Check if the type of the current artifact is a watermark
if (item.Subtype == Artifact.ArtifactSubtype.Watermark)
{
// Save the artifact reference in the list for later deletion
artifactsToBeDeleted.Add(item);
}
}
// Parse through all the artifacts to be deleted
foreach (var item in artifactsToBeDeleted)
{
// Delete the current artifact
page.Artifacts.Delete(item);
}
}
// Save the resultant PDF file having no watermark in it
document.Save("withoutWatermark.pdf");
Console.WriteLine("Done");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment