商城首页欢迎来到中国正版软件门户

您的位置: 首页 > 文章列表 > 编程开发 > C#代码实现替换PDF文档中的文本

C#代码实现替换PDF文档中的文本

  发布于2026-05-30 阅读(0)

扫一扫,手机访问

先来聊聊一个很常见的场景:为什么要替换 PDF 文档里的文本?原因其实挺多的——修正拼写错误、更新那些已经过时的信息、针对不同的受众调整内容,或者满足某些法律与合规层面的要求。本质上,替换 PDF 中的文本,就是在提升内容的准确性、保持文档的一致性,让信息真正变得好用、好读。

C#代码实现替换PDF文档中的文本

这篇文章就来聊聊,用 C# 如何搞定这个事儿。

安装 PDF 处理库

在开始写代码之前,先得把“家伙”请进项目。你可以在 .NET 项目中手动添加对应的 DLL 文件引用,也可以直接用 NuGet 装一个,省心省力。

PM> Install-Package Spire.PDF

在 C# 中替换指定 PDF 页面中的文本

大多数 PDF 处理库都内置了文本替换方法,可以把某个页面里所有匹配的目标文本,一把换成你想要的新内容。实现起来很直接:

  1. 新建一个 PdfDocument 对象。
  2. 把目标 PDF 文件加载进来。
  3. 从文档中取出你想动刀的那个页面。
  4. 创建一个文本替换选项对象,通过它的属性来设定替换的规则(比如要不要忽略大小写、是不是匹配整个单词)。
  5. 再弄一个文本替换器对象,把刚才设好的选项应用上去。
  6. 调用替换方法,页面里所有符合条件的文本就会被替换成新文本。
  7. 最后把修改完的文档,另存为一个新 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:\Users\Administrator\Desktop\Input.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.Sa veToFile("ReplaceTextInPage.pdf");

            // 释放资源
            doc.Dispose();
        }
    }
}

在 C# 中替换整个 PDF 文档中的文本

如果要把整份文档所有页面的某个词都替换掉,也没什么特别的黑科技——只需要遍历文档里的每一页,对每个页面重复执行替换操作。

基本流程如下:

  1. 创建 PdfDocument 对象并加载文件。
  2. 设定好替换选项。
  3. 遍历所有页面,每一页都创建替换器,应用选项,执行替换。
  4. 保存新文档。

代码实现是这样的:

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:\Users\Administrator\Desktop\Input.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.Sa veToFile("ReplaceTextInDocument.pdf");

            // 释放资源
            doc.Dispose();
        }
    }
}

在 C# 中替换目标文本的首次出现内容

有时候,你并不想把所有匹配的文本都换掉,只想替换第一次出现的那一个。对应的,就用 PdfTextReplacer 类的 ReplaceText() 方法。

步骤和单页替换差不多,只是调用方法不一样:

  1. 加载文档,拿到指定页面。
  2. 设定替换选项。
  3. 创建替换器,应用选项。
  4. 调用 ReplaceText() 方法,替换首次匹配的文本。
  5. 保存文件。

示例代码:

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:\Users\Administrator\Desktop\Input.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.Sa veToFile("ReplaceFirstOccurance.pdf");

            // 释放资源
            doc.Dispose();
        }
    }
}

在 C# 中基于正则表达式替换 PDF 文本

接下来这个玩法,可能让很多朋友眼前一亮——正则表达式。正则表达式这玩意儿,熟悉的朋友都知道,它就像一把精密的瑞士军刀,能帮你匹配和操作那些符合特定模式的文本。借助 PDF 处理库,我们可以直接拿正则表达式在 PDF 里做搜索替换。

实现步骤也很清晰:

  1. 加载文档,获取指定页面。
  2. 创建 PdfTextReplaceOptions 对象。
  3. 把替换类型设为 Regex,打开正则匹配开关。
  4. 创建文本替换器并应用选项。
  5. 调用 ReplaceAllText() 方法,传入正则表达式和新文本内容。
  6. 保存结果。

实际操作长这样:

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:\Users\Administrator\Desktop\Input.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.Sa veToFile("ReplaceWithRegularExpression.pdf");

            // 释放资源
            doc.Dispose();
        }
    }
}

总结

这篇文章主要介绍了在 C# 中替换 PDF 文本的几种常见套路:按页面替换、整个文档替换、只替换首次出现的内容,以及使用正则表达式进行更灵活的匹配替换。其实核心思路都一样——加载文档,设置替换选项,创建替换器,执行替换,保存文件。

特别是正则表达式的方式,它让你不再局限于固定的字符串查找,而是可以按某种模式去匹配内容。程序会扫描指定的 PDF 页面,把符合规则的文本统统找出来,再替换成你指定的新内容。这种基于正则表达式的玩法,在处理批量修改、格式清理这类任务时,优势就体现出来了。

本文转载于:https://www.jb51.net/program/364830gh4.htm 如有侵犯,请联系zhengruancom@outlook.com删除。
免责声明:正软商城发布此文仅为传递信息,不代表正软商城认同其观点或证实其描述。

热门关注