Created
March 18, 2024 08:54
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/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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