TL;DR: Learn how to effortlessly convert PDF pages into images using Syncfusion’s PdfToImageConverter in C#. This guide covers step-by-step instructions for various platforms, including ASP.NET Core, ASP.NET MVC, WinForms, and WPF. Discover how to install the necessary NuGet packages, set up your project, and implement the conversion code to efficiently transform PDF files into images.
Exporting PDF document pages as images can often be a challenging task. To simplify this process, we’ve developed our Syncfusion PdfToImageConverter, which uses PDFium to convert PDF documents into images. PDFium, the same engine used in Google Chrome for rendering PDF files, provides accurate and robust PDF rendering.
In this blog, we’ll see how to export PDF pages as images across different platforms easily.
The following table displays the supported frameworks and platforms and the corresponding Syncfusion PdfToImageConverter NuGet packages they support.
Frameworks | Platforms | NuGet package |
net6.0-windows7.0/ net461 / net40 | WinForms | |
WPF | ||
net6.0/ net7.0 / net8.0 / netstandard2.0 | ASP.NET Core, Blazor | |
net45 / net40 | ASP.NET MVC | |
Note: For more details, refer to PDF to image conversion in C# documentation.
Follow these steps to convert your PDF document into images in an ASP.NET Core app:
Note: If you want to use the PdfToImageConverter in the Linux environment, you need to install the SkiaSharp.NativeAssets.Linux v2.88.6 NuGet package as a reference to your app from NuGet Gallery.
using Syncfusion.PdfToImageConverter;
@{Html.BeginForm("ExportToImage", "Home", FormMethod.Post); { <div> <input type="submit" value="Convert Image" style="width:150px;height:27px" /> </div> } Html.EndForm(); }
//Initialize the PDF to Image converter. PdfToImageConverter imageConverter = new PdfToImageConverter(); //Load the PDF document as a stream. FileStream inputStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.ReadWrite); imageConverter.Load(inputStream); //Convert PDF to image. Stream outputStream = imageConverter.Convert(0, false, false); MemoryStream stream = outputStream as MemoryStream; byte[] bytes = stream.ToArray(); using (FileStream output = new FileStream("output.png", FileMode.OpenOrCreate, FileAccess.ReadWrite)) { output.Write(bytes, 0, bytes. Length); }
After executing the above code examples, we’ll get the following output.
Also, refer to converting a PDF to images in an ASP.NET Core app GitHub demo.
Similarly, we can use the PdfToImageConverter.Net NuGet to convert a PDF to images in a Blazor app.
Note: The PdfToImageConverter.Net relies on the Pdfium assembly. However, it is not supported in the Blazor WebAssembly due to the framework’s restrictions on accessing assemblies.
Follow these steps to convert your PDF document into images in an ASP.NET MVC app:
using Syncfusion.PdfToImageConverter; using System.Drawing; using System.IO;
@{Html.BeginForm("ExportToImage", "Home", FormMethod.Post); { <div> <input type="submit" value="Convert Image" style="width:150px;height:27px" /> </div> } Html.EndForm(); }
//Initialize the PDF to Image converter. PdfToImageConverter imageConverter = new PdfToImageConverter(); //Load the PDF document as a stream. FileStream inputStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.ReadWrite); imageConverter.Load(inputStream); //Convert PDF to image. Stream outputStream = imageConverter.Convert(0, false, false); MemoryStream stream = outputStream as MemoryStream; return File(stream.ToArray(), System.Net.Mime.MediaTypeNames.Image.Jpeg, "sample.jpeg");
Note: Also, refer to converting a PDF to images in an ASP.NET MVC app GitHub demo.
Follow these steps to convert your PDF document into images in a WinForms app:
using System; using System.Windows.Forms;
private Button btnCreate; private Label label; private void InitializeComponent() { btnCreate = new Button(); label = new Label(); //Label. label.Location = new System.Drawing.Point(0, 40); label.Size = new System.Drawing.Size(426, 35); label.Text = "Click the button to convert PDF file to Image"; label.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; //Button. btnCreate.Location = new System.Drawing.Point(180, 110); btnCreate.Size = new System.Drawing.Size(85, 26); btnCreate.Text = "Convert PDF to Image"; btnCreate.Click += new EventHandler(btnCreate_Click); //Create PDF. ClientSize = new System.Drawing.Size(450, 150); Controls.Add(label); Controls.Add(btnCreate); Text = "Convert PDF to Image"; }
using Syncfusion.PdfToImageConverter; using System.Drawing;
//Initialize the PDF to Image converter. PdfToImageConverter imageConverter = new PdfToImageConverter(); //Load the PDF document as a stream. FileStream inputStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.ReadWrite); imageConverter.Load(inputStream); //Convert PDF to image. Stream outputStream = imageConverter.Convert(0, false, false); Bitmap image = new Bitmap(outputStream); image. Save("sample.png");
Note: For more details, refer to converting a PDF to images in a WinForms app GitHub demo.
Follow these steps to convert your PDF document into images in a WPF app:
using Syncfusion.PdfToImageConverter; using System.Drawing; using System.IO; using System.Windows;
<Grid HorizontalAlignment="Left" Margin="0,0,0,-0.333" Width="793"> <Button Content="Convert PDF to Image" HorizontalAlignment="Left" Margin="318,210,0,0" VerticalAlignment="Top" Width="166" Click=" btnCreate_Click " Height="19"/> <TextBlock HorizontalAlignment="Left" Margin="222,177,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="17"/> <TextBlock HorizontalAlignment="Left" Margin="291,175,0,0" TextWrapping="Wrap" Text="Click the button to convert PDF to Image." VerticalAlignment="Top"/> </Grid>
//Initialize the PDF to Image converter. PdfToImageConverter imageConverter = new PdfToImageConverter(); //Load the PDF document as a stream. FileStream inputStream = new FileStream("Input.pdf", FileMode.Open,FileAccess.ReadWrite); imageConverter.Load(inputStream); //Convert PDF to image. Stream outputStream = imageConverter.Convert(0, false, false); Bitmap image = new Bitmap(outputStream); image. Save("sample.png");
Note: For more details, refer to converting a PDF to images in a WPF app GitHub demo.
Thanks for reading! In this blog, we’ve seen how to convert a PDF document to images using the Syncfusion PDFtoImageConverter in C# across various supported platforms. Try out the steps in this blog and provide your feedback in the comments below!
The existing customers can download the latest version of Essential Studio® from the License and Downloads page. If you are new, try our 30-day free trial to explore our incredible features.
You can also contact us through our support forums, support portal, or feedback portal. We are always happy to assist you!