使用“查找并替换”选项可以一次性替换文档中的特定文本。这样,您不必手动定位和更新整个文档中每次出现的文本。本文甚至更进一步,介绍了如何在PDF文档中自动查找和替换文本功能。特别是,将学习如何使用C#在整个PDF,特定页面或页面区域中查找和替换文本。

  • 使用C#查找和替换PDF中的文本
  • 查找和替换特定页面中的文本
  • 定义PDF页面区域以查找和替换文本
  • 使用正则表达式查找和替换PDF中的文本

.NET的Aspose.PDF是一个C#类库,为.NET应用程序提供基本以及高级的PDF操作功能。该API还允许您以不同的方式无缝地查找和替换PDF文档中的文本。(点击下载)


使用C#查找和替换PDF中的文本

以下是在PDF文档中查找和替换文本的步骤。

  • 使用Document类使用其路径加载PDF文档。
  • 创建TextFragmentAbsorber类的实例,并将搜索短语提供给其构造函数。
  • 使用Document.Pages.Accept(TextFragmentAbsorber)接受PDF所有页面的文本吸收器。
  • 将提取的文本片段获取到TextFragmentCollection对象中。
  • 遍历找到的TextFragmentCollection并替换每个片段中的文本。
  • 使用Document.Save(String)方法保存更新的PDF文档。

下面的代码示例演示如何使用C#查找和替换PDF中的文本。

// Open document
Document pdfDocument = new Document("Document.pdf");// Create TextAbsorber object to find all instances of the input search phrase
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("text");// Accept the absorber for all the pages
pdfDocument.Pages.Accept(textFragmentAbsorber);// Get the extracted text fragments
TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;// Loop through the fragments
foreach (TextFragment textFragment in textFragmentCollection)
{// Update text and other propertiestextFragment.Text = "TEXT";textFragment.TextState.Font = FontRepository.FindFont("Verdana");textFragment.TextState.FontSize = 22;textFragment.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Blue);textFragment.TextState.BackgroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Green);
}// Save resulting PDF document.
pdfDocument.Save("updated-document.pdf");

使用C#查找和替换特定页面中的文本

以下是在PDF文档的特定页面上查找和替换文本的步骤。

  • 使用Document类使用其路径加载PDF文档。
  • 创建TextFragmentAbsorber类的实例,并将搜索短语提供给其构造函数。
  • 使用Document.Pages [1] .Accept(TextFragmentAbsorber)接受所需页面的文本吸收器。
  • 遍历找到的TextFragmentAbsorber.TextFragments集合,并替换每个片段中的文本。
  • 使用Document.Save(String)方法保存更新的PDF文档。

以下代码示例显示了如何使用C#在PDF的特定页面中查找和替换文本。

// Open document
Document pdfDocument = new Document("Document.pdf");// Create TextAbsorber object to find all instances of the input search phrase
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("text");// Accept the absorber for desired
pdfDocument.Pages[1].Accept(textFragmentAbsorber);// Get the extracted text fragments
TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;// Loop through the fragments
foreach (TextFragment textFragment in textFragmentCollection)
{// Update text and other propertiestextFragment.Text = "TEXT";textFragment.TextState.Font = FontRepository.FindFont("Verdana");textFragment.TextState.FontSize = 22;textFragment.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Blue);textFragment.TextState.BackgroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Green);
}// Save resulting PDF document.
pdfDocument.Save("updated-document.pdf");

定义页面区域以查找和替换文本

还可以在PDF文档的页面特定区域中查找和替换文本。以下步骤显示了如何定义特定区域,然后替换其中的文本。

  • 使用Document类使用其路径加载PDF文档。
  • 创建TextFragmentAbsorber类的实例,并将搜索短语提供给其构造函数。
  • 使用Document.Pages [0] .Accept(TextFragmentAbsorber)接受所需页面的文本吸收器。
  • 使用Rectangle类定义页面区域。
  • 循环遍历TextFragmentAbsorber.TextFragments集合,并替换每个片段中的文本。
  • 使用Document.Save(String)方法保存更新的PDF文档。

下面的代码示例演示如何使用C#在PDF的特定页面区域中查找和替换文本。

// load PDF file
Document pdf = new Document("Document.pdf");// instantiate TextFragment Absorber object
TextFragmentAbsorber TextFragmentAbsorberAddress = new TextFragmentAbsorber();// search text within page bound
TextFragmentAbsorberAddress.TextSearchOptions.LimitToPageBounds = true;// specify the page region for TextSearch Options
TextFragmentAbsorberAddress.TextSearchOptions.Rectangle = new Rectangle(100, 100, 200, 200);// search text from first page of PDF file
pdf.Pages[1].Accept(TextFragmentAbsorberAddress);// iterate through individual TextFragment
foreach (TextFragment tf in TextFragmentAbsorberAddress.TextFragments)
{// update text to blank characterstf.Text = "";
}// save updated PDF file after text replace
pdf.Save("output.pdf");

使用正则表达式查找和替换PDF中的文本

也可以使用正则表达式来查找和替换与特定模式匹配的文本。为此,您只需要提供一个正则表达式即可代替普通搜索短语并使用TextSearchOptions。以下是执行此操作的步骤。

  • 使用Document类使用其路径加载PDF文档。
  • 创建TextFragmentAbsorber类的实例,并将搜索短语提供给其构造函数。
  • 创建TextSearchOptions类的实例,然后将true传递给其构造函数以启用基于正则表达式的搜索。
  • 分配TextSearchOptions对象TextFragmentAbsorber.TextSearchOptions财产。
  • 使用Document.Pages [0] .Accept(TextFragmentAbsorber)接受所需页面的文本吸收器。
  • 使用Rectangle类定义页面区域。
  • 循环遍历TextFragmentAbsorber.TextFragments集合,并替换每个片段中的文本。
  • 使用Document.Save(String)方法保存更新的PDF文档。

下面的代码示例演示如何使用C#使用正则表达式查找和替换PDF中的文本。

// Open document
Document pdfDocument = new Document("Document.pdf");// Create TextAbsorber object to find all the phrases matching the regular expression
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("\\d{4}-\\d{4}"); // Like 1999-2000// Set text search option to specify regular expression usage
TextSearchOptions textSearchOptions = new TextSearchOptions(true);
textFragmentAbsorber.TextSearchOptions = textSearchOptions;// Accept the absorber for a single page
pdfDocument.Pages[1].Accept(textFragmentAbsorber);// Get the extracted text fragments
TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;// Loop through the fragments
foreach (TextFragment textFragment in textFragmentCollection)
{// Update text and other propertiestextFragment.Text = "New Phrase";// Set to an instance of an object.textFragment.TextState.Font = FontRepository.FindFont("Verdana");textFragment.TextState.FontSize = 22;textFragment.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Blue);textFragment.TextState.BackgroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Green);
}// Save PDF
pdfDocument.Save("output.pdf");

如果您有任何疑问或需求,请随时加入Aspose技术交流群(761297826),我们很高兴为您提供查询和咨询。

