PDF is considered to be a suitable document format for printing and sharing. However, there might be a case when you need to convert pages in a PDF file to PNG images. For example, when you want to embed PDF pages on a web page or generate the PDF’s cover, etc. In this article, you will learn how to automate the PDF to PNG C# conversion from within your .NET applications.
C# PDF to PNG Converter API – Free Download
Aspose.PDF for .NET is a powerful PDF file management API that lets you manipulate PDF documents from within your .NET applications. The API allows you to create, edit, and convert PDF documents seamlessly. You can either download the API or install it using NuGet.
PM> Install-Package Aspose.Pdf
PDF to PNG C# Conversion
The following are the steps to convert the pages in a PDF document to PNG images using Aspose.PDF for .NET.
- Load the PDF file using Document class.
- Loop through the pages of the PDF using Document.Pages collection.
- In each iteration, create a FileStream object for the output PNG image.
- Create and initialize an object of PngDevice object.
- Convert page to PNG using PngDevice.Process(Page, Stream) method.
The following code sample shows how to convert pages in PDF to PNG using C#.
Convert Single Page of PDF to PNG
You can also convert only a single page of PDF to PNG. In that case, you can access the desired page from Document.Pages collection. The following are the steps to convert only a single page of PDF to PNG.
- Load the PDF file using Document class.
- Create FileStream for the output PNG image.
- Create and initialize the PngDevice object.
- Convert page to PNG using PngDevice.Process(Page, Stream).
The following code sample shows how to convert a single page in PDF to PNG.
Conclusion
In this post, you have learned how to convert pages in a PDF file to PNG images using C#. The code samples have shown how to convert all or desired pages of PDF to PNG images. You can explore more about .NET PDF API using the documentation.