文字與圖像擷取功能可從 PDF 文件中提取文字內容及圖形元素。 存取並重新利用內容,以便進行編輯、搜尋、將文字轉換為其他格式,或儲存圖片以供重複使用。 無論您需要使用 C# 解析 PDF 進行資料分析、將內容轉換為可搜尋格式,還是擷取視覺元素以供歸檔,IronPDF 皆提供全面的擷取工具。
using IronPDF 擷取文字與圖片。 請將擷取的圖片儲存至磁碟,或轉換為其他格式,再嵌入至新文件中。 此靈活性可支援需要內容轉換的工作流程,例如將 PDF 轉換為 HTML 或重新利用擷取的圖片。
快速入門:使用 IronPDF 擷取文字與圖片
只需幾行程式碼,即可從 PDF 中擷取文字和圖片。 本快速入門指南將示範如何從 PDF 文件中擷取嵌入式內容,以供內容再利用與分析之用。 透過 IronPDF 的精簡解決方案,擷取文字進行編輯,或儲存圖片以供後續使用。
1Install IronPDF with NuGet Package Manager
PM > Install-Package IronPdf
Install-Package IronPdf
2複製並運行這段程式碼片段。
var pdf = new IronPdf.PdfDocument("sample.pdf"); string text = pdf.ExtractAllText(); var images = pdf.ExtractAllImages();
var pdf = new IronPdf.PdfDocument("sample.pdf");
string text = pdf.ExtractAllText();
var images = pdf.ExtractAllImages();
從新渲染及現有的 PDF 文件中擷取文字。 請使用 ExtractAllText 方法從文件中提取嵌入的文字。 此方法會傳回一個包含 PDF 中所有文字的字串。 各頁以四個連續的換行字元分隔。 此範例使用從維基百科網站擷取並渲染的樣本 PDF 檔案。
當處理包含國際語言及 UTF-8 字元的 PDF 檔案時,IronPDF 能維持正確的編碼與字元呈現。 這可確保非拉丁文字及特殊字元的正確顯示。
using IronPdf;using System.IO;PdfDocument pdf = PdfDocument.FromFile("sample.pdf");// Extract textstring text = pdf.ExtractAllText();// Export the extracted text to a text fileFile.WriteAllText("extractedText.txt", text);
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);
ImportsIronPdfImportsSystem.IOPrivate pdf AsPdfDocument = PdfDocument.FromFile("sample.pdf")' Extract textPrivate text AsString = pdf.ExtractAllText()' Export the extracted text to a text fileFile.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)
如何根據精確座標擷取文字?
擷取每頁 PDF 文件中文字行與字元的座標。 從 PDF 中選取一頁,並存取 Lines 和 Characters 屬性。 座標包含 Bottom 及 Left 等值,代表文字位置。 此功能可保留版面配置,並支援文字位置分析。
對於需要以 C# 讀取 PDF 檔案並具備位置感知能力的開發人員而言,座標擷取功能可提供資料,用以維持文件結構並實現進階文字分析。
using IronPdf;using System.IO;using System.Linq;// Open PDF from filePdfDocument pdf = PdfDocument.FromFile("sample.pdf");// Extract text by linesvar lines = pdf.Pages[0].Lines;// Extract text by charactersvar characters = pdf.Pages[0].Characters;File.WriteAllLines("lines.txt", lines.Select(l => $"at Y={l.BoundingBox.Bottom:F2}: {l.Contents}"));
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}"));
ImportsIronPdfImportsSystem.IOImportsSystem.Linq' Open PDF from filePrivate pdf AsPdfDocument = PdfDocument.FromFile("sample.pdf")' Extract text by linesPrivate lines = pdf.Pages(0).Lines' Extract text by charactersPrivate characters = pdf.Pages(0).CharactersFile.WriteAllLines("lines.txt", lines.Select(Function(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}"))
如何從 PDF 中擷取圖片?
請使用 ExtractAllImages 方法從文件中提取所有嵌入的圖片。 此方法會將影像以 List 物件的 AnyBitmap 清單形式傳回。 我們使用同一份文件,擷取其中的圖片並將其匯出至"images"資料夾。 此功能支援影像歸檔、內容遷移,以及將 PDF 頁面渲染為影像以供後續處理。
using IronPdf;PdfDocument pdf = PdfDocument.FromFile("sample.pdf");// Extract imagesvar images = pdf.ExtractAllImages();for(int i = 0; i < images.Count; i++){ // Export the extracted images images[i].SaveAs($"images/image{i}.png");}
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");
}
ImportsIronPdfPrivate pdf AsPdfDocument = PdfDocument.FromFile("sample.pdf")' Extract imagesPrivate images = pdf.ExtractAllImages()For i AsInteger = 0 To images.Count - 1 ' Export the extracted images images(i).SaveAs($"images/image{i}.png")Next i
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
這種細粒度的控制功能,對於處理大型文件時特別有用,因為通常只有特定段落包含相關內容。 它還支援分割 PDF 並擷取單一頁面以進行獨立處理的功能。
using IronPdf;PdfDocument pdf = PdfDocument.FromFile("sample.pdf");// Extract text from page 1string textFromPage1 = pdf.ExtractTextFromPage(0);int[] pages = new[] { 0, 2 };// Extract text from pages 1 & 3string textFromPage1_3 = pdf.ExtractTextFromPages(pages);
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);
ImportsIronPdfPrivate pdf AsPdfDocument = PdfDocument.FromFile("sample.pdf")' Extract text from page 1Private textFromPage1 AsString = pdf.ExtractTextFromPage(0)Private pages() AsInteger = { 0, 2 }' Extract text from pages 1 & 3Private textFromPage1_3 AsString = 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)
using IronPDF 的 ExtractAllText 方法,可從 PDF 文件中擷取嵌入的文字。此方法會傳回一個字串,其中包含 PDF 中的所有文字,各頁面以四個連續的換行字元分隔。IronPDF 能正確處理國際語言及 UTF-8 字元的編碼。
我可以透過程式碼從 PDF 檔案中擷取圖片嗎?
是的,IronPDF 提供 ExtractAllImages 方法,用於從 PDF 文件中擷取圖形元素。您可以將擷取的圖片儲存至磁碟,或轉換為其他格式,再將其嵌入新文件中。
PDF 內容擷取的主要應用情境有哪些?
IronPDF 的資料擷取工具支援多種工作流程,包括解析 PDF 進行資料分析、將內容轉換為可搜尋格式、擷取視覺元素以供歸檔,以及將內容重新利用以進行編輯或轉換為其他格式(如 HTML)。
提取 PDF 內容需要多少行程式碼?
透過 IronPDF,您只需幾行程式碼即可擷取文字與圖片。只需載入您的 PDF 文件,並呼叫 ExtractAllText() 進行文字擷取,或呼叫 ExtractAllImages() 進行圖片擷取。
我可以從特定頁面提取內容,而不是整個文件嗎?
是的,IronPDF 允許您指定要擷取文字和圖片的特定頁面,讓您能精確控制從 PDF 文件中擷取哪些內容。
How can IronPDF assist with extracting text from PDFs containing international languages?
IronPDF maintains proper encoding and character representation for international languages and UTF-8 characters, ensuring correct text extraction and display for non-Latin scripts and special characters.
Is it possible to extract images from a PDF for use in Azure Blob Storage using IronPDF?
Yes, IronPDF's image extraction capabilities are compatible with Azure Blob Storage workflows, allowing you to save extracted images in various formats and manage them effectively in the cloud.
Does IronPDF support extracting text from specific layers within a PDF?
Yes, IronPDF can extract text from specific layers (OCGs) within a PDF using the `ExtractTextFromLayer` and `GetTextObjectsByLayer` methods, which target content grouped in named layers like CAD drawings or map exports.