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

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

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

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

以下是在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文档的特定页面上查找和替换文本的步骤。

以下代码示例显示了如何使用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文档的页面特定区域中查找和替换文本。以下步骤显示了如何定义特定区域,然后替换其中的文本。

下面的代码示例演示如何使用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。以下是执行此操作的步骤。

下面的代码示例演示如何使用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");

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