PDF 文書のテキストを置換する必要がある場合は多々あります。誤字脱字の修正、古い情報の更新、特定の読者や目的に合わせた内容のカスタマイズ、または法的・規制上の要件に準拠するためなどが考えられます。PDF 内のテキストを置換することで、正確性を確保し、文書の整合性を保ち、提供される情報の品質と関連性を向上させることができます。
本記事では、C# を使用して Spire.PDF for .NET ライブラリを使い、PDF 文書のテキストを置換する方法を紹介します。
Spire.PDF for .NET をインストールします
まず、Spire.PDF for .NET パッケージに含まれている DLL ファイルを .NET プロジェクトの参照として追加する必要があります。DLL ファイルは、このリンクからダウンロードするか、NuGet を介してインストールできます。
PM> Install-Package Spire.PDFC# で特定の PDF ページ内の一致するテキストを置換
Spire.PDF for .NET は、PdfTextReplacer.ReplaceAllText() メソッドを提供しており、ページ内のターゲットテキストのすべての一致箇所を新しいテキストに置換することが可能です。以下は、C# で特定のページ内の一致するテキストを置換する手順です。
- PdfDocument オブジェクトを作成する。
- PDF ファイルを読み込む。
- 文書から特定のページを取得する。
- PdfTextReplaceOptions オブジェクトを作成し、ReplaceType プロパティを使って置換オプションを指定する。
- PdfTextReplacer オブジェクトを作成し、Options プロパティを使用して置換オプションを適用する。
- PdfTextReplacer.ReplaceAllText() メソッドを使って、ページ内のすべての一致するテキストを新しいテキストに置換する。
- 文書を保存する。
- C#
using Spire.Pdf;
using Spire.Pdf.Texts;
namespace ReplaceTextInPage
{
    class Program
    {
        static void Main(string[] args)
        {
            // PdfDocumentオブジェクトを作成
            PdfDocument doc = new PdfDocument();
            // PDFファイルを読み込む
            doc.LoadFromFile("Sample.pdf");
            // PdfTextReplaceOptionsオブジェクトを作成
            PdfTextReplaceOptions textReplaceOptions = new PdfTextReplaceOptions();
            // テキスト置換のオプションを指定
            textReplaceOptions.ReplaceType = PdfTextReplaceOptions.ReplaceActionType.IgnoreCase;
            textReplaceOptions.ReplaceType = PdfTextReplaceOptions.ReplaceActionType.WholeWord;
            textReplaceOptions.ReplaceType = PdfTextReplaceOptions.ReplaceActionType.AutofitWidth;
            // 特定のページを取得
            PdfPageBase page = doc.Pages[0];
            // ページに基づいてPdfTextReplacerオブジェクトを作成
            PdfTextReplacer textReplacer = new PdfTextReplacer(page);
            // 置換オプションを設定
            textReplacer.Options = textReplaceOptions;
            // ターゲットテキストを新しいテキストに全て置換
            textReplacer.ReplaceAllText("Spire.PDF for .NET", "Spire.Pdf for .Net");
            // ドキュメントを別のPDFファイルに保存
            doc.SaveToFile("PDFページのテキストを置換.pdf");
            // リソースを解放
            doc.Dispose();
        }
    }
}
C# で PDF 文書全体の一致するテキストを置換
文書全体のすべての一致するテキストを新しいテキストに置換するには、文書内のページを繰り返し処理し、それぞれのページで PdfTextReplacer.ReplaceAllText() メソッドを使用してテキストを置換する必要があります。
以下は、C# で PDF 文書全体の一致するテキストを置換する手順です。
- PdfDocument オブジェクトを作成する。
- PDF ファイルを読み込む。
- PdfTextReplaceOptions オブジェクトを作成し、ReplaceType プロパティを使って置換オプションを指定する。
- 文書内の各ページを繰り返し処理する。
- 指定されたページに基づいて PdfTextReplacer オブジェクトを作成し、Options プロパティを使って置換オプションを適用する。
- PdfTextReplacer.ReplaceAllText() メソッドを使ってページ内のすべての一致するテキストを新しいテキストに置換する。
- 文書を保存する。
- C#
using Spire.Pdf;
using Spire.Pdf.Texts;
namespace ReplaceInEntireDocument
{
    class Program
    {
        static void Main(string[] args)
        {
            // PdfDocumentオブジェクトを作成
            PdfDocument doc = new PdfDocument();
            // PDFファイルを読み込む
            doc.LoadFromFile("Sample.pdf");
            // PdfTextReplaceOptionsオブジェクトを作成
            PdfTextReplaceOptions textReplaceOptions = new PdfTextReplaceOptions();
            // テキスト置換のオプションを指定
            textReplaceOptions.ReplaceType = PdfTextReplaceOptions.ReplaceActionType.IgnoreCase;
            textReplaceOptions.ReplaceType = PdfTextReplaceOptions.ReplaceActionType.WholeWord;
            textReplaceOptions.ReplaceType = PdfTextReplaceOptions.ReplaceActionType.AutofitWidth;
            for (int i = 0; i < doc.Pages.Count; i++)
            {
                // 特定のページを取得
                PdfPageBase page = doc.Pages[i];
                // ページに基づいてPdfTextReplacerオブジェクトを作成
                PdfTextReplacer textReplacer = new PdfTextReplacer(page);
                // 置換オプションを設定
                textReplacer.Options = textReplaceOptions;
                // ターゲットテキストを新しいテキストに全て置換
                textReplacer.ReplaceAllText("Spire.PDF for .NET", "Spire.Pdf for .Net");
            }
            // ドキュメントを別のPDFファイルに保存
            doc.SaveToFile("PDF文書のテキストを置換.pdf");
            // リソースを解放
            doc.Dispose();
        }
    }
}C# で最初の一致するテキストのみを置換
ページ内のすべての一致するテキストを置換する代わりに、PdfTextReplacer クラスの ReplaceText() メソッドを使用して、ターゲットテキストの最初の一致箇所のみを置換することができます。
以下は、C# でターゲットテキストの最初の一致箇所を置換する手順です。
- PdfDocument オブジェクトを作成する。
- PDF ファイルを読み込む。
- 文書から特定のページを取得する。
- PdfTextReplaceOptions オブジェクトを作成し、ReplaceType プロパティを使って置換オプションを指定する。
- PdfTextReplacer オブジェクトを作成し、Options プロパティを使用して置換オプションを適用する。
- PdfTextReplacer.ReplaceText() メソッドを使ってページ内の最初のターゲットテキストを新しいテキストに置換する。
- 文書を保存する。
- C#
using Spire.Pdf;
using Spire.Pdf.Texts;
namespace ReplaceFirstOccurance
{
    class Program
    {
        static void Main(string[] args)
        {
            // PdfDocumentオブジェクトを作成
            PdfDocument doc = new PdfDocument();
            // PDFファイルを読み込む
            doc.LoadFromFile("Sample.pdf");
            // PdfTextReplaceOptionsオブジェクトを作成
            PdfTextReplaceOptions textReplaceOptions = new PdfTextReplaceOptions();
            // テキスト置換のオプションを指定
            textReplaceOptions.ReplaceType = PdfTextReplaceOptions.ReplaceActionType.IgnoreCase;
            textReplaceOptions.ReplaceType = PdfTextReplaceOptions.ReplaceActionType.WholeWord;
            textReplaceOptions.ReplaceType = PdfTextReplaceOptions.ReplaceActionType.AutofitWidth;
            // 特定のページを取得
            PdfPageBase page = doc.Pages[0];
            // ページに基づいてPdfTextReplacerオブジェクトを作成
            PdfTextReplacer textReplacer = new PdfTextReplacer(page);
            // 置換オプションを設定
            textReplacer.Options = textReplaceOptions;
            // ターゲットテキストの最初の出現を新しいテキストに置換
            textReplacer.ReplaceText("Spire.PDF for .NET", "Spire.Pdf for .Net");
            // ドキュメントを別のPDFファイルに保存
            doc.SaveToFile("PDF内の最初の一致するテキストを置換.pdf");
            // リソースを解放
            doc.Dispose();
        }
    }
}
C# で正規表現を使用してテキストを置換
正規表現は、テキストのパターンをマッチングし操作するための強力で柔軟な手段です。Spire.PDF では、正規表現を使用して PDF 内の特定のテキストパターンを検索し、新しい文字列に置換することが可能です。
以下は、正規表現を使用して PDF 内のテキストを置換する手順です。
- PdfDocument オブジェクトを作成する。
- PDF ファイルを読み込む。
- 文書から特定のページを取得する。
- PdfTextReplaceOptions オブジェクトを作成する。
- PdfTextReplaceOptions.ReplaceType プロパティを使用して置換タイプを Regex に設定する。
- PdfTextReplacer オブジェクトを作成し、Options プロパティを使って置換オプションを適用する。
- PdfTextReplacer.ReplaceAllText() メソッドを使用して、指定した正規表現に一致するテキストを検索し、置換する。
- 文書を保存する。
- C#
using Spire.Pdf;
using Spire.Pdf.Texts;
namespace ReplaceUsingRegularExpression
{
    class Program
    {
        static void Main(string[] args)
        {
            // PdfDocumentオブジェクトを作成
            PdfDocument doc = new PdfDocument();
            // PDFファイルを読み込む
            doc.LoadFromFile("Sample1.pdf");
            // PdfTextReplaceOptionsオブジェクトを作成
            PdfTextReplaceOptions textReplaceOptions = new PdfTextReplaceOptions();
            // 置換タイプを正規表現に設定
            textReplaceOptions.ReplaceType = PdfTextReplaceOptions.ReplaceActionType.Regex;
            // 特定のページを取得
            PdfPageBase page = doc.Pages[0];
            // ページに基づいてPdfTextReplacerオブジェクトを作成
            PdfTextReplacer textReplacer = new PdfTextReplacer(page);
            // 置換オプションを設定
            textReplacer.Options = textReplaceOptions;
            // 正規表現を指定
            string regularExpression = @"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b";
            // 正規表現に一致するすべての出現を新しいテキストに置換
            textReplacer.ReplaceAllText(regularExpression, "manager @system.com");
            // ドキュメントを別のPDFファイルに保存
            doc.SaveToFile("正規表現を使ってテキストを置換.pdf");
            // リソースを解放
            doc.Dispose();
        }
    }
}
一時ライセンスを申請する
結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。にお問い合わせ、30 日間有効な一時ライセンスを取得してください。
 
	    
	  	  





