LoginSignup
0
0

【C#/VB.NET】PDFで特定のテキストを検索して強調表示する方法

Posted at

PDF文書の作成や編集において、文書中の重要な文字、単語、文を特別な背景色で強調表示することで、読者が重要な内容を探しやすくし、読者の読書効率や閲覧体験を向上させることができます。この記事では、無料のFree Spire.PDF for .NETを使用して、プログラムを通じて指定したテキストを検索し、ハイライト表示する方法を説明します。

依存関係の追加

Free Spire.PDF for .NETの依存関係は、公式ウェブサイトから手動でダウンロードするか、NuGet Package Manageで検索するか、パッケージマネージャーコンソールで次のコードを使用して追加することができます。

PM> Install-Package FreeSpire.PDF

PDF文書内の特定のテキストを検索して強調表示する

PdfTextFinder.Find() メソッドで指定したテキストを探し、PdfTextFragment.Highlight() メソッドで探すテキストを強調表示すればいいのです。
     1. PdfDocumentのオブジェクトを作成し、PDF文書を読み込みます。

PdfDocument pdf = new PdfDocument(“サンプル.pdf”);

     2. PdfTextFindOptionsのオブジェクトを作成します。

PdfTextFindOptions findOptions = new PdfTextFindOptions();

     3. テキスト検索のパラメーターを設定します。

findOptions.Parameter = TextFindParameter.WholeWord;

     4. PdfTextFinderのオブジェクトを作成します。

PdfTextFinder finder = new PdfTextFinder(page);

     5. テキスト検索のオプションを設定します。

finder.Options = findOptions;

     6. 指定のテキストを検索します。

List<PdfTextFragment> results = finder.Find("PostScript");

     7. 見つかった指定のテキストをすべて強調表示します。

text.HighLight(Color.GreenYellow);

     8. 文書を保存します。

pdf.SaveToFile("テキストの強調表示.pdf");

【完全なコード例】

C#

using Spire.Pdf;
using Spire.Pdf.Texts;
using System.Collections.Generic;
using System.Drawing;

namespace HighlightTextInPdf
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // PDFドキュメントを読み込む
            PdfDocument pdf = new PdfDocument("サンプル.pdf");

            //PdfTextFindOptionsのオブジェクトを作成する
            PdfTextFindOptions findOptions = new PdfTextFindOptions();
            //テキスト検索のパラメータを設定する
            findOptions.Parameter = TextFindParameter.WholeWord;

            //文書内のページをループする
            foreach (PdfPageBase page in pdf.Pages)
            {
                //PdfTextFinderのオブジェクトを作成する
                PdfTextFinder finder = new PdfTextFinder(page);
                //テキスト検索のオプションを設定する
                finder.Options = findOptions;
                //指定したテキストを検索する
                List<PdfTextFragment> results = finder.Find("PostScript");
                //指定したテキストをすべて強調表示する
                foreach (PdfTextFragment text in results)
                {
                    text.HighLight(Color.GreenYellow);
                }
            }

            //ドキュメントを保存する
            pdf.SaveToFile("テキストの強調表示.pdf");
        }
    }
}

VB.NET

Imports Spire.Pdf
Imports Spire.Pdf.Texts
Imports System.Collections.Generic
Imports System.Drawing

Namespace HighlightTextInPdf
    Friend Class Program
        Shared Sub Main(ByVal args() As String)
            'PDFドキュメントを読み込む
            Dim pdf As PdfDocument = New PdfDocument("サンプル.pdf")

            'PdfTextFindOptionsのオブジェクトを作成する
            Dim findOptions As PdfTextFindOptions = New PdfTextFindOptions()
            'テキスト検索のパラメータを設定する
            findOptions.Parameter = TextFindParameter.WholeWord

            '文書内のページをループする
            Dim page As PdfPageBase
            For Each page In pdf.Pages
                'PdfTextFinderのオブジェクトを作成する
                Dim finder As PdfTextFinder = New PdfTextFinder(page)
                'テキスト検索のオプションを設定する
                finder.Options = findOptions
                '指定したテキストを検索する
                Dim results As List<PdfTextFragment>=  finder.Find("PostScript") 
                '指定したテキストをすべて強調表示する
                Dim text As PdfTextFragment
                For Each text In results
                    text.HighLight(Color.GreenYellow)
                Next
            Next

            'ドキュメントを保存する
            pdf.SaveToFile("テキストの強調表示.pdf")
        End Sub
    End Class
End Namespace

【結果文書】

PDF文書内の特定のテキストを検索して強調表示する

これは、PDF文書内の特定のテキストを検索し、強調するためにフリーSpire.PDF for .NETを使用する方法についてのすべてです。 フリーSpire.PDF for NETは、他の多くのPDF処理機能を持ち、あなたはより多くを学ぶためにSpire.PDF for .NETチュートリアルに行くことができます。

代表的な機能です:

PDF文書をWord文書に変換する
PDF文書のページを回転させる
PDF文書から空白ページを削除する

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0