Convert PDF to Image without using Ghostscript DLL

Use LibPdf, for PDF to Image conversion

LibPdf library converts converts PDF file to an image. Supported image formats are PNG and BMP, but you can easily add more.

Usage example:

using (FileStream file = File.OpenRead(@"..\path\to\pdf\file.pdf")) // in file
{
    var bytes = new byte[file.Length];
    file.Read(bytes, 0, bytes.Length);
    using (var pdf = new LibPdf(bytes))
    {
        byte[] pngBytes = pdf.GetImage(0,ImageType.PNG); // image type
        using (var outFile = File.Create(@"..\path\to\pdf\file.png")) // out file
        {
            outFile.Write(pngBytes, 0, pngBytes.Length);
        }
    }
}

ImageMagick, you should also look at this freely available and powerful tool. It's capable of doing what you want and also provides some .NET bindings (as well as bindings to several other languages).

In its simplest form, it's just like writing a command

convert file.pdf imagefile.png

You can use below any one library for PDF to Image conversion

Use Aspose.pdf link below: http://www.aspose.com/docs/display/pdfnet/Convert+all+PDF+pages+to+JPEG+Images

code sample:

Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(MyPdfPath));
using (FileStream imageStream = new FileStream(MyOutputImage.png, FileMode.Create))
{
     Resolution resolution = new Resolution(300);
    PngDevice pngDevice = new PngDevice(resolution);
    pngDevice.Process(pdfDocument.Pages[PageNo], MyOutputImage);
    imageStream.Close();
}

Use Bytescout PDF Renderer link below: http://bytescout.com/products/developer/pdfrenderersdk/convert-pdf-to-png-basic-examples

code sample :

MemoryStream ImageStream = new MemoryStream();
RasterRenderer renderer = new RasterRenderer();
renderer.RegistrationName = "demo";
renderer.RegistrationKey = "demo";
// Load PDF document.
renderer.LoadDocumentFromFile(FilePath);
for (int i = 0; i < renderer.GetPageCount(); i++)
{
    // Render first page of the document to PNG image file.
    renderer.RenderPageToStream(i, RasterOutputFormat.PNG, ImageStream);
}
Image im = Image.FromStream(ImageStream);
im.Save("MyOutputImage.png");
ImageStream.Close();

Try Freeware.Pdf2Png, check below url:

PDF to PNG converter.

byte[] png = Freeware.Pdf2Png.Convert(pdf, 1);

https://www.nuget.org/packages/Freeware.Pdf2Png/1.0.1?_src=template

In the about info, It said MIT license, I check it on March 22, 2022. But as said Mitya, please double check.

enter image description here


the best and free nuget package that you can save every page of your Pdf to png and with custom resilution Docnet.core this can be use in the .net core project.

they have github and nice examples but here i want to add my code for reading en pdf with more that one page

        string webRootPath = _hostingEnvironment.WebRootPath;
        string fullPath = webRootPath + "/uploads/user-manual/file.pdf";
        string fullPaths = webRootPath + "/uploads/user-manual";

        using (var library = DocLib.Instance)
        {
            using (var docReader = library.GetDocReader(fullPath, 1080, 1920))
            {
                for (int i = 1; i < docReader.GetPageCount(); i++)
                {
                    using (var pageReader = docReader.GetPageReader(i))
                    {
                        var bytes = EmailTemplates.GetModifiedImage(pageReader);

                        System.IO.File.WriteAllBytes(fullPaths+"/page_image_" +i+".png", bytes);
                    }
                }

            }
        }

Other functions you can find in thier github repo.

Tags:

C#