C#でPDFから埋め込まれたテキストと画像を抽出する方法

How to Extract Embedded Text and Images from PDFs

This article was translated from English: Does it need improvement?
Translated
View the article in English
role="alert">あなたのビジネスはPDFセキュリティとコンプライアンスに年間サブスクリプションの費用をかけすぎています。デジタル署名、墨塗り、暗号化、保護などのSaaSサービスを一括払いで管理できるIronSecureDocを検討してください。IronSecureDocドキュメントを探索

Extracting embedded text and images involves retrieving textual content and graphical elements within the document. This process allows users to access and repurpose the content for editing, searching, or converting text to other formats and saving images for reuse or analysis.

To extract text and images from a PDF, use IronPdf. The extracted image can be saved to the disk or converted to another image format and embedded in the newly rendered document.

Quickstart: Extract Text and Images with IronPDF

Effortlessly extract text and images from PDFs using IronPDF in just a few lines of code. This quickstart guide equips developers with the tools needed to retrieve embedded content from PDF documents, facilitating content repurposing and analysis. Whether you're extracting text for editing or saving images for further use, IronPDF ensures a streamlined and cost-effective solution. Get started with the IronPdf library today and experience seamless PDF content management.

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronPDF with NuGet Package Manager

    PM > Install-Package IronPdf

  2. Copy and run this code snippet.

    var pdf = new IronPdf.PdfDocument("sample.pdf");
    string text = pdf.ExtractAllText();
    var images = pdf.ExtractAllImages();
  3. Deploy to test on your live environment

    Start using IronPDF in your project today with a free trial
    arrow pointer


Extract Text Example

Text extraction can be performed on both newly rendered and existing PDF documents. Use the ExtractAllText method to extract the embedded text from the document. The method will return a string containing all the text in the given PDF. Pages are separated by four consecutive new line characters. Let's use a sample PDF that I have rendered from the Wikipedia website.

:path=/static-assets/pdf/content-code-examples/how-to/extract-text-and-images-extract-text.cs
using IronPdf;
using System.IO;

PdfDocument pdf = PdfDocument.FromFile("sample.pdf");

// Extract text
string text = pdf.ExtractAllText();

// Export the extracted text to a text file
File.WriteAllText("extractedText.txt", text);
Imports IronPdf
Imports System.IO

Private pdf As PdfDocument = PdfDocument.FromFile("sample.pdf")

' Extract text
Private text As String = pdf.ExtractAllText()

' Export the extracted text to a text file
File.WriteAllText("extractedText.txt", text)
$vbLabelText   $csharpLabel
Extracted text

Extract Text by Line and Character

Within each PDF page, it is possible to retrieve the coordinates of text lines and characters. First, select a page from the PDF and access the Lines and Characters properties. The coordinates are laid out as Top, Right, Bottom, and Left values, representing the position of the text.

:path=/static-assets/pdf/content-code-examples/how-to/extract-text-and-images-extract-text-by-line-character.cs
using IronPdf;
using System.IO;
using System.Linq;

// Open PDF from file
PdfDocument pdf = PdfDocument.FromFile("sample.pdf");

// Extract text by lines
var lines = pdf.Pages[0].Lines;

// Extract text by characters
var characters = pdf.Pages[0].Characters;

File.WriteAllLines("lines.txt", lines.Select(l => $"at Y={l.BoundingBox.Bottom:F2}: {l.Contents}"));
Imports IronPdf
Imports System.IO
Imports System.Linq

' Open PDF from file
Private pdf As PdfDocument = PdfDocument.FromFile("sample.pdf")

' Extract text by lines
Private lines = pdf.Pages(0).Lines

' Extract text by characters
Private characters = pdf.Pages(0).Characters

File.WriteAllLines("lines.txt", lines.Select(Function(l) $"at Y={l.BoundingBox.Bottom:F2}: {l.Contents}"))
$vbLabelText   $csharpLabel
Extracted text by line and character

画像抽出の例

Use the ExtractAllImages method to extract all images embedded in the document. The method will return the images as a list of AnyBitmap objects. Using the same document from our previous example, we extracted the images and exported them to the 'images' folder.

:path=/static-assets/pdf/content-code-examples/how-to/extract-text-and-images-extract-image.cs
using IronPdf;

PdfDocument pdf = PdfDocument.FromFile("sample.pdf");

// Extract images
var images = pdf.ExtractAllImages();

for(int i = 0; i < images.Count; i++)
{
    // Export the extracted images
    images[i].SaveAs($"images/image{i}.png");
}
Imports IronPdf

Private pdf As PdfDocument = PdfDocument.FromFile("sample.pdf")

' Extract images
Private images = pdf.ExtractAllImages()

For i As Integer = 0 To images.Count - 1
	' Export the extracted images
	images(i).SaveAs($"images/image{i}.png")
Next i
$vbLabelText   $csharpLabel
Extracted images

