PDF to bmp Images (12 pages = 12 images)

后端 未结 1 1651
臣服心动
臣服心动 2021-02-10 14:02

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?

1条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-10 14:20

    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");
            }
        }
    }
    

    0 讨论(0)
提交回复
热议问题