DEV Community

Cover image for Easily Convert Your PDF to Images in C#
Lucy Muturi for Syncfusion, Inc.

Posted on • Originally published at syncfusion.com on

Easily Convert Your PDF to Images in C#

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.

Supported frameworks and platforms

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

Syncfusion.PdfToImageConverter.WinForms

WPF

Syncfusion.PdfToImageConverter.WPF

net6.0/ net7.0 / net8.0 / netstandard2.0

ASP.NET Core, Blazor

Syncfusion.PdfToImageConverter.Net

net45 /  net40

ASP.NET MVC

Syncfusion.PdfToImageConverter.AspNet.Mvc4

Syncfusion.PdfToImageConverter.AspNet.Mvc5

Note: For more details, refer to PDF to image conversion in C# documentation.

Convert PDF documents to images in ASP.NET Core

Follow these steps to convert your PDF document into images in an ASP.NET Core app:

1.First, create a new C# ASP.NET Core web application project.
2.Install the Syncfusion.PdfToImageConverter.Net NuGet package as a reference to your .NET Standard apps from the NuGet Gallery.

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.

3.A default file named HomeController.cs gets added when creating the ASP.NET Core project. Now, include the following namespace in the HomeController.cs file.

using Syncfusion.PdfToImageConverter;
Enter fullscreen mode Exit fullscreen mode

4.Let’s add a new button to convert a PDF document to images in the index.cshtml file, as shown below.

@{Html.BeginForm("ExportToImage", "Home", FormMethod.Post);
    {
        <div>
         <input type="submit" value="Convert Image" style="width:150px;height:27px" />
        </div>
    }
    Html.EndForm();
}
Enter fullscreen mode Exit fullscreen mode

5.Add a new action method named ExportToImage in the HomeController.cs file and include the following code example to convert PDF documents to images using the Convert method in the PdfToImageConverter class.

//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);
}
Enter fullscreen mode Exit fullscreen mode

After executing the above code examples, we’ll get the following output.

Converting a PDF document to image in an ASP.NET Core app

Converting a PDF document to image in an ASP.NET Core app

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.

Convert PDF document to images in ASP.NET MVC

Follow these steps to convert your PDF document into images in an ASP.NET MVC app:

1.Create a new C# ASP.NET web app (.NET Framework) project.
2.Install the Syncfusion.PdfToImageConverter.AspNet.Mvc5 NuGet package as a reference to your .NET app from NuGet Gallery.
3.Include the following namespaces in the HomeController.cs file.

using Syncfusion.PdfToImageConverter;
using System.Drawing;
using System.IO;
Enter fullscreen mode Exit fullscreen mode

4.Let’s add a new button to convert your PDF document to images in the index.cshtml, as shown below.

@{Html.BeginForm("ExportToImage", "Home", FormMethod.Post);
    {
        <div>
            <input type="submit" value="Convert Image" style="width:150px;height:27px" />
        </div>
    }
    Html.EndForm();
 }
Enter fullscreen mode Exit fullscreen mode

5.Add a new action method named ExportToImage in the HomeController.cs file and include the following code example to convert a PDF document to images using the Convert method in the PdfToImageConverter class.

//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");
Enter fullscreen mode Exit fullscreen mode

Note: Also, refer to converting a PDF to images in an ASP.NET MVC app GitHub demo.

Convert PDF document to images in Windows Forms

Follow these steps to convert your PDF document into images in a WinForms app:
1.Create a new Windows Forms app project.
2.Install the Syncfusion.PdfToImageConverter.WinForms NuGet package as a reference to your WinForms app from NuGet Gallery.
3.Add the following namespaces into the Form1.Designer.cs file.

using System;
using System.Windows.Forms;
Enter fullscreen mode Exit fullscreen mode

4.Add a new button in the Form1.Designer.cs file to convert your PDF document to images as follows.

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";
}
Enter fullscreen mode Exit fullscreen mode

5.Include the following namespaces in the Form1.cs file.

using Syncfusion.PdfToImageConverter;
using System.Drawing;
Enter fullscreen mode Exit fullscreen mode

6.Now, create the btnCreate_Click event and add the following code in it to convert a PDF document to images using the Convert method in the PdfToImageConverter

//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");
Enter fullscreen mode Exit fullscreen mode

Note: For more details, refer to converting a PDF to images in a WinForms app GitHub demo.

Convert PDF document to images in WPF

Follow these steps to convert your PDF document into images in a WPF app:

  1. Create a new WPF app project.
  2. Install the Syncfusion.PdfToImageConverter.WPF NuGet package as a reference to your WPF app from NuGet Gallery.
  3. Include the following namespaces in the MainWindow.xaml.cs file.
using Syncfusion.PdfToImageConverter;
using System.Drawing;
using System.IO;
using System.Windows;
Enter fullscreen mode Exit fullscreen mode

4.Add a new button in the MainWindow.xaml file to convert a PDF to images.

<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>
Enter fullscreen mode Exit fullscreen mode

5.Add the following code in the btnCreate_Click event to convert a PDF document to images using the Convert method in the PdfToImageConverter

//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");
Enter fullscreen mode Exit fullscreen mode

Note: For more details, refer to converting a PDF to images in a WPF app GitHub demo.

Conclusion

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!

Related blogs

Top comments (0)