In addition to the ExtractAllImages method shown above, the user can use the ExtractAllBitmaps and ExtractAllRawImages methods to extract image information from the document. While the ExtractAllBitmaps method will return a List of AnyBitmap, like the code example, the ExtractAllRawImages method extracts all images from a PDF document and returns them as raw data in the form of Byte Arrays (byte[]).


Extract Text and Images on Specific Pages

Both text and image extraction can be performed on single or multiple specified pages. Use the ExtractTextFromPage and ExtractTextFromPages methods to extract text from a single page or multiple pages, respectively. For extracting images, use the ExtractImagesFromPage and ExtractImagesFromPages methods.

:path=/static-assets/pdf/content-code-examples/how-to/extract-text-and-images-extract-text-single-multiple.cs
using IronPdf;

PdfDocument pdf = PdfDocument.FromFile("sample.pdf");

// Extract text from page 1
string textFromPage1 = pdf.ExtractTextFromPage(0);

int[] pages = new[] { 0, 2 };

// Extract text from pages 1 & 3
string textFromPage1_3 = pdf.ExtractTextFromPages(pages);
Imports IronPdf

Private pdf As PdfDocument = PdfDocument.FromFile("sample.pdf")

' Extract text from page 1
Private textFromPage1 As String = pdf.ExtractTextFromPage(0)

Private pages() As Integer = { 0, 2 }

' Extract text from pages 1 & 3
Private textFromPage1_3 As String = pdf.ExtractTextFromPages(pages)
$vbLabelText   $csharpLabel

よくある質問

.NET C#でPDFから埋め込まれたテキストを抽出するにはどうすればよいですか?

PDFから埋め込まれたテキストを抽出するには、IronPdfライブラリのExtractAllTextメソッドを使用できます。このメソッドは、各ページごとに4つの連続した改行文字で区切られたテキストを含む文字列を返します。

C#を使用してPDFから画像を抽出する際の手順は何ですか?

C#でPDFから画像を抽出するには、まずNuGet経由でIronPdfライブラリをダウンロードします。その後、ExtractAllImagesメソッドを使用して、画像を表すAnyBitmapオブジェクトのリストを取得します。

PDFドキュメントの特定のページからテキストを抽出することは可能ですか?

はい、IronPdfのExtractTextFromPageおよびExtractTextFromPagesメソッドを使用して、PDFドキュメントの特定のページまたは複数のページからテキストを抽出することができます。

行と文字の座標によってテキストを抽出する目的は何ですか?

行と文字の座標によるテキスト抽出は、PDFページ内のテキストの正確な位置を取得できるようにします。これは、IronPdfの**Lines**および**Characters**プロパティを使用して、Top、Right、Bottom、Leftの値を提供することで実行できます。

PDFから生画像を抽出するにはどうすればよいですか?

生の形式で画像を抽出するには、IronPdfのExtractAllRawImagesメソッドを使用します。このメソッドは画像をバイト配列として返し、元の画像データにアクセスできます。

テキストと画像の抽出にIronPdfを使用することの利点は何ですか?

IronPdfを使ってPDFからテキストと画像を抽出することは、一回限りの支払いソリューションを提供するため、コスト効率が良いです。それは、編集、検索、他の形式への変換、または分析のための画像の再利用に対してコンテンツを再利用するのに役立ちます。

PDFコンテンツの抽出を開始するには、どうすればIronPdfを使用できますか?

IronPdfを使用し始めるには、NuGetからIronPdf C#ライブラリをダウンロードし、PDFドキュメントを準備してExtractAllTextExtractAllImagesメソッドなどを使用したコンテンツ抽出のためのガイドに従います。

単一のPDFページからテキストと画像の両方を抽出することは可能ですか?

はい、IronPdfを使用すると、ExtractTextFromPageおよびExtractImagesFromPageメソッドを使用して、単一のPDFページからテキストと画像の両方を抽出できます。

複数のページから画像を抽出するための方法は何ですか?

IronPdfのExtractImagesFromPagesメソッドを使用して、PDFドキュメントの複数のページから画像を抽出できます。

IronPdf はテキストと画像の抽出において .NET 10 と互換性がありますか?

はい。IronPdf は .NET 10 に加え、.NET 9、8、7、6、CORE、Standard、Framework などの以前の最新バージョンもサポートしています。ExtractAllText、 ExtractAllTextExtractTextFromPageExtractImagesFromPagesなどのメソッドはすべて、.NET 10 プロジェクト内でもExtractAllImages回避策や互換性調整を必要とせずに使用できます。

Chaknith Bin
ソフトウェアエンジニア
ChaknithはIronXLとIronBarcodeに取り組んでいます。彼はC#と.NETの深い専門知識を持ち、ソフトウェアの改善や顧客サポートに貢献しています。ユーザーとの対話から得られる洞察が、より良い製品、ドキュメント、および全体的な経験に寄与しています。
準備はいいですか?
Nuget ダウンロード 16,154,058 | バージョン: 2025.11 ただ今リリースされました