文檔金喜正規買球>>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),我們很高興為您提供查詢和咨詢。