1分钟教你快速使用C#查找和替换PDF文件中的文本相关推荐

  1. python docx 替换文字_查找并替换.docx文件中的文本 - Python

    我一直在寻找一种方法来查找和替换docx文件中的文本而运气不佳 . 我已经尝试过docx模块而无法使用它 . 最后,我使用zipfile模块计算了下面描述的方法,并替换了docx存档中的documen ...

  2. 查找多个 PDF 文件中的关键字并替换为其它文字

    概要:在一个文件中查找并替换关键字的场景下,我们都知道怎么去操作,我们直接按住Ctrl+F5或者直接按软件界面上的查找按钮,并且替换就可以了.但是如果我们需要一次性的查找并替换PDF文件中的多个关键字 ...

  3. 手把手教你如何用Python从PDF文件中导出数据(附链接)

    作者:Mike Driscoll :翻译:季洋:校对:丁楠雅 本文约4000字,建议阅读10分钟. 本文介绍了在提取出想要的数据之后,如何将数据导出成其他格式的方法. 有很多时候你会想用Python从 ...

  4. 梦幻西游三维获取服务器信息,梦幻西游三维版:潜能果上线后经验紧缺?五分钟教你快速获得经验...

    原标题:梦幻西游三维版:潜能果上线后经验紧缺?五分钟教你快速获得经验 目前<梦幻西游三维版>服务器等级最高在89级,其实从80级到89级需要不少的时间,再加上新上线了"潜能果&q ...

  5. php对pdf关键字定位,如何在PDF文件中快速查找关键字

    有时候我们在阅读和编辑PDF文章时需要对PDF文件里的重要关键词进行查找,但如果PDF文章内容过长,怎么才能快速查找出想要查找的关键字? 想要在PDF文件中快速查找出关键字,那么利用迅捷PDF编辑器进 ...

  6. html实现文本的查找与替换,在 InDesign 中查找并替换文本

    在对文档进行修订时,"查找并替换"操作将很有用.您可以全局查找并替换文本.对象.字体.字形和颜色. "查找/更改"对话框 A. 查找/更改选项卡 B. &quo ...

  7. 查找并删除EXCEL文件中的重复行(整行重复)

    ''' 用Python写代码:查找并删除EXCEL文件中的重复行(整行重复) --GhatGPT方案 ''' #-------------------------------------------- ...

  8. 如何快速在PDF文件中插入图片

    在 PDF文件中插入图片我优先想到了 Adobe Acrobat DC,胜任此项工作完全 OK.但是有个问题,Acrobat 会自动识别 PDF 中的文字.如果有手写字迹经过 Acrobat 识别再保 ...

  9. 怎么翻译PDF文件内容?教你一招轻松翻译整篇PDF文件

    怎么把PDF文件中的内容给翻译成其他语言呢?PDF是很多人在办公或学习中必不可少的文件格式,但当我们收到或者是下载到一份不怎么认识的PDF文件时,想要知道文件具体的内容,我们就需要将文件进行翻译,那么 ...

  10. 教你如何简单压缩过大的PDF文件

    PDF文件现在广泛应用于各大办公场所,只要有办公的地方就可以看到PDF文件,文件都有大小之分,PDF文件也不例外,遇到过大的文件该怎么呢,小编教你如何简单压缩过大的PDF文件! 操作方法要借助压缩软件 ...

最新文章

  1. SAP RETAIL 通过自动补货功能触发采购申请
  2. 20145234黄斐《网络对抗技术》实验八、Web基础
  3. 来自Riot 的一份游戏美术教程(五):技术美术
  4. 36、重分布配置实验之route-map
  5. RocketMQ消息存储的整体结构
  6. c++ int8_t转int_Python 90行代码让微信地球转起来,你也可以!| 原力计划
  7. 11个实用的CSS学习工具
  8. 如何确定你的伴侣真的爱你?复杂数学公式告诉你
  9. dojo调用php,dojo学习第一天 Tab选项卡 实现_dojo
  10. 设计模式 - Visitor 模式(访问者模式)
  11. matlab把图例放在左边,如何将图例放在p之外
  12. pyton3 with异常
  13. 兮°Android下的屏幕适配问题的一点心得
  14. JVM——深入理解类加载器
  15. html5是什么意思,html5是什么意思?
  16. linux常用命令大全(linux基础命令入门到精通+实例讲解+持续更新+命令备忘录+面试复习)
  17. 深度学习 | BN层原理浅谈
  18. 让子弹飞 | 院士深度解析Alphafold DB的未来影响
  19. 这几款摸鱼神器,让我惊了!
  20. 什么是文件包含漏洞?文件包含漏洞分类!

热门文章

  1. hive 漫威the_漫威系列的观看顺序
  2. iwork8旗舰版 android,性能彪悍 双面神兽——酷比魔方iwork8旗舰版体验
  3. 哈尔滨华夏计算机学院分数线,黑龙江2008年高职(专科)批录取院校录取最低分数线(九)...
  4. Eureka高可用注册中心通过defaultZone深入理解zone和serviceUrl
  5. python模拟支付宝扫码登录_Python爬虫模拟登录支付宝并获取订单信息
  6. ubuntu下终于安装好了nvidia的gt540显卡驱动
  7. 愚人节将至,怎么恶搞最过瘾
  8. Android Serach框架使详解
  9. PDF文件如何旋转页面保存
  10. 【C语言】用C语言输出菱形