如何移除PDF文件上的密碼
您的業務在 PDF 安全性和合規性上的每年訂閱花費過大。考慮使用 IronSecureDoc,它提供如數位簽名、刪減、加密和保護等 SaaS 服務的管理解決方案,僅需一次性付款。了解更多關於 IronSecureDoc
從 PDF 提取資料對於節省手動輸入時間至關重要。 本文說明開發者如何使用 IronPDF 程式庫從 PDF 文件中提取文字和圖像。
如何在 C# 中從 PDF 提取資料
- 下載從 PDF 提取資料的 C# 程式庫
- 在 Visual Studio 中建立新專案
- 將程式庫安裝到您的專案中
- 從特定頁面提取資料並從 PDF 提取特定資料
- 查看來自 PDF 文件的資料輸出
IronPDF: C# PDF程式庫
IronPDF 是一個 .NET 程式庫,可以用來建立、編輯和轉換 PDF 文件。 它為開發者在應用程式中使用 API 提供了易於使用的解決方案。 它是全球最受歡迎的建立、編輯和轉換 PDF 文件的程式庫之一。 使用 IronPDF,您可以快速建立針對 PDF 的簡易解決方案。 您將自定義文字以適應每個文件,您的佈局將被設置以便於閱讀,而圖形將在 .NET 程式協助下設計。
IronPDF 程式庫提供了一個出色的特性來從 PDF 文件中提取資料。 本文將探討如何使用 IronPDF 提取資料。 首先,您需要建立或開啟一個 C# 專案。 讓我們繼續到下一節。
在 Visual Studio 中建立或開啟一個 C# 專案
本教學建議使用 Visual Studio 的最新版本。
Visual Studio 開啟後,請按照下列步驟建立一個新的 C# 專案。 如果您想使用現有的專案,則可以跳過這些步驟,直接進入下一節。
- 開啟 Visual Studio
- 點擊"建立新專案"按鈕。
Visual Studio 開啟介面
- 從範本中選擇"C# Console Application"。
建立新專案
- 給專案命名,然後點擊下一步按鈕。
- 根據專案要求選擇 .NET Framework,然後點擊建立按鈕。
.NET Framework 選擇
Visual Studio 現在將生成新的 C# .NET 專案。
安裝IronPDF程式庫
IronPDF程式庫可以通過多種方式安裝。
使用套件管理器控制台
- 通過進入工具 > NuGet 套件管理員 > 套件管理員控制台打開套件管理控制台。
- 運行以下命令以安裝 IronPDF 程式庫:
套件管理控制台標籤中的安裝進度
安裝後,您將在下方的 dependencies 區段中看到 IronPDF 依賴項。
在解決方案資源管理器中引用 IronPDF 套件
使用 NuGet 套件管理器
另一種安裝 IronPDF 程式庫的方法是使用 Visual Studio 的整合 NuGet 套件管理器介面。
- 前往主選單中的工具。 將滑鼠停留在下拉選單中的"NuGet 套件管理器"上,然後選擇"管理解決方案的 NuGet 套件..."。
導航至NuGet套件管理器
- 這將打開NuGet套件管理器窗口。 轉到瀏覽標籤,搜尋
IronPdf,然後按下 Enter。 - 從搜尋結果中選擇 IronPDF,然後點擊"安裝"按鈕開始安裝。
從NuGet套件管理器安裝IronPDF套件
從 PDF 檔案中提取資料
讓我們來看看以下使用 IronPDF 提取資料的程式碼:
// Import necessary namespaces
using IronPdf;
using System.Collections.Generic;
using System.Drawing;
public class PDFExtractor
{
public void ExtractDataFromPDF()
{
// Open a 128-bit encrypted PDF file by providing the filename and password
using PdfDocument pdf = PdfDocument.FromFile("encrypted.pdf", "password");
// Extract all text from the PDF document
string allText = pdf.ExtractAllText();
// Extract all images from the PDF document
IEnumerable<Image> allImages = pdf.ExtractAllImages();
// Iterate over each page in the PDF document
for (var index = 0; index < pdf.PageCount; index++)
{
int pageNumber = index + 1;
// Extract text from the specific page
string text = pdf.ExtractTextFromPage(index);
// Extract images from the specific page
IEnumerable<Image> images = pdf.ExtractImagesFromPage(index);
// Code to process the extracted text and images
//...
}
}
}' Import necessary namespaces
Imports IronPdf
Imports System.Collections.Generic
Imports System.Drawing
Public Class PDFExtractor
Public Sub ExtractDataFromPDF()
' Open a 128-bit encrypted PDF file by providing the filename and password
Using pdf As PdfDocument = PdfDocument.FromFile("encrypted.pdf", "password")
' Extract all text from the PDF document
Dim allText As String = pdf.ExtractAllText()
' Extract all images from the PDF document
Dim allImages As IEnumerable(Of Image) = pdf.ExtractAllImages()
' Iterate over each page in the PDF document
For index = 0 To pdf.PageCount - 1
Dim pageNumber As Integer = index + 1
' Extract text from the specific page
Dim text As String = pdf.ExtractTextFromPage(index)
' Extract images from the specific page
Dim images As IEnumerable(Of Image) = pdf.ExtractImagesFromPage(index)
' Code to process the extracted text and images
'...
Next index
End Using
End Sub
End Class' Import necessary namespaces
Imports IronPdf
Imports System.Collections.Generic
Imports System.Drawing
Public Class PDFExtractor
Public Sub ExtractDataFromPDF()
' Open a 128-bit encrypted PDF file by providing the filename and password
Using pdf As PdfDocument = PdfDocument.FromFile("encrypted.pdf", "password")
' Extract all text from the PDF document
Dim allText As String = pdf.ExtractAllText()
' Extract all images from the PDF document
Dim allImages As IEnumerable(Of Image) = pdf.ExtractAllImages()
' Iterate over each page in the PDF document
For index = 0 To pdf.PageCount - 1
Dim pageNumber As Integer = index + 1
' Extract text from the specific page
Dim text As String = pdf.ExtractTextFromPage(index)
' Extract images from the specific page
Dim images As IEnumerable(Of Image) = pdf.ExtractImagesFromPage(index)
' Code to process the extracted text and images
'...
Next index
End Using
End Sub
End Class在此程式碼範例中:
- 使用
FromFile方法載入輸入的 PDF 文件,此文件已加密並需要密碼。 - 使用
ExtractAllText方法從 PDF 中提取所有文字內容。 - 使用
ExtractAllImages方法獲取所有嵌入的圖片。 - 一個迴圈遍歷文件的每一頁,使用
ExtractTextFromPage和ExtractImagesFromPage從那特定頁提取文字和圖片。
總結
IronPDF 使開發者能輕鬆地從 PDF 文件中提取文字和圖片。 利用 ExtractAllText 和 ExtractAllImages,可以立即提取 PDF 文件的整個內容。 或者,這些方法可以用來從特定頁面提取內容。 前面的程式碼展示如何使用這兩種方法從多頁讀取文字和圖片。
此外,IronPDF 提供的功能還有渲染圖表,新增條形碼,使用密碼增強安全性,水印功能以及以程式化方式處理 PDF 表單。
IronPDF 在開發期間可免費使用,用於商業用途則需付費。 提供IronPDF 的免費試用可在生產環境中無需付費即用。
以兩個IronPDF Lite 授權的價格購買Iron Software 的全部文件程式庫套裝。
立即下載 IronPDF 開始從 PDF 中提取資料!
Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。