How to convert PDF files to images

前端 未结 12 494
庸人自扰
庸人自扰 2020-11-27 14:46

I need to convert PDF files to images. If the PDF file is multi-page,I just need one image that contains all of the PDF pages.

Is there an open sou

相关标签:
12条回答
  • 2020-11-27 15:03

    As for 2018 there is still not a simple answer to the question of how to convert a PDF document to an image in C#; many libraries use Ghostscript licensed under AGPL and in most cases an expensive commercial license is required for production use.

    A good alternative might be using the popular 'pdftoppm' utility which has a GPL license; it can be used from C# as command line tool executed with System.Diagnostics.Process. Popular tools are well known in the Linux world, but a windows build is also available.

    If you don't want to integrate pdftoppm by yourself, you can use my PdfRenderer popular wrapper (supports both classic .NET Framework and .NET Core) - it is not free, but pricing is very affordable.

    0 讨论(0)
  • 2020-11-27 15:03

    Regarding PDFiumSharp: After elaboration I was able to create PNG files from a PDF solution.

    This is my code:

    using PDFiumSharp;
    using System.Collections.Generic;
    using System.Drawing;
    using System.IO;
    
    public class Program
    {
        static public void Main(String[] args)
        {
            var renderfoo = new Renderfoo()
            renderfoo.RenderPDFAsImages(@"C:\Temp\example.pdf", @"C:\temp");
        }
    }
    
    
    
    public class Renderfoo
    {
    
        public void RenderPDFAsImages(string Inputfile, string OutputFolder)
        {
            string fileName = Path.GetFileNameWithoutExtension(Inputfile);
    
            using (PDFiumSharp.PdfDocument doc = new PDFiumSharp.PdfDocument(Inputfile))
            {
                for (int i = 0; i < doc.Pages.Count; i++)
                {
                    var page = doc.Pages[i];
                    using (var bitmap = new System.Drawing.Bitmap((int)page.Width, (int)page.Height))
                    {
                        var grahpics = Graphics.FromImage(bitmap);
                        grahpics.Clear(Color.White);
                        page.Render(bitmap);
                        var targetFile = Path.Combine(OutputFolder, fileName + "_" + i + ".png");
                        bitmap.Save(targetFile);
                    }
                }
            }
        }
    
    }
    

    For starters, you need to take the following steps to get the PDFium wrapper up and running:

    • Run the Custom Code tool for both tt files via right click in Visual Studio
    • Compile the GDIPlus Project
    • Copy the compiled assemblies (from the GDIPlus project) to your project
    • Reference both PDFiumSharp and PDFiumsharp.GdiPlus assemblies in your project

    • Make sure that pdfium_x64.dll and/or pdfium_x86.dll are both found in your project output directory.

    0 讨论(0)
  • 2020-11-27 15:04

    Using Android default libraries like AppCompat, you can convert all the PDF pages into images. This way is very fast and optimized. The below code is for getting separate images of a PDF page. It is very fast and quick.

    ParcelFileDescriptor fileDescriptor = ParcelFileDescriptor.open(new File("pdfFilePath.pdf"), MODE_READ_ONLY);
        PdfRenderer renderer = new PdfRenderer(fileDescriptor);
        final int pageCount = renderer.getPageCount();
        for (int i = 0; i < pageCount; i++) {
            PdfRenderer.Page page = renderer.openPage(i);
            Bitmap bitmap = Bitmap.createBitmap(page.getWidth(), page.getHeight(),Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmap);
            canvas.drawColor(Color.WHITE);
            canvas.drawBitmap(bitmap, 0, 0, null);
            page.render(bitmap, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);
            page.close();
    
            if (bitmap == null)
                return null;
    
            if (bitmapIsBlankOrWhite(bitmap))
                return null;
    
            String root = Environment.getExternalStorageDirectory().toString();
            File file = new File(root + filename + ".png");
    
            if (file.exists()) file.delete();
            try {
                FileOutputStream out = new FileOutputStream(file);
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
                Log.v("Saved Image - ", file.getAbsolutePath());
                out.flush();
                out.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    

    =======================================================

    private static boolean bitmapIsBlankOrWhite(Bitmap bitmap) {
        if (bitmap == null)
            return true;
    
        int w = bitmap.getWidth();
        int h = bitmap.getHeight();
        for (int i =  0; i < w; i++) {
            for (int j = 0; j < h; j++) {
                int pixel =  bitmap.getPixel(i, j);
                if (pixel != Color.WHITE) {
                    return false;
                }
            }
        }
        return true;
    }
    
    0 讨论(0)
  • 2020-11-27 15:06

    (Disclaimer I worked on this component at Software Siglo XXI)

    You could use Super Pdf2Image Converter to generate a TIFF multi-page file with all the rendered pages from the PDF in high resolution. It's available for both 32 and 64 bit and is very cheap and effective. I'd recommend you to try it.

    Just one line of code...

    GetImage(outputFileName, firstPage, lastPage, resolution, imageFormat)
    
    Converts specifies pages to image and save them to outputFileName (tiff allows multi-page or creates several files)
    

    You can take a look here: http://softwaresigloxxi.com/SuperPdf2ImageConverter.html

    0 讨论(0)
  • 2020-11-27 15:09

    The NuGet package Pdf2Png is available for free and is only protected by the MIT License, which is very open.

    I've tested around a bit and this is the code to get it to convert a PDF file to an image (tt does save the image in the debug folder).

    using cs_pdf_to_image;
    using PdfToImage;
    
    private void BtnConvert_Click(object sender, EventArgs e)
    {
        if(openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            try
            {
                string PdfFile = openFileDialog1.FileName;
                string PngFile = "Convert.png";
                List<string> Conversion = cs_pdf_to_image.Pdf2Image.Convert(PdfFile, PngFile);
                Bitmap Output = new Bitmap(PngFile);
                PbConversion.Image = Output;
            }
            catch(Exception E)
            {
                MessageBox.Show(E.Message);
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-27 15:12

    I used PDFiumSharp and ImageSharp in a .NET Standard 2.1 class library.

    /// <summary>
    /// Saves a thumbnail (jpg) to the same folder as the PDF file, using dimensions 300x423,
    /// which corresponds to the aspect ratio of 'A' paper sizes like A4 (ratio h/w=sqrt(2))
    /// </summary>
    /// <param name="pdfPath">Source path of the pdf file.</param>
    /// <param name="thumbnailPath">Target path of the thumbnail file.</param>
    /// <param name="width"></param>
    /// <param name="height"></param>
    public static void SaveThumbnail(string pdfPath, string thumbnailPath = "", int width = 300, int height = 423)
    {
        using var pdfDocument = new PdfDocument(pdfPath);
        var firstPage = pdfDocument.Pages[0];
    
        using var pageBitmap = new PDFiumBitmap(width, height, true);
    
        firstPage.Render(pageBitmap);
    
        var imageJpgPath = string.IsNullOrWhiteSpace(thumbnailPath)
            ? Path.ChangeExtension(pdfPath, "jpg")
            : thumbnailPath;
        var image = Image.Load(pageBitmap.AsBmpStream());
    
        // Set the background to white, otherwise it's black. https://github.com/SixLabors/ImageSharp/issues/355#issuecomment-333133991
        image.Mutate(x => x.BackgroundColor(Rgba32.White));
    
        image.Save(imageJpgPath, new JpegEncoder());
    }
    
    0 讨论(0)
提交回复
热议问题