このクイックガイドでは、詳細な手順と実行可能なコードを使用して、** C#を使用してPDF内のテキストを検索および置換する方法について説明します。環境の構成を支援し、 C#を使用してPDF内のテキストを置き換える**ための段階的なプロセスを提供します。ファイルが更新されたら、元の形式、つまりPDFとして、またはDOCX、Excel、HTMLなどとしてディスクに保存できます。
C#を使用してPDF内のテキストを検索および置換する手順
- NuGetパッケージマネージャーを使用してAspose.PDF for .NETを使用するようにプロジェクトを構成します
- Documentクラスオブジェクトを使用して、サンプルテキストを含むPDFファイルを作成またはロードします
- TextFragmentAbsorberクラスオブジェクトを使用して、検索するテキストを設定します
- 入力PDFファイルのすべてのページで、テキストアブソーバーを受け入れます
- ロードされたPDFファイルからテキストが抽出されるフラグメントのコレクションを取得します
- すべてのフラグメントを解析し、新しいテキストを設定します
- 更新されたPDFファイルを保存します
これらの手順では、* PDF検索でC#*を使用してテキストを置き換える方法について説明します。サンプルテキストを含む新しいファイルが作成されますが、テキストが置き換えられる既存のPDFファイルをロードできます。シャドウテキストを無視する、検索をページバインドに制限するなど、PDF内のテキストを検索するために使用できるさまざまなオプションがあります。
C#を使用してPDF内のテキストを置き換えるコード
using Aspose.Pdf; | |
using Aspose.Pdf.Text; | |
namespace FindAndReplaceTextInPdfUsingCSharp | |
{ | |
class Program | |
{ | |
static void Main(string[] args) // Main function to create 7z archive in CSharp | |
{ | |
// Instantiate a license to avoid watermark in output PDF | |
Aspose.Pdf.License licForPdf= new Aspose.Pdf.License(); | |
licForPdf.SetLicense("Aspose.Pdf.lic"); | |
// Create an empty PDF document | |
Document newPDFFile = new Document(); | |
// Add an empty page in the newly created PDF | |
Page page = newPDFFile.Pages.Add(); | |
// Add sample text in the PDF file | |
for(int iTxtCounter = 0 ; iTxtCounter < 15; iTxtCounter++) | |
page.Paragraphs.Add(new Aspose.Pdf.Text.TextFragment($"my_data\nanother data")); | |
// Save the newly created PDF file containing the test data in it | |
newPDFFile.Save("InputPDFToReplaceText.pdf"); | |
// Open PDF document to replace text in it | |
Document inputPDFFile = new Document("InputPDFToReplaceText.pdf"); | |
// Set the text that is to be searched in the TextAbsorber object | |
TextFragmentAbsorber txtAbsorber = new TextFragmentAbsorber("my_data"); | |
// Apply the text absorber for all the pages in the input PDF file | |
inputPDFFile.Pages.Accept(txtAbsorber); | |
// Get the collection of fragments containing extracted text from the PDF | |
TextFragmentCollection textFragmentCollection = txtAbsorber.TextFragments; | |
// Parse all the fragments and replace text using particular font, size and foreground/background color | |
foreach (TextFragment txtFragment in textFragmentCollection) | |
txtFragment.Text = "MY_DATA"; | |
// Save resulting PDF document. | |
inputPDFFile.Save("OutputPDFAfterReplacingText.pdf"); | |
System.Console.WriteLine("Done"); | |
} | |
} | |
} |
このコードは、テキストにTextFragmentAbsorberとTextFragmentを使用して、* C#を使用してPDFで検索および置換します*。テキストを置き換えるだけでなく、結果のPDFファイルのフォントファミリ、サイズ、前景色、および背景色を変更することもできます。 PDF全体のテキストを一度に置換したり、正規表現に基づいてテキストを置換したりするオプションも利用できます。
このトピックでは、PDF内のテキストを検索して置換する方法を学びましたが、PDFファイルをページごとに分割する方法を学びたい場合は、PDFファイルをC#でページごとに分割する方法の記事を参照してください。