文档半岛外围网上直营>>Aspose.PDF使用教程>>Aspose.PDF功能演示:使用C#将PDF页面转换为PNG图像
Aspose.PDF功能演示:使用C#将PDF页面转换为PNG图像
PDF被认为是适合打印和共享的文档格式。但是,在某些情况下,需要将PDF文件中的页面转换为PNG图像。例如,当要将PDF页面嵌入网页或生成PDF封面等时。在本文中,将学习如何在.NET应用程序中自动将PDF转换为PNG。
- PDF至PNG的转换
- 将PDF单页转换为PNG
使用C#将PDF至PNG C#的转换
以下是使用Aspose.PDF for .NET将PDF文档中的页面转换为PNG图像的步骤。
- 使用Document类加载PDF文件。
- 使用Document.Pages集合循环浏览PDF页面。
- 在每次迭代中,为输出的PNG图像创建一个FileStream对象。
- 创建并初始化一个PngDevice对象的对象。
- 使用PngDevice.Process(Page,Stream)方法将页面转换为PNG 。
下面的代码示例演示如何使用C#将PDF中的页面转换为PNG。
// Open PDF document
Document pdfDocument = new Document("Document.pdf");
// Loop through each page
foreach (var page in pdfDocument.Pages)
{
// Create file stream for output image
using (FileStream imageStream = new FileStream(string.Format("page_{0}.png", page.Number), FileMode.Create))
{
// Create Resolution object
Resolution resolution = new Resolution(300);
// Create Png device with specified attributes
// Width, Height, Resolution
PngDevice PngDevice = new PngDevice(500, 700, resolution);
// Convert a particular page and save the image to stream
PngDevice.Process(page, imageStream);
// Close stream
imageStream.Close();
}
}
使用C#将PDF单页转换为PNG
有时只能将PDF的一页转换为PNG。在这种情况下,您可以从Document.Pages集合中访问所需的页面。以下是仅将PDF的单个页面转换为PNG的步骤。
- 使用Document类加载PDF文件。
- 为输出的PNG图像创建FileStream。
- 创建并初始化PngDevice对象。
- 使用PngDevice.Process(Page,Stream)将页面转换为PDF 。
以下代码示例显示了如何将PDF中的单个页面转换为PNG。
// Open PDF document
Document pdfDocument = new Document("Document.pdf");
// Set page index
int page = 1;
// Create FileStream for the output image
using (FileStream imageStream = new FileStream(string.Format("page_{0}.png", page), FileMode.Create))
{
// Create Resolution object
Resolution resolution = new Resolution(300);
// Create Png device with specified attributes
// Width, Height, Resolution
PngDevice PngDevice = new PngDevice(500, 700, resolution);
// Convert a particular page and save the image to stream
PngDevice.Process(pdfDocument.Pages[page], imageStream);
// Close stream
imageStream.Close();
}
还想要更多吗?您可以点击阅读【2020 · Aspose最新资源整合】,查找需要的教程资源。如果您有任何疑问或需求,请随时加入Aspose技术交流群(761297826),我们很高兴为您提供查询和咨询。

QQ交谈
在线咨询

渝公网安备
50010702500608号

客服热线