
引入
PDF 文件常常包含有价值的图像,需要 提取重新使用,分析或转换. 本文提供了一个全面的步骤指南关于 如何提取图像从 PDF 文件在 C# 使用 Aspose.PDF for .NET. 这个强大的 .NET 插件 确保 高质量的图像获取,保存 原始格式和分辨率。
为什么要从PDF中提取图像?
从PDF中提取图像可能有益于几个原因:
- 重新使用图像 报告、演示文稿或档案。
- 将 PDF 图像 转换为单独的文件,用于编辑或进一步处理。
- 自动图像提取 用于大批 PDF 处理。
- 保留高分辨率图像,无损质量。
内容表
1. 设置 Aspose.PDF for Image Extraction
要从PDF中提取图像,我们使用 Aspose.PDF for .NET。
- 高精度图像提取 无数据损失。
- 支持多个图像格式(JPEG、PNG、BMP等)。
- 从多页PDF中自动提取。
安装
您可以使用 NuGet 轻松安装图书馆:
PM> Install-Package Aspose.PDF
否则,下载 DLL 从 Aspose 下载页面.
2. 从 PDF 文件中提取图像在 C#
遵循以下步骤 从 PDF 编程中提取图像:
- 下载 PDF 文件 使用
Document
班级。 - 通过每个页面 访问图像。
- 提取每个图像 从
Page.Resources.Images
. - 保存所提取的图像 在您想要的格式中。
代码例子
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Open document | |
Document pdfDocument = new Document("candy.pdf"); | |
int imageCounter = 1; | |
// Loop through pages | |
foreach (var page in pdfDocument.Pages) | |
{ | |
// Loop through all images | |
foreach (XImage image in page.Resources.Images) | |
{ | |
// Create file stream for image | |
FileStream outputImage = new FileStream(String.Format("Page{0}_Image{1}.jpg", page.Number, imageCounter), FileMode.Create); | |
// Save output image | |
image.Save(outputImage, ImageFormat.Jpeg); | |
// Close stream | |
outputImage.Close(); | |
imageCounter++; | |
} | |
// Reset counter | |
imageCounter = 1; | |
} |
这种方法确保 无缝提取 同时保持 原始图像质量。
3、保存不同格式的提取图像
Aspose.PDF 允许您在多个格式中保存提取的图像:
格式 | 福利 |
---|---|
◎JPEG | 高压,适合网页使用。 |
◎PNG | 无损压缩高品质的图像。 |
“BMP” | Bitmap 格式为详细图像保存。 |
要 保存以不同的格式 提取的图像,请在保存方法中简单地修改文件扩展。
4. Batch 图像从多个 PDF 提取
要从 多个 PDF 同时 提取图像,您可以通过下面的目录滚动:
string[] files = Directory.GetFiles("input_pdfs", "*.pdf");
foreach (string file in files)
{
Document pdfDocument = new Document(file);
foreach (var page in pdfDocument.Pages)
{
foreach (var image in page.Resources.Images)
{
FileStream stream = new FileStream("output_" + Path.GetFileName(file) + ".jpg", FileMode.Create);
image.Save(stream, ImageFormat.Jpeg);
stream.Close();
}
}
}
此方法 自动大批 PDF 图像提取 有效。
5、获得免费许可证
要解锁 完整的 Aspose.PDF 功能,您可以申请 (免费临时许可证)(https://purchase.aspose.com/temporary-license)。
要了解更多详细信息,请参阅 (官方文件)https://docs.aspose.net/pdf/net/ 或在 Aspose forum 上提问。
结论和额外资源
总结
在此指南中,我们涵盖了:
- 如何使用C#从PDF中提取图像
- 保留图像质量和格式
- Batch 处理多个 PDF 文件
了解更多
使用 Aspose.PDF for .NET,您可以有效地从 PDF 中提取、处理和管理图像. 开始使用 Aspose.PDF 今天 为高性能的 C# PDF 图像提取 仅从 $99 开始!