如何从PDF文件中移除密码
您的企业在PDF安全性和合规性年度订阅上花费过多。考虑使用IronSecureDoc,它提供数字签名、编辑、加密和保护等管理SaaS服务的解决方案,全部一次性付款。了解更多关于IronSecureDoc的信息
从 PDF 中提取数据对于节省手动输入时间至关重要。 本文说明了开发人员如何使用 IronPDF 库从 PDF 文档中提取文本和图像。
如何在C#中从PDF中提取数据
- 从 PDF 中提取数据 C# 库
- 在Visual Studio中创建一个新项目
- 将库安装到您的项目中
- 从特定页面提取数据并从 PDF 中提取特定内容
- 查看 PDF 文档中的数据输出
IronPDF:C# PDF库
IronPDF 是一个可以用来创建、编辑和转换 PDF 文件的 .NET 库。 它为开发人员在其应用中提供了易于使用的 API。 它是全球创建、编辑和转换 PDF 文件的最受欢迎的库之一。 使用 IronPDF,您可以创建直截了当且快速的 PDF 解决方案。 您的文字将针对每个文档进行定制,您的布局将被设置为便于阅读,并且您的图形将得到 .NET 程序的帮助进行设计。
IronPDF 库具有从 PDF 文件中提取数据的出色功能。 本文将研究如何使用 IronPDF 提取数据。 首先,需要创建或打开一个 C# 项目。 让我们进入下一部分。
在Visual Studio中创建或打开C#项目
本教程建议使用最新版本的 Visual Studio。
打开 Visual Studio 后,按照下面的步骤创建一个新的 C# 项目。 如果已有您想使用的项目,则跳过这些步骤并直接进入下一部分。
- 打开 Visual Studio
- 点击"创建一个新项目"按钮。
Visual Studio 打开 UI
- 从模板中选择"C# 控制台应用程序"。
创建一个新项目
- 为项目命名,并点击下一步按钮。
- 根据项目的需求选择一个 .NET Framework,并点击创建按钮。
.NET Framework选择
Visual Studio 将生成一个新的 C# .NET 项目。
安装IronPDF库
IronPDF库可以通过多种方式安装。
使用包管理器控制台
- 通过转到 工具 > NuGet 包管理器 > 包管理器控制台打开包管理器控制台。
- 运行以下命令来安装 IronPDF 库:
包管理器控制台选项卡中的安装进度
安装完成后,您将在解决方案资源管理器的dependencies部分看到IronPDF依赖项,如下所示。
在Solution Explorer中引用IronPDF包
使用 NuGet 包管理器
另一种安装 IronPDF 库的方法是使用 Visual Studio 集成的 NuGet 包管理器 UI。
- 从主菜单中转到工具。 从下拉菜单中悬停在"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方法获取所有嵌入的图像。 - 使用循环遍历文档的每一页,通过
ExtractImagesFromPage从特定页面提取文本和图像。
结论
IronPDF 允许开发人员轻松地从 PDF 文件中提取文本和图像。 使用ExtractAllImages,可以立即提取PDF文件的全部内容。 或者,可以使用这些方法从特定页面提取内容。 前面的代码演示了如何使用这两种方法从多个页面读取文本和图像。
此外,IronPDF 提供了如渲染图表、添加条形码、使用密码增强安全性、水印和程序化处理 PDF 表单等功能。
IronPDF 在开发期间可免费使用,但需要付费才能进行商业用途。 可免费使用IronPDF 的试用版以进行生产使用。
购买[Iron Software 文档库的完整套件](Iron Suite),仅需支付两个IronPDF Lite 授权的费用。
Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。