翻譯|使用教程|編輯:李顯亮|2020-11-30 10:10:04.673|閱讀 559 次
概述:在某些情況下,需要將PDF文件中的頁面轉(zhuǎn)換為PNG圖像。在本文中,將學(xué)習(xí)如何在.NET應(yīng)用程序中自動將PDF轉(zhuǎn)換為PNG。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
PDF被認(rèn)為是適合打印和共享的文檔格式。但是,在某些情況下,需要將PDF文件中的頁面轉(zhuǎn)換為PNG圖像。例如,當(dāng)要將PDF頁面嵌入網(wǎng)頁或生成PDF封面等時。在本文中,將學(xué)習(xí)如何在.NET應(yīng)用程序中自動將PDF轉(zhuǎn)換為PNG。
(安裝包僅提供部分功能,并設(shè)置限制,如需試用完整功能請。)
使用C#將PDF至PNG C#的轉(zhuǎn)換
以下是使用Aspose.PDF for .NET將PDF文檔中的頁面轉(zhuǎn)換為PNG圖像的步驟。
下面的代碼示例演示如何使用C#將PDF中的頁面轉(zhuǎn)換為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單頁轉(zhuǎn)換為PNG
有時只能將PDF的一頁轉(zhuǎn)換為PNG。在這種情況下,您可以從Document.Pages集合中訪問所需的頁面。以下是僅將PDF的單個頁面轉(zhuǎn)換為PNG的步驟。
以下代碼示例顯示了如何將PDF中的單個頁面轉(zhuǎn)換為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();
}
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn