PDF转图片的另外一种方法

背景

俺在之前的博客中提到了 把PDF的每一页另存为图片的方法。当时是使用的Devexpress。因为Devexpress确实很方便,因为 Devexpress 自带了PdfViewer的控件。直接使用**PdfViewer的CreateBitmap(i_page_no, bmp_w)**就可以。最近遇到了一个很特别的pdf,这个pdf使用Devexpress 会只显示位置,pdf里的背景图无法显示,转出的图片中也丢失了背景图。所以俺就使用了另外的方法。

另一种方法

通过Nuget 安装 Ghostscript.NET

然后使用 GhostscriptRasterizer 获得每一页的图片

int pageCount = 0;

using (var rasterizer = new GhostscriptRasterizer ())

{

var gsVersion = new GhostscriptVersionInfo(ghostscriptDllPath);

rasterizer.Open(pdfPath, gsVersion, true);

pageCount = rasterizer.PageCount;

for (int pageNumber = 1; pageNumber <= pageCount; pageNumber++)

{

SkiaSharp.SKBitmap image = rasterizer.GetPage(dpi, pageNumber);

注意

需要的dll

调用时可以放在执行目录的gs子目录下

private void button_proc_Click(object sender, EventArgs e)

{

string fn = textBox1.Text.Trim();

if (File.Exists(fn))

{

string path = Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);

string DllPath = Path.Combine(path, "gs", "gsdll64.dll");

string dir = fn + ".pages";

button_proc.Enabled = false;

progressBar1.Visible = true;

Application.DoEvents();

GsPdf2Img gs = new GsPdf2Img();

gs.convertProgressEvent += proc_convertProgressEvent;

gs.imgFormat = "jpg";

gs.ghostscriptDllPath = DllPath;

gs.ConvertPdfToImages(fn, dir, 96);

progressBar1.Visible = false;

button_proc.Enabled = true;

MessageBox.Show("save to "+ dir);

}

}

代码

cs 复制代码
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks; 
using Ghostscript.NET;
using Ghostscript.NET.Rasterizer; 

namespace PDF2IMG
{
   public  class GsPdf2Img
    {
        public  string ghostscriptDllPath = @"C:\Program Files\gs\gs10.06.0\bin\gsdll64.dll";
        public string imgFormat = "jpg";
        public event EventHandler<ConvertPdfToImagesEventArgs> convertProgressEvent;
        public  void ConvertPdfToImages(string pdfPath, string outputDir, int dpi = 300 )
        {
            if (!File.Exists(pdfPath))
                return;

            if (!Directory.Exists(outputDir))
                Directory.CreateDirectory(outputDir);

            if (!File.Exists(ghostscriptDllPath))
                throw new FileNotFoundException("Ghostscript DLL not exists", ghostscriptDllPath);

            int pageCount = 0; 
            using (var rasterizer = new GhostscriptRasterizer())
            { 
                var gsVersion = new GhostscriptVersionInfo(ghostscriptDllPath);
                rasterizer.Open(pdfPath, gsVersion, true);
                pageCount = rasterizer.PageCount; 
                for (int pageNumber = 1; pageNumber <= pageCount; pageNumber++)
                { 
                    SkiaSharp.SKBitmap image = rasterizer.GetPage(dpi, pageNumber);


                    if ((string.Compare(imgFormat, "jpg", true) == 0) || (string.Compare(imgFormat, "jpg", true) == 0))
                    { 
                        string outputPath = Path.Combine(outputDir, pageNumber.ToString()+ ".jpg");
                        using (var stream = File.OpenWrite(outputPath))
                        {
                            image.Encode(SkiaSharp.SKEncodedImageFormat.Jpeg, 100).SaveTo(stream);
                        }
                    }
                    else if ((string.Compare(imgFormat, "png", true) == 0)  )
                    {
                        string outputPath = Path.Combine(outputDir, pageNumber.ToString() + ".png");
                        using (var stream = File.OpenWrite(outputPath))
                        {
                            image.Encode(SkiaSharp.SKEncodedImageFormat.Png, 100).SaveTo(stream);
                        }
                    }
                    else if ((string.Compare(imgFormat, "bmp", true) == 0))
                    {
                        string outputPath = Path.Combine(outputDir, pageNumber.ToString() + ".bmp");
                        using (var stream = File.OpenWrite(outputPath))
                        {
                            image.Encode(SkiaSharp.SKEncodedImageFormat.Bmp, 100).SaveTo(stream);
                        }
                    }
                    image.Dispose();   
                    convertProgressEvent?.Invoke(this, new ConvertPdfToImagesEventArgs(pageNumber, pageCount)); 
                }

                rasterizer.Close();
            }
             
        }

    }

    public class ConvertPdfToImagesEventArgs : EventArgs
    {
        public int Page { get; }
        public int TotalCount { get; }


        public ConvertPdfToImagesEventArgs(int page, int totalCount)
        {
            Page = page;
            TotalCount = totalCount; 
        }
    }
}
相关推荐
其实秋天的枫19 小时前
【2026年最新】驾考科目一考试题库2309道电子版pdf
经验分享·pdf
周杰伦fans20 小时前
C# required 关键字详解
开发语言·网络·c#
墨染天姬21 小时前
【AI】如何基于cursor创建MCP索引pdf
人工智能·pdf
qq_429499571 天前
分享免费的PDF 翻译 原格式
pdf
来自外太空的鱼-张小张1 天前
jeecg预览pdf、jeecg无法预览pdf、jeecg自带预览pdf
pdf·状态模式
游乐码1 天前
c#ArrayList
开发语言·c#
唐青枫1 天前
C#.NET Monitor 与 Mutex 深入解析:进程内同步、跨进程互斥与使用边界
c#·.net
周杰伦fans1 天前
cad文件选项卡不见了怎么办?
c#
llm大模型算法工程师weng1 天前
Python敏感词检测方案详解
开发语言·python·c#
游乐码1 天前
c#stack
开发语言·c#