How to convert PDF files to images

前端 未结 12 529
庸人自扰
庸人自扰 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

    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.

提交回复
热议问题