翻譯|使用教程|編輯:李顯亮|2020-12-24 10:13:54.030|閱讀 725 次
概述:使用“查找并替換”選項可以一次性替換文檔中的特定文本。本文介紹了如何在PDF文檔中自動查找和替換文本功能。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
使用“查找并替換”選項可以一次性替換文檔中的特定文本。這樣,您不必手動定位和更新整個文檔中每次出現的文本。本文甚至更進一步,介紹了如何在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");
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn