人们常常需要出于各种原因替换 PDF 文档中的文本。这可能是为了纠正错误或打字错误,更新过时的信息,为特定的受众个性化定制内容,或者为符合法律或监管变化要求。通过在 PDF 中替换文本,可以保持文档的完整性,并提高所呈现信息的整体质量和相关性。在本文中,你将学习如何使用 Spire.PDF for .NET 在 PDF 文档中查找替换文本。
安装 Spire.PDF for .NET
首先,您需要添加 Spire.PDF for .NET 包中包含的 DLL 文件作为 .NET 项目中的引用。DLL 文件可以从此链接下载或通过 NuGet 安装。
PM> Install-Package Spire.PDF
替换特定 PDF 页面的文本
Spire.PDF for .NET 提供了 PdfTextReplacer.ReplaceAllText() 方法,允许用户将页面中所有目标文本替换为新文本。以下是使用 C# 在特定页面替换文本的步骤:
- 创建 PdfDocument 对象。
 - 使用 PdfDocument.LoadFromFile() 方法加载 PDF 文件。
 - 通过 PdfDocument.Pages[index] 获取文档特定的页面。
 - 基于页面创建PdfTextReplacer 对象。
 - 创建 PdfTextReplaceOptions 对象,并使用 PdfTextReplaceOptions.ReplaceActionType 属性指定替换选项。
 - 使用 PdfTextReplacer.ReplaceAllText(PdfTextReplaceOptions) 方法将页面中所有目标文本的出现替换为新文本。
 - 保存 PDF 文件中。
 
- C#
 
using Spire.Pdf;
using Spire.Pdf.Texts;
namespace ReplaceTextInPage
{
	class Program
	{
		static void Main(string[] args)
		{
			// 创建PdfDocument 对象
			PdfDocument doc = new PdfDocument();
			// 加载PDF 文件
			doc.LoadFromFile("in.pdf");
			// 获取特定的页面
			PdfPageBase page = doc.Pages[0];
			// 根据页面创建 PdfTextReplacer 对象
			PdfTextReplacer textReplacer = new PdfTextReplacer(page);
			// 创建PdfTextReplaceOptions 对象
			PdfTextReplaceOptions textReplaceOptions = new PdfTextReplaceOptions();
			// 指定文本替换的选项
			textReplaceOptions.ReplaceType = PdfTextReplaceOptions.ReplaceActionType.WholeWord;
			textReplaceOptions.ReplaceType = PdfTextReplaceOptions.ReplaceActionType.AutofitWidth;
			textReplacer.Options = textReplaceOptions;
			// 将所有目标文本的出现替换为新文本
			textReplacer.ReplaceAllText("文档", "文件");
			// 将文档保存到另一个 PDF 文件中
			doc.SaveToFile("out.pdf");
			// 释放资源
			doc.Dispose();
		}
	}
}
替换整个 PDF 文档匹配的文本
上面介绍了如何在单页中替换文本。要在整个 PDF 文档中将所有特定文本替换为新文本,只需遍历文档的每一页,具体实现如下:
- 创建 PdfDocument 对象。
 - 使用 PdfDocument.LoadFromFile() 方法加载 PDF 文件。
 - 创建 PdfTextReplaceOptions 对象,并使用 PdfTextReplaceOptions.ReplaceActionType 属性指定替换选项。
 - 遍历文档所有页面并,通过 PdfDocument.Pages[index] 获取特定页面。
 - 基于页面创建 PdfTextReplacer 对象。
 - 使用 PdfTextReplacer.ReplaceAllText(PdfTextReplaceOptions) 方法将目标文本的出现替换为新文本。
 - 保存 PDF 文件中。
 
- C#
 
