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; 
        }
    }
}
相关推荐
Java面试题总结15 小时前
基于 Java 的 PDF 文本水印实现方案(iText7 示例)
java·python·pdf
傻啦嘿哟17 小时前
Python操作PDF页面详解:删除指定页的完整方案
开发语言·python·pdf
fie888919 小时前
基于C#的推箱子小游戏实现
开发语言·c#
.房东的猫20 小时前
ERP(金蝶云星空)开发【业务数据中心创建和注册】
c#
bugcome_com20 小时前
C# 进阶核心知识点汇总|多项目开发 + 委托 + Lambda + 事件一次吃透
c#
SunflowerCoder1 天前
基于插件化 + Scriban 模板引擎的高效 HTTP 协议中心设计
http·c#
青云计划1 天前
知光项目用户关系模块
c#·linq
m5655bj1 天前
使用 C# 修改 PDF 页面尺寸
java·pdf·c#
专注VB编程开发20年1 天前
c#模仿内置 Socket.Receive(无需 out/ref,直接写回数据)
开发语言·c#
bugcome_com1 天前
【零基础入门】C# 核心教程:从 HelloWorld 到入门精髓
c#