Aspose.PDF功能演示:使用C#查找和替換PDF文件中的文本
使用“查找并替換”選項(xiàng)可以一次性替換文檔中的特定文本。這樣,您不必手動(dòng)定位和更新整個(gè)文檔中每次出現(xiàn)的文本。本文甚至更進(jìn)一步,介紹了如何在PDF文檔中自動(dòng)查找和替換文本功能。特別是,將學(xué)習(xí)如何使用C#在整個(gè)PDF,特定頁(yè)面或頁(yè)面區(qū)域中查找和替換文本。
- 使用C#查找和替換PDF中的文本
- 查找和替換特定頁(yè)面中的文本
- 定義PDF頁(yè)面區(qū)域以查找和替換文本
- 使用正則表達(dá)式查找和替換PDF中的文本
.NET的Aspose.PDF是一個(gè)C#類庫(kù),為.NET應(yīng)用程序提供基本以及高級(jí)的PDF操作功能。該API還允許您以不同的方式無(wú)縫地查找和替換PDF文檔中的文本。
使用C#查找和替換PDF中的文本
以下是在PDF文檔中查找和替換文本的步驟。
- 使用Document類使用其路徑加載PDF文檔。
- 創(chuàng)建TextFragmentAbsorber類的實(shí)例,并將搜索短語(yǔ)提供給其構(gòu)造函數(shù)。
- 使用Document.Pages.Accept(TextFragmentAbsorber)接受PDF所有頁(yè)面的文本吸收器。
- 將提取的文本片段獲取到TextFragmentCollection對(duì)象中。
- 遍歷找到的TextFragmentCollection并替換每個(gè)片段中的文本。
- 使用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#查找和替換特定頁(yè)面中的文本
以下是在PDF文檔的特定頁(yè)面上查找和替換文本的步驟。
- 使用Document類使用其路徑加載PDF文檔。
- 創(chuàng)建TextFragmentAbsorber類的實(shí)例,并將搜索短語(yǔ)提供給其構(gòu)造函數(shù)。
- 使用Document.Pages [1] .Accept(TextFragmentAbsorber)接受所需頁(yè)面的文本吸收器。
- 遍歷找到的TextFragmentAbsorber.TextFragments集合,并替換每個(gè)片段中的文本。
- 使用Document.Save(String)方法保存更新的PDF文檔。
以下代碼示例顯示了如何使用C#在PDF的特定頁(yè)面中查找和替換文本。
// 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");
定義頁(yè)面區(qū)域以查找和替換文本
還可以在PDF文檔的頁(yè)面特定區(qū)域中查找和替換文本。以下步驟顯示了如何定義特定區(qū)域,然后替換其中的文本。
- 使用Document類使用其路徑加載PDF文檔。
- 創(chuàng)建TextFragmentAbsorber類的實(shí)例,并將搜索短語(yǔ)提供給其構(gòu)造函數(shù)。
- 使用Document.Pages [0] .Accept(TextFragmentAbsorber)接受所需頁(yè)面的文本吸收器。
- 使用Rectangle類定義頁(yè)面區(qū)域。
- 循環(huán)遍歷TextFragmentAbsorber.TextFragments集合,并替換每個(gè)片段中的文本。
- 使用Document.Save(String)方法保存更新的PDF文檔。
下面的代碼示例演示如何使用C#在PDF的特定頁(yè)面區(qū)域中查找和替換文本。
// 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");
使用正則表達(dá)式查找和替換PDF中的文本
也可以使用正則表達(dá)式來(lái)查找和替換與特定模式匹配的文本。為此,您只需要提供一個(gè)正則表達(dá)式即可代替普通搜索短語(yǔ)并使用TextSearchOptions。以下是執(zhí)行此操作的步驟。
- 使用Document類使用其路徑加載PDF文檔。
- 創(chuàng)建TextFragmentAbsorber類的實(shí)例,并將搜索短語(yǔ)提供給其構(gòu)造函數(shù)。
- 創(chuàng)建TextSearchOptions類的實(shí)例,然后將true傳遞給其構(gòu)造函數(shù)以啟用基于正則表達(dá)式的搜索。
- 分配TextSearchOptions對(duì)象TextFragmentAbsorber.TextSearchOptions財(cái)產(chǎn)。
- 使用Document.Pages [0] .Accept(TextFragmentAbsorber)接受所需頁(yè)面的文本吸收器。
- 使用Rectangle類定義頁(yè)面區(qū)域。
- 循環(huán)遍歷TextFragmentAbsorber.TextFragments集合,并替換每個(gè)片段中的文本。
- 使用Document.Save(String)方法保存更新的PDF文檔。
下面的代碼示例演示如何使用C#使用正則表達(dá)式查找和替換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");
還想要更多嗎?您可以點(diǎn)擊閱讀【2020 · Aspose最新資源整合】,查找需要的教程資源。如果您有任何疑問(wèn)或需求,請(qǐng)隨時(shí)加入Aspose技術(shù)交流群(761297826),我們很高興為您提供查詢和咨詢。