在日常文档处理中,PDF文本替换需求十分常见。无论是修正错误、更新信息还是满足合规要求,掌握高效替换技巧都能显著提升工作效率。

本文将详细讲解使用C#实现PDF文档文本替换的多种方法,包括基础替换和高级正则表达式匹配。
开始前需为.NET项目添加PDF处理库引用。可直接从NuGet获取安装包,运行以下命令完成安装。
PM> Install-Package Spire.PDF
通过PDF处理库提供的方法,可精准替换页面中所有匹配文本。具体操作流程如下:
PdfDocument对象完整示例代码如下:
using Spire.Pdf;
using Spire.Pdf.Texts;
namespace ReplaceTextInPage
{
class Program
{
static void Main(string[] args)
{
// 创建 PdfDocument 对象
PdfDocument doc = new PdfDocument();
// 加载 PDF 文件
doc.LoadFromFile("C:UsersAdministratorDesktopInput.pdf");
// 创建 PdfTextReplaceOptions 对象
PdfTextReplaceOptions textReplaceOptions = new PdfTextReplaceOptions();
// 设置文本替换选项
textReplaceOptions.ReplaceType = PdfTextReplaceOptions.ReplaceActionType.IgnoreCase;
textReplaceOptions.ReplaceType = PdfTextReplaceOptions.ReplaceActionType.WholeWord;
textReplaceOptions.ReplaceType = PdfTextReplaceOptions.ReplaceActionType.AutofitWidth;
// 获取指定页面
PdfPageBase page = doc.Pages[0];
// 基于页面创建 PdfTextReplacer 对象
PdfTextReplacer textReplacer = new PdfTextReplacer(page);
// 应用替换选项
textReplacer.Options = textReplaceOptions;
// 将所有目标文本替换为新文本
textReplacer.ReplaceAllText(".NET Framework", "New Content");
// 将文档保存为新的 PDF 文件
doc.SaveToFile("ReplaceTextInPage.pdf");
// 释放资源
doc.Dispose();
}
}
}
要实现全文档范围的文本替换,需遍历每个页面并逐一处理。主要步骤包括:
完整示例代码如下:
using Spire.Pdf;
using Spire.Pdf.Texts;
namespace ReplaceInEntireDocument
{
class Program
{
static void Main(string[] args)
{
// 创建 PdfDocument 对象
PdfDocument doc = new PdfDocument();
// 加载 PDF 文件
doc.LoadFromFile("C:UsersAdministratorDesktopInput.pdf");
// 创建 PdfTextReplaceOptions 对象
PdfTextReplaceOptions textReplaceOptions = new PdfTextReplaceOptions();
// 设置文本替换选项
textReplaceOptions.ReplaceType = PdfTextReplaceOptions.ReplaceActionType.IgnoreCase;
textReplaceOptions.ReplaceType = PdfTextReplaceOptions.ReplaceActionType.WholeWord;
textReplaceOptions.ReplaceType = PdfTextReplaceOptions.ReplaceActionType.AutofitWidth;
for (int i = 0; i < doc.Pages.Count; i++)
{
// 获取当前页面
PdfPageBase page = doc.Pages[i];
// 基于页面创建 PdfTextReplacer 对象
PdfTextReplacer textReplacer = new PdfTextReplacer(page);
// 应用替换选项
textReplacer.Options = textReplaceOptions;
// 将所有目标文本替换为新文本
textReplacer.ReplaceAllText(".NET Framework", "New Content");
}
// 将文档保存为新的 PDF 文件
doc.SaveToFile("ReplaceTextInDocument.pdf");
// 释放资源
doc.Dispose();
}
}
}
针对只需修改首次出现文本的场景,可调用ReplaceText()方法实现。操作流程如下:
完整示例代码如下:
using Spire.Pdf;
using Spire.Pdf.Texts;
namespace ReplaceFirstOccurance
{
class Program
{
static void Main(string[] args)
{
// 创建 PdfDocument 对象
PdfDocument doc = new PdfDocument();
// 加载 PDF 文件
doc.LoadFromFile("C:UsersAdministratorDesktopInput.pdf");
// 创建 PdfTextReplaceOptions 对象
PdfTextReplaceOptions textReplaceOptions = new PdfTextReplaceOptions();
// 设置文本替换选项
textReplaceOptions.ReplaceType = PdfTextReplaceOptions.ReplaceActionType.IgnoreCase;
textReplaceOptions.ReplaceType = PdfTextReplaceOptions.ReplaceActionType.WholeWord;
textReplaceOptions.ReplaceType = PdfTextReplaceOptions.ReplaceActionType.AutofitWidth;
// 获取指定页面
PdfPageBase page = doc.Pages[1];
// 基于页面创建 PdfTextReplacer 对象
PdfTextReplacer textReplacer = new PdfTextReplacer(page);
// 应用替换选项
textReplacer.Options = textReplaceOptions;
// 替换目标文本首次出现的内容
textReplacer.ReplaceText(".NET Framework", "New Content");
// 将文档保存为新的 PDF 文件
doc.SaveToFile("ReplaceFirstOccurance.pdf");
// 释放资源
doc.Dispose();
}
}
}
正则表达式能实现更智能的文本匹配。通过设置Regex替换模式,可处理符合特定规则的文本内容。
正则替换的关键步骤:
完整示例代码如下:
using Spire.Pdf;
using Spire.Pdf.Texts;
namespace ReplaceUsingRegularExpression
{
class Program
{
static void Main(string[] args)
{
// 创建 PdfDocument 对象
PdfDocument doc = new PdfDocument();
// 加载 PDF 文件
doc.LoadFromFile("C:UsersAdministratorDesktopInput.pdf");
// 创建 PdfTextReplaceOptions 对象
PdfTextReplaceOptions textReplaceOptions = new PdfTextReplaceOptions();
// 将替换类型设置为 Regex
textReplaceOptions.ReplaceType = PdfTextReplaceOptions.ReplaceActionType.Regex;
// 获取指定页面
PdfPageBase page = doc.Pages[1];
// 基于页面创建 PdfTextReplacer 对象
PdfTextReplacer textReplacer = new PdfTextReplacer(page);
// 应用替换选项
textReplacer.Options = textReplaceOptions;
// 指定正则表达式
string regularExpression = @"bCw*?Rb";
// 将所有匹配正则表达式的内容替换为新文本
textReplacer.ReplaceAllText(regularExpression, "NEW");
// 将文档保存为新的 PDF 文件
doc.SaveToFile("ReplaceWithRegularExpression.pdf");
// 释放资源
doc.Dispose();
}
}
}
本文系统讲解了C#处理PDF文本替换的多种方案,从基础全文替换到高级正则匹配,每种方法都配有完整代码示例,帮助开发者快速实现PDF文档的智能化修改需求。