如何使用 C# 从 PDF 中删除水印

本简短指南介绍了如何使用 C#PDF 中删除水印。它包含建立环境所需的所有资源、编写应用程序的分步过程,以及用于使用 C# 从 PDF 中删除水印文本的可运行示例代码。您还将学习在页面上使用不同类型的工件并过滤所需类型的工件。

使用 C# 从 PDF 中删除水印的步骤

  1. 建立环境以将 Aspose.PDF for .NET 添加到您的项目中以去除水印
  2. 将源 PDF 文件加载到 Document 类对象中以删除水印
  3. 解析文档每一页上的所有 artifacts 并创建一个水印类型工件列表
  4. 使用所需的工件填充列表后,删除所有水印
  5. 去除水印后保存生成的 PDF 文件

这些步骤通过识别编写此应用程序时所需的重要资源、类、方法和属性来解释使用 C#* 从 PDF 文档中删除水印的过程。在此过程中,源 PDF 文件被加载到 Document 类对象中,然后解析其所有页面。对于每个页面,都会检查其工件的类型水印并保存在列表中以便在最后删除。

使用 C# 从 PDF 中删除水印的代码

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");
}
}
}

此代码演示了使用 C#* 在 PDF 中编写*水印去除器的过程。它使用 Page.Artifacts 集合通过将 Subtype 属性与枚举值 Artifact.ArtifactSubtype.Watermark 进行比较来识别 Watermark 类型的工件。一旦将所有水印工件收集到一个列表中,就会对该列表进行解析,并分别从每个页面中删除相应的水印。

本快速教程指导我们使用 C#* 从 PDF 中擦除水印。如果您想了解添加水印的过程,请参阅 如何在 C# 中为 PDF 添加水印 上的文章。

 简体中文