如何在 Node.js 中讀取 PDF 文件

C# PDF 解析器

This article was translated from English: Does it need improvement?
Translated
View the article in English

使用 IronPDF 的 ExtractAllText 方法在 C# 中解析 PDF 文件,以從整個文件或特定頁面中提取文字。 這種方法只需幾行程式碼即可為.NET應用程式提供簡單、高效的 PDF 文字擷取。

IronPDF使 C# 應用程式中的 PDF 解析變得簡單。 本教學課程示範如何使用IronPDF (一個用於產生和操作 PDF 的綜合性 C# 函式庫)來解析 PDF,只需幾個步驟即可完成。

快速入門:使用IronPDF高效率解析 PDF

使用IronPDF ,只需編寫最少的程式碼即可開始在 C# 中解析 PDF 檔案。 本範例展示如何從 PDF 文件中提取所有文本,同時保持其原始格式。 IronPDF 的 ExtractAllText 方法可實現將 PDF 解析無縫整合到.NET應用程式中。 請按照以下步驟即可輕鬆完成設定和執行。

  1. 使用NuGet套件管理器安裝https://www.nuget.org/packages/IronPdf

    PM > Install-Package IronPdf
  2. 複製並運行這段程式碼。

    var text = IronPdf.FromFile("sample.pdf").ExtractAllText();
  3. 部署到您的生產環境進行測試

    今天就在您的專案中開始使用免費試用IronPDF

    arrow pointer

如何在C#中解析PDF檔案?

使用IronPDF解析 PDF 檔案非常簡單。 下面的程式碼使用 ExtractAllText 方法從整個 PDF 文件中提取每一行文字。 對比圖顯示了擷取的 PDF 內容及其輸出結果。 該庫還支援從 PDF 文件的特定部分提取文字和圖像

:path=/static-assets/pdf/content-code-examples/how-to/csharp-parse-pdf-parse-pdf.cs
using IronPdf;

// Select the desired PDF File
PdfDocument pdf = PdfDocument.FromFile("sample.pdf");

// Extract all text from an pdf
string allText = pdf.ExtractAllText();

// Extract all text from page 1
string page1Text = pdf.ExtractTextFromPage(0);
$vbLabelText   $csharpLabel

IronPDF簡化了各種場景下的 PDF 解析。 無論是進行HTML 到 PDF 的轉換、從現有文件中提取內容,還是實現高級 PDF 功能,該程式庫都提供全面的支援。

IronPDF可與Windows 應用程式無縫集成,並支援在LinuxmacOS平台上部署。 該程式庫還支援Azure 部署,以實現基於雲端的解決方案。

高級文字擷取範例

以下是使用IronPDF解析 PDF 內容的其他方法:

using IronPdf;

// Parse PDF from URL
var pdfFromUrl = PdfDocument.FromUrl("https://example.com/document.pdf");
string urlPdfText = pdfFromUrl.ExtractAllText();

// Parse password-protected PDFs
var protectedPdf = PdfDocument.FromFile("protected.pdf", "password123");
string protectedText = protectedPdf.ExtractAllText();

// Extract text from specific page range
var largePdf = PdfDocument.FromFile("large-document.pdf");
for (int i = 5; i < 10; i++)
{
    string pageText = largePdf.ExtractTextFromPage(i);
    Console.WriteLine($"Page {i + 1}: {pageText.Substring(0, 100)}...");
}
using IronPdf;

// Parse PDF from URL
var pdfFromUrl = PdfDocument.FromUrl("https://example.com/document.pdf");
string urlPdfText = pdfFromUrl.ExtractAllText();

// Parse password-protected PDFs
var protectedPdf = PdfDocument.FromFile("protected.pdf", "password123");
string protectedText = protectedPdf.ExtractAllText();

// Extract text from specific page range
var largePdf = PdfDocument.FromFile("large-document.pdf");
for (int i = 5; i < 10; i++)
{
    string pageText = largePdf.ExtractTextFromPage(i);
    Console.WriteLine($"Page {i + 1}: {pageText.Substring(0, 100)}...");
}
$vbLabelText   $csharpLabel

這些範例展示了 IronPDF 在處理不同 PDF 來源和場景時的靈活性。 對於複雜的解析需求,可以探索PDF DOM 物件訪問,以便處理結構化內容。

處理不同類型的PDF文件

IronPDF擅長解析各種類型的 PDF 檔案:

using IronPdf;
using System.Text.RegularExpressions;

// Parse scanned PDFs with OCR (requires IronOcr)
var scannedPdf = PdfDocument.FromFile("scanned-document.pdf");
string ocrText = scannedPdf.ExtractAllText();

// Parse PDFs with forms
var formPdf = PdfDocument.FromFile("form.pdf");
string formText = formPdf.ExtractAllText();

// Extract and filter specific content
string invoiceText = pdf.ExtractAllText();
var invoiceNumber = Regex.Match(invoiceText, @"Invoice #: (\d+)").Groups[1].Value;
var totalAmount = Regex.Match(invoiceText, @"Total: \$([0-9,]+\.\d{2})").Groups[1].Value;
using IronPdf;
using System.Text.RegularExpressions;

// Parse scanned PDFs with OCR (requires IronOcr)
var scannedPdf = PdfDocument.FromFile("scanned-document.pdf");
string ocrText = scannedPdf.ExtractAllText();

// Parse PDFs with forms
var formPdf = PdfDocument.FromFile("form.pdf");
string formText = formPdf.ExtractAllText();

