Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PDF to bmp Images (12 pages = 12 images)

I have to deconstruct/extract a pdf page by page into bitmap images. This will be done on a server via a web service which I've setup. How do I get this right? It has to be page by page (1 page per image). I am really stuck and I know one of you geniuses have the answer that I've been looking for.

I have tried: http://www.pdfsharp.net/wiki/ExportImages-sample.ashx Which didn't work correctly.

I am using C#; The PDF is not password protected; If this solution could take a Uri as a parameter for the location of the PDF it would be excellent!

The solution should not be reliant on Acrobat PDF Reader at all

I have been struggling for a very long time trying to use MigraDoc and PDFSharp and their alternatives to achieve the aforementioned problem.

ANY help/advice/code would be greatly appreciated!!

Thanks in advance!

like image 728
msbarnard Avatar asked Mar 19 '23 02:03

msbarnard


1 Answers

LibPdf

This 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.BMP); // image type
        using (var outFile = File.Create(@"..\path\to\pdf\file.bmp")) // out file
        {
            outFile.Write(pngBytes, 0, pngBytes.Length);
        }
    }
}

Or Bytescout PDF Renderer SDK

using System;

using Bytescout.PDFRenderer;


namespace PDF2BMP
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create an instance of Bytescout.PDFRenderer.RasterRenderer object and register it.
            RasterRenderer renderer = new RasterRenderer();
            renderer.RegistrationName = "demo";
            renderer.RegistrationKey = "demo";

            // Load PDF document.
            renderer.LoadDocumentFromFile("multipage.pdf");

            for (int i = 0; i < renderer.GetPageCount(); i++)
            {
                // Render first page of the document to BMP image file.
                renderer.RenderPageToFile(i, RasterOutputFormat.BMP, "image" + i + ".bmp");
            }

            // Open the first output file in default image viewer.
            System.Diagnostics.Process.Start("image0.bmp");
        }
    }
}
like image 77
Moez Rebai Avatar answered Mar 23 '23 01:03

Moez Rebai