• PDF处理控件Aspose.PDF功能演示:使用C#查找和替换PDF文件中的文本


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

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

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

    点击下载最新版Aspose.PDFicon-default.png?t=N7T8https://www.evget.com/product/565/download


    使用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 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 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 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 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 characters
        tf.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 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 PDF
    pdfDocument.Save("output.pdf");
  • 相关阅读:
    Java工程师常见面试题集锦
    周记之马上要答辩了
    quarkus依赖注入之三:用注解选择注入bean
    多线程案例
    python没有重复数字的两位数统计 青少年编程电子学会python编程等级考试二级真题解析2021年6月
    vue前端开发ios系统:https发http请求 网络通讯之间的安全问题
    安装使用TinyCore Linux的一些收获
    Raid 独立磁盘冗余阵列
    基于Java的宠物商城管理系统设计与实现(源码+lw+部署文档+讲解等)
    【牛客刷题】前端--JS篇(一)
  • 原文地址:https://blog.csdn.net/m0_67129275/article/details/134419909