本文介绍如何使用 C# 从 PDF 中删除背景。它包含设置开发环境的详细信息、编写应用程序的步骤列表以及使用 C# 开发 PDF 文件背景删除器的示例代码。您将学习如何删除页面上的背景和其他伪像。
使用 C# 删除 PDF 背景的步骤
- 设置 IDE 使用 Aspose.PDF for .NET 删除背景
- 使用 Document 类加载源 PDF 文件以清除背景
- 选择具有背景图像的目标页面
- 遍历选定页面上的所有工件
- 选择 ArtifactSubtype.Background 类型的工件
- 调用背景伪像的 Delete 方法将其删除
- Save 输出 PDF 文件
这些步骤总结了如何使用 C# 清除 PDF 背景。将目标 PDF 文件加载到 Document 对象中,选择所需页面,然后遍历其中的所有工件以搜索背景工件。调用 Artifacts 集合类中的 Delete() 方法删除背景并保存输出 PDF 文件。
使用 C# 清除 PDF 背景的代码
using Aspose.Pdf; | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
License lic = new License(); | |
lic.SetLicense("license.lic"); | |
// Open the PDF file | |
var doc = new Document("BackGround.pdf"); | |
// Access the target page | |
var page = doc.Pages[1]; | |
// Iterate through all the artifacts in the page | |
foreach(var fact in page.Artifacts) | |
{ | |
// Search background artifact | |
if(fact.Subtype == Artifact.ArtifactSubtype.Background) | |
{ | |
// Delete the background | |
page.Artifacts.Delete(fact); | |
} | |
} | |
// Save the output after removing background | |
doc.Save("output.pdf"); | |
System.Console.WriteLine("Background removed successfully"); | |
} | |
} |
此代码演示了如何使用 C# 从 PDF 文件中删除背景。您可以使用 ArtifactSubtype 枚举器删除页眉/页脚和水印。要从整个 PDF 中消除背景,请在文档的每一页上执行此步骤。
本快速教程教我们如何开发 PDF 背景橡皮擦。如果您想要在 PDF 文件中拼版,请参阅 使用 C# 拼合 PDF 上的文章。