How to convert PDF to PNG using C# and VB.NET?
Syncfusion Essential PDF is a .NET PDF Libraries used to create, read, and edit PDF documents. Using Pdf Viewer library, you can convert PDF to PNG in C# and VB.NET.
Steps to convert a page of the PDF file into a PNG image programmatically:
- Create a new C# console application project.
- Install the Syncfusion.PdfViewer.Windows NuGet package as a reference to your .NET Framework application from NuGet.org.
- Include the following namespace in the Program.cs file.
C#
using Syncfusion.Pdf.Parsing;
using Syncfusion.Windows.Forms.PdfViewer;
using System.Drawing;
using System.Drawing.Imaging;
VB.NET
Imports Syncfusion.Pdf.Parsing
Imports Syncfusion.Windows.Forms.PdfViewer
Imports System.Drawing
Imports System.Drawing.Imaging
- Use the following code snippet to convert a page of the PDF file to a PNG image.
C#
//Initialize the PdfViewer Control
PdfViewerControl pdfViewer = new PdfViewerControl();
//Load the input PDF file
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("../../Data/Barcode.pdf");
pdfViewer.Load(loadedDocument);
//Export the particular PDF page as image at the page index of 0
Bitmap image = pdfViewer.ExportAsImage(0);
// Save the image.
image.Save("Sample.png", ImageFormat. Png);
VB.NET
'Initialize the PdfViewer Control
Dim pdfViewer As PdfViewerControl = New PdfViewerControl()
'Load the input PDF file
Dim loadedDocument As PdfLoadedDocument = New
PdfLoadedDocument("../../Data/Barcode.pdf")
pdfViewer.Load(loadedDocument)
'Export the particular PDF page as image at the page index of 0
Dim image As Bitmap = pdfViewer.ExportAsImage(0)
'Save the image.
image.Save("Sample. png ", ImageFormat. Png)
Steps to convert a specific range of pages of the PDF file into PNG images programmatically:
- Include the following namespace in the Program.cs file.
C#
using Syncfusion.Pdf.Parsing;
using Syncfusion.Windows.Forms.PdfViewer;
using System.Drawing;
using System.Drawing.Imaging;
VB.NET
Imports Syncfusion.Pdf.Parsing
Imports Syncfusion.Windows.Forms.PdfViewer
Imports System.Drawing
Imports System.Drawing.Imaging
- Use the following code snippet to convert the specific range of pages of a PDF file that can be exported as PNG images.
C#
//Initialize the PdfViewer Control
PdfViewerControl pdfViewer = new PdfViewerControl();
//Load the input PDF file
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("../../Data/Barcode.pdf");
pdfViewer.Load(loadedDocument);
//Export all the pages as images at the specific page range
Bitmap[] image = pdfViewer.ExportAsImage(0, loadedDocument.Pages.Count - 1);
for (int i = 0; i < image.Length; i++)
{
// Save the image.
image[i].Save("Sample" + i.ToString()+".png", ImageFormat. Png);
}
VB.NET
'Initialize the PdfViewer Control
Dim pdfViewer As PdfViewerControl = New PdfViewerControl()
'Load the input PDF file
Dim loadedDocument As PdfLoadedDocument = New PdfLoadedDocument("../../Data/Barcode.pdf")
pdfViewer.Load(loadedDocument)
'Export all the pages as images at the specific page range
Dim image As Bitmap() = pdfViewer.ExportAsImage(0, loadedDocument.Pages.Count - 1)
For i As Integer = 0 To image.Length - 1
'Save the image.
image(i).Save("Sample" + i.ToString() + ".png", ImageFormat.Png)
Next
You can download the working sample from PDFToPNGSample.Zip
By executing the program, you will get the image as follows.
Take a moment to peruse the documentation, where you can find other features like converting Word to PDF, Excel to PDF, XPS to PDF, and HTML to PDF with code examples.
Refer here to explore the rich set of Syncfusion Essential PDF features.
Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering the Syncfusion license key in your application to use the components without a trail message.
See Also:
https://support.syncfusion.com/kb/article/8092/how-to-convert-pdf-to-jpg-using-c-and-vb-net
https://support.syncfusion.com/kb/article/9462/how-to-convert-pdf-to-image-in-wpf-pdfviewer
https://support.syncfusion.com/kb/article/9455/how-to-convert-pdf-to-jpg-in-wpf-pdfviewer
https://www.syncfusion.com/blogs/post/pdf-to-image-conversion-is-made-easy-with-wpf-pdf-viewer.aspx
Conclusion
I hope you enjoyed learning about how toconvert PDF to PNG using C# and VB.NET.
You can refer to our .NET PDF Libraries to know about its other groundbreaking feature representations. You can also explore our documentation to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!