この簡単なガイドでは、** C#を使用してテキストを検索してPDFで強調表示する方法について説明します。 PDFの読み込み、PDF内のテキストの検索、色と形を使用したハイライトアノテーションの作成、およびテキストへのさまざまなハイライトアノテーションの適用について説明します。サードパーティのツールをインストールしなくても、** C#を使用してPDFドキュメントを強調表示**できます。
C#を使用してPDFでテキストとハイライトを検索する手順
- NuGetパッケージマネージャーからAspose.PDF for .NETライブラリを構成して、PDF内のテキストを強調表示します
- Documentクラスオブジェクトを含む入力PDFファイルをロードして、テキストを検索して強調表示します
- PDF内のテキストを検索して強調表示します
- 色と形を指定しながらHighlightAnnotationを作成します
- 検索されたテキストにハイライト注釈を適用します
- 強調表示されたテキストで出力PDFファイルを保存します
これらの手順では、* C#を使用してPDFのテキストを強調表示する*簡単なプロセスを詳しく説明します。ドキュメント内の特定の文字列を検索して、それを強調表示するだけです。さらに、必要に応じて、形状とともに黄色、赤、またはその他の色などのテキストを強調表示する色を選択できます。
C#を使用してPDF内の単語を強調表示するコード
using Aspose.Pdf; | |
using Aspose.Pdf.Annotations; | |
using Aspose.Pdf.Text; | |
namespace SearchTextAndHighlightInPdfUsingCSharp | |
{ | |
class Program | |
{ | |
static void Main(string[] args) // Main function to search text in PDF and highlight | |
{ | |
// Instantiate the license to avoid any trial version limitations | |
// and watermark in the output ODF file | |
Aspose.Pdf.License licHighlightText= new Aspose.Pdf.License(); | |
licHighlightText.SetLicense("Aspose.Pdf.lic"); | |
// Load an existing PDF file in which you want to highlight text | |
Document doc = new Document("sample_input.pdf"); | |
// Search target text to highlight | |
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("The text to be searched"); | |
doc.Pages[1].Accept(textFragmentAbsorber); | |
// Create a highlight annotation | |
HighlightAnnotation ha = new HighlightAnnotation(doc.Pages[1], textFragmentAbsorber.TextFragments[1].Rectangle); | |
// Specify highlight color | |
ha.Color = Color.Yellow; | |
// Add annotation to highlight text in PDF | |
doc.Pages[1].Annotations.Add(ha); | |
// Save the document | |
doc.Save("PDF_with_Highlighted_Text.pdf"); | |
System.Console.WriteLine("Done"); | |
} | |
} | |
} |
このコードは、* C#を使用してPDFで強調表示する方法*の質問に答えます。 TextFragmentAbsorberクラスは、PDFの特定のページで文字列を検索するために使用されます。 HighlightAnnotationクラスは、使用する蛍光ペンの特性を定義するために使用されます。最後に、ディスクに保存する前に、PDFのテキストを強調表示するために定義された注釈を追加します。
このチュートリアルでは、PDFファイル内の特定のテキストを強調表示する方法について説明しました。ただし、プログラムでPDFファイルを読むことに興味がある場合は、C#でPDFを読む方法の記事に進んでください。