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; 
        }
    }
}
相关推荐
daanpdf20 小时前
历年考研数学一、数学二、数学三真题试卷及答案PDF
考研·pdf
南风微微吹21 小时前
2026年初级社会工作者考试历年真题及答案解析PDF电子版(2010-2025年)
pdf
爱炸薯条的小朋友1 天前
C#由窗体原子表溢出造成的软件闪退,根本原因补充
开发语言·c#·wpf
俊哥工具1 天前
不用安装不收费!多功能U盘修复工具,解决大部分U盘故障
学习·pdf·word·excel·音视频
我是苏苏1 天前
C#基础:Winform桌面开发中自定义组件UI、属性及事件
服务器·数据库·c#
2401_853087881 天前
Confluence 替代落地复盘:存量数据迁移、权限重构、信创适配踩坑总结
开发语言·重构·c#
曹牧1 天前
C#:DataGridView控件中展示JSON内容
开发语言·c#·json
He少年1 天前
【AI路径代理与业务接入 — 成功失败感悟】
人工智能·c#
爱炸薯条的小朋友1 天前
C#的详细应用和讲解池化为什么能提升 OpenCvSharp / Mat 的整体效率
开发语言·opencv·c#