最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
使用C#编写代码替换PDF文件中的文本内容
时间:2026-05-31 09:30:01 编辑:袖梨 来源:一聚教程网
在日常文档处理中,PDF文本替换需求十分常见。无论是修正错误、更新信息还是满足合规要求,掌握高效替换技巧都能显著提升工作效率。

本文将详细讲解使用C#实现PDF文档文本替换的多种方法,包括基础替换和高级正则表达式匹配。
安装 PDF 处理库
开始前需为.NET项目添加PDF处理库引用。可直接从NuGet获取安装包,运行以下命令完成安装。
PM> Install-Package Spire.PDF
在 C# 中替换指定 PDF 页面中的文本
通过PDF处理库提供的方法,可精准替换页面中所有匹配文本。具体操作流程如下:
- 实例化
PdfDocument对象 - 载入目标PDF文件
- 选取特定页面
- 配置文本替换选项参数
- 初始化文本替换器并应用设置
- 执行文本替换操作
- 保存修改后的文档
完整示例代码如下:
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();
}
}
}
在 C# 中替换整个 PDF 文档中的文本
要实现全文档范围的文本替换,需遍历每个页面并逐一处理。主要步骤包括:
- 初始化文档对象
- 加载源文件
- 设置替换参数
- 循环处理各页面
- 创建替换器实例
- 执行替换操作
- 输出最终文档
完整示例代码如下:
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();
}
}
}
在 C# 中替换目标文本的首次出现内容
针对只需修改首次出现文本的场景,可调用ReplaceText()方法实现。操作流程如下:
- 创建文档实例
- 导入PDF文件
- 定位目标页面
- 配置替换参数
- 初始化替换工具
- 执行首次匹配替换
- 保存结果文档
完整示例代码如下:
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();
}
}
}
在 C# 中基于正则表达式替换 PDF 文本
正则表达式能实现更智能的文本匹配。通过设置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文档的智能化修改需求。