- 使用C#查找和替换PDF中的文本
- 查找和替换特定页面中的文本
- 定义PDF页面区域以查找和替换文本
- 使用正则表达式查找和替换PDF中的文本
.NET的
17周年庆来啦!整合所有格式API处理控件Aspose.Total永久授权火热促销中,新购乐享85折起!联系客服立马1分钟了解全部!
使用C#查找和替换PDF中的文本
以下是在PDF文档中查找和替换文本的步骤。
- 使用Document类使用其路径加载PDF文档。
- 创建TextFragmentAbsorber类的实例,并将搜索短语提供给其构造函数。
- 使用Document.Pages.Accept(TextFragmentAbsorber)接受PDF所有页面的文本吸收器。
- 将提取的文本片段获取到TextFragmentCollection对象中。
- 遍历找到的TextFragmentCollection并替换每个片段中的文本。
- 使用Document.Save(String)方法保存更新的PDF文档。
下面的代码示例演示如何使用C#查找和替换PDF中的文本。
// Open documentDocument pdfDocument = new Document("Document.pdf");// Create TextAbsorber object to find all instances of the input search phraseTextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("text");// Accept the absorber for all the pagespdfDocument.Pages.Accept(textFragmentAbsorber);// Get the extracted text fragmentsTextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;// Loop through the fragmentsforeach (TextFragment textFragment in textFragmentCollection){ // Update text and other properties textFragment.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 documentDocument pdfDocument = new Document("Document.pdf");// Create TextAbsorber object to find all instances of the input search phraseTextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("text");// Accept the absorber for desiredpdfDocument.Pages[1].Accept(textFragmentAbsorber);// Get the extracted text fragmentsTextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;// Loop through the fragmentsforeach (TextFragment textFragment in textFragmentCollection){ // Update text and other properties textFragment.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 fileDocument pdf = new Document("Document.pdf");// instantiate TextFragment Absorber objectTextFragmentAbsorber TextFragmentAbsorberAddress = new TextFragmentAbsorber();// search text within page boundTextFragmentAbsorberAddress.TextSearchOptions.LimitToPageBounds = true;// specify the page region for TextSearch OptionsTextFragmentAbsorberAddress.TextSearchOptions.Rectangle = new Rectangle(100, 100, 200, 200);// search text from first page of PDF filepdf.Pages[1].Accept(TextFragmentAbsorberAddress);// iterate through individual TextFragmentforeach (TextFragment tf in TextFragmentAbsorberAddress.TextFragments){ // update text to blank characters tf.Text = "";}// save updated PDF file after text replacepdf.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 documentDocument pdfDocument = new Document("Document.pdf");// Create TextAbsorber object to find all the phrases matching the regular expressionTextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("\d{4}-\d{4}"); // Like 1999-2000// Set text search option to specify regular expression usageTextSearchOptions textSearchOptions = new TextSearchOptions(true);textFragmentAbsorber.TextSearchOptions = textSearchOptions;// Accept the absorber for a single pagepdfDocument.Pages[1].Accept(textFragmentAbsorber);// Get the extracted text fragmentsTextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;// Loop through the fragmentsforeach (TextFragment textFragment in textFragmentCollection){ // Update text and other properties textFragment.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 PDFpdfDocument.Save("output.pdf");
还想要更多吗可以点击阅读【2020 · Aspose最新资源整合】,查找需要的教程资源。如果您有任何疑问或需求,请随时加入Aspose技术交流群(),我们很高兴为您提供查询和咨询。
标签:
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!