// Extract and filter specific content
string invoiceText = pdf.ExtractAllText();
var invoiceNumber = Regex.Match(invoiceText, @"Invoice #: (\d+)").Groups[1].Value;
var totalAmount = Regex.Match(invoiceText, @"Total: \$([0-9,]+\.\d{2})").Groups[1].Value;
$vbLabelText   $csharpLabel

如何查看已解析的PDF內容?

C# 表單顯示上述程式碼執行後解析的 PDF 內容。 此輸出提供 PDF 文件中的確切文本,以滿足文件處理需求。

~ PDF ~

C# 表單

擷取的文字保留了 PDF 的原始格式和結構,非常適合資料處理、內容分析或遷移任務。 透過尋找和取代特定內容或將其匯出為其他格式來進一步處理此文字。

將 PDF 解析整合到您的應用程式中

IronPDF的解析功能可整合到各種類型的應用程式中:

// ASP.NET Core example
public IActionResult ParseUploadedPdf(IFormFile pdfFile)
{
    using var stream = pdfFile.OpenReadStream();
    var pdf = PdfDocument.FromStream(stream);

    var extractedText = pdf.ExtractAllText();

    // Process or store the extracted text
    return Json(new { 
        success = true, 
        textLength = extractedText.Length,
        preview = extractedText.Substring(0, Math.Min(500, extractedText.Length))
    });
}

// Console application example
static void BatchParsePdfs(string folderPath)
{
    var pdfFiles = Directory.GetFiles(folderPath, "*.pdf");

    foreach (var file in pdfFiles)
    {
        var pdf = PdfDocument.FromFile(file);
        var text = pdf.ExtractAllText();

        // Save extracted text
        var textFile = Path.ChangeExtension(file, ".txt");
        File.WriteAllText(textFile, text);

        Console.WriteLine($"Parsed: {Path.GetFileName(file)} - {text.Length} characters");
    }
}
// ASP.NET Core example
public IActionResult ParseUploadedPdf(IFormFile pdfFile)
{
    using var stream = pdfFile.OpenReadStream();
    var pdf = PdfDocument.FromStream(stream);

    var extractedText = pdf.ExtractAllText();

    // Process or store the extracted text
    return Json(new { 
        success = true, 
        textLength = extractedText.Length,
        preview = extractedText.Substring(0, Math.Min(500, extractedText.Length))
    });
}

// Console application example
static void BatchParsePdfs(string folderPath)
{
    var pdfFiles = Directory.GetFiles(folderPath, "*.pdf");

    foreach (var file in pdfFiles)
    {
        var pdf = PdfDocument.FromFile(file);
        var text = pdf.ExtractAllText();

        // Save extracted text
        var textFile = Path.ChangeExtension(file, ".txt");
        File.WriteAllText(textFile, text);

        Console.WriteLine($"Parsed: {Path.GetFileName(file)} - {text.Length} characters");
    }
}
$vbLabelText   $csharpLabel

這些範例展示如何將 PDF 解析整合到 Web 應用程式和批次處理場景中。 對於進階實現,可以探索非同步和多執行緒技術,以提高處理多個 PDF 時的效能。

準備好要看看你還能做什麼了嗎? 請造訪我們的教學頁面:編輯 PDF

常見問題解答

如何用 C# 從 PDF 檔案中萃取所有文字?

您可以使用 IronPDF 的 ExtractAllText 方法提取 PDF 文件中的所有文本。只需使用 IronPdf.FromFile("sample.pdf") 載入您的 PDF,並呼叫 ExtractAllText() 以擷取所有文字內容,同時保持原始格式。

在 .NET 中解析 PDF 的最簡單方法是什麼?

最簡單的方法是使用 IronPDF,只需一行程式碼:var text = IronPdf.FromFile("sample.pdf").ExtractAllText().此方法可從整個 PDF 文件中抽取每一行文字,所需的設定極少。

我可以從 PDF 的特定頁面中擷取文字嗎?

是的,IronPDF 提供 ExtractTextFromPage 方法來從個別頁面中提取文字。這可讓您針對 PDF 文件的特定部分,而非一次抽取所有內容。

如何在 C# 中解析受密碼保護的 PDF?

IronPDF 支援解析密碼保護的 PDF。使用 PdfDocument.FromFile("protected.pdf", "password123") 載入受保護的文件,然後調用 ExtractAllText() 來提取文本內容。

我可以從 URL 而非本機檔案解析 PDF 嗎?

是的,IronPDF 可以使用 PdfDocument.FromUrl("https://example.com/document.pdf") 直接從 URL 解析 PDF。從 URL 載入 PDF 之後,使用 ExtractAllText() 來提取文字內容。

PDF 解析器支援哪些平台?

IronPDF 支援跨多種平台的 PDF 解析,包括 Windows 應用程式、Linux、macOS 和 Azure 雲端部署,為您的 .NET 應用程式提供全面的跨平台相容性。

PDF 解析器在擷取過程中會保持文字格式嗎?

是的,IronPDF 的 ExtractAllText 方法可在提取過程中保持 PDF 內容的原始格式,確保解析後的文字保留來源文件的結構和排版。

我可以從 PDF 中同時擷取文字與影像嗎?

IronPDF 支持從 PDF 文檔中提取文本和图像。除了用於文字萃取的 ExtractAllText 方法之外,該函式庫還提供了從 PDF 文件的特定部分萃取影像的附加功能。

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。

準備好開始了嗎?
Nuget 下載 17,803,474 | 版本: 2026.3 剛剛發布
Still Scrolling Icon

還在滾動嗎?

想快速取得證據? PM > Install-Package IronPdf
運行範例看著你的HTML程式碼變成PDF檔。