翻譯|使用教程|編輯:李顯亮|2020-12-24 10:13:54.030|閱讀 725 次
概述:使用“查找并替換”選項(xiàng)可以一次性替換文檔中的特定文本。本文介紹了如何在PDF文檔中自動(dòng)查找和替換文本功能。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門(mén)軟控件火熱銷售中 >>
相關(guān)鏈接:
使用“查找并替換”選項(xiàng)可以一次性替換文檔中的特定文本。這樣,您不必手動(dòng)定位和更新整個(gè)文檔中每次出現(xiàn)的文本。本文甚至更進(jìn)一步,介紹了如何在PDF文檔中自動(dòng)查找和替換文本功能。特別是,將學(xué)習(xí)如何使用C#在整個(gè)PDF,特定頁(yè)面或頁(yè)面區(qū)域中查找和替換文本。
.NET的Aspose.PDF是一個(gè)C#類庫(kù),為.NET應(yīng)用程序提供基本以及高級(jí)的PDF操作功能。該API還允許您以不同的方式無(wú)縫地查找和替換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#查找和替換特定頁(yè)面中的文本
以下是在PDF文檔的特定頁(yè)面上查找和替換文本的步驟。
以下代碼示例顯示了如何使用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ū)域,然后替換其中的文本。
下面的代碼示例演示如何使用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í)行此操作的步驟。
下面的代碼示例演示如何使用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");
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn