Convert PDF file to Image in ASP.NET Core

26 Apr 20242 minutes to read

The Syncfusion PDF to Image converter is a .NET library used to convert PDF document to image in ASP.NET Core application.

Steps to convert PDF document to Image in ASP.NET Core application

Step 1: Create a new C# ASP.NET Core Web Application project.
Create ASP.NET Core Web application

Step 2: In configuration windows, name your project and select Next.
Configuration window1
Configuration window2

Step 3: Install Syncfusion.PdfToImageConverter.Net NuGet package as reference to your .NET Standard applications from NuGet.org.

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 reference to your applications from NuGet.org.

Step 4: A default controller with name HomeController.cs gets added on creation of ASP.NET Core project. Include the following namespaces in that HomeController.cs file.

  • C#
  • using Syncfusion.PdfToImageConverter;

    Step 5: Add a new button in index.cshtml as shown below.

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

    Step 6: Add a new action method named ExportToImage in HomeController.cs and include the below code example to convert PDF document to Image using Convert method in PdfToImageConverter class.

  • C#
  • //Initialize 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);
    }

    By executing the program, you will get the image as follows.
    Convert PDFToImage output