using Spire.Pdf;
using Spire.Pdf.Texts;
namespace ReplaceTextInDocument
{
	class Program
	{
		static void Main(string[] args)
		{
			// 创建PdfDocument 对象
			PdfDocument doc = new PdfDocument();
			// 加载PDF 文件
			doc.LoadFromFile("in.pdf");
			// 创建PdfTextReplaceOptions 对象
			PdfTextReplaceOptions textReplaceOptions = new PdfTextReplaceOptions();
			// 指定文本替换的选项
			textReplaceOptions.ReplaceType = PdfTextReplaceOptions.ReplaceActionType.WholeWord;
			textReplaceOptions.ReplaceType = PdfTextReplaceOptions.ReplaceActionType.AutofitWidth;
			//遍历PDF文档所有页面
			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("文档", "文件");
			}
			// 将文档保存到另一个 PDF 文件中
			doc.SaveToFile("out.pdf");
			// 释放资源
			doc.Dispose();
		}
	}
}

替换第一个匹配的文本
如需替换 PDF 第一个匹配的目标文本,使用 PdfTextReplacer.replaceText() 方法。具体参考以下步骤:
- 创建 PdfDocument 对象。
 - 使用 PdfDocument.LoadFromFile() 方法加载 PDF 文件。
 - 创建 PdfTextReplaceOptions 对象,并使用 PdfTextReplaceOptions.ReplaceActionType 属性指定替换选项。
 - 通过 PdfDocument.Pages[index] 获取特定页面。
 - 基于页面创建 PdfTextReplacer 对象。
 - 使用 PdfTextReplacer.ReplaceText(PdfTextReplaceOptions) 方法将第一个目标文本的出现替换为新文本。
 - 保存 PDF 文件中。
 
- C#
 
using Spire.Pdf;
using Spire.Pdf.Texts;
namespace ReplaceFirstOccurance
{
    class Program
    {
        static void Main(string[] args)
        {
            // 创建PdfDocument 对象
            PdfDocument doc = new PdfDocument();
            // 加载PDF 文件
            doc.LoadFromFile("in.pdf");
            // 创建PdfTextReplaceOptions 对象
            PdfTextReplaceOptions textReplaceOptions = new PdfTextReplaceOptions();
            // 指定文本替换的选项
            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.ReplaceText("文档", "文件");
            // 将文档保存到另一个 PDF 文件中
            doc.SaveToFile("out.pdf");
            // 释放资源
            doc.Dispose();
        }
    }
}

基于正则表达式替换文本
正则表达式是一种非常强大和灵活的模式,通常用于匹配文本。在 Spire.PDF for .NET 中,同样可以利用正则表达式在 PDF 文档页面中匹配特定文本并替换为新文本,步骤如下:
- 创建 PdfDocument 对象。
 - 使用 PdfDocument.LoadFromFile() 方法加载 PDF 文件。
 - 创建 PdfTextReplaceOptions 对象,并使用 PdfTextReplaceOptions.ReplaceActionType 属性指定正则类型的文本替换选项。
 - 通过 PdfDocument.Pages[index] 获取特定页面。
 - 基于页面创建 PdfTextReplacer 对象。
 - 使用 PdfTextReplacer.ReplaceAllText(PdfTextReplaceOptions) 方法将正则匹配的目标文本替换为新文本。
 - 保存 PDF 文件中。
 
- C#
 
using Spire.Pdf;
using Spire.Pdf.Texts;
namespace ReplaceUsingRegularExpression
{
    class Program
    {
        static void Main(string[] args)
        {
            // 创建PdfDocument 对象
            PdfDocument doc = new PdfDocument();
            // 加载PDF 文件
            doc.LoadFromFile("in.pdf");
            // 创建PdfTextReplaceOptions 对象
            PdfTextReplaceOptions textReplaceOptions = new PdfTextReplaceOptions();
            // 指定正则类型的文本替换选项
            textReplaceOptions.ReplaceType = PdfTextReplaceOptions.ReplaceActionType.Regex;
            //获取第一页
            PdfPageBase page = doc.Pages[0];
            // 根据页面创建PdfTextReplacer 对象
            PdfTextReplacer textReplacer = new PdfTextReplacer(page);
            textReplacer.Options = textReplaceOptions;
            // 定义正则表达式
            string regularExpression = "安.性";
            // 使用正则表达式匹配并替换为新文本
            textReplacer.ReplaceAllText(regularExpression, "安全度");
            // 将文档保存到另一个PDF 文件中
            doc.SaveToFile("out.pdf");
            // 释放资源
            doc.Dispose();
        }
    }
}

申请临时 License
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。
    


					



