这周碰到一个需求,由于公司系统框架的原因,不能直接显示第三方回传回来的pdf(说明一下,第三方回传的pdf是带上了签章信息(即在pdf中加入了签名图片)),需要把pdf转成图片进行显示,但在做的过程中踩了不少雷。最后使用第三方插件PDFRender4NET

1   第三方的插件PdfiumViewer(缺点,丢失签章信息)

首先试了第三方的插件PdfiumViewer,代码很简单,网上也有很多demo,把代码拷贝过来修改一下,三两下就搞定了,试了一下,确实是可以实现pdf传图片,但当我把业务代码写完了,在业务系统上运行时,发现,妈的,大意了,转换完毕的图片丢失了签章信息。下面是我略作修改后的部分代码:

public class PdfToImageHelper{/// <summary>/// pdf转图片(base64格式的字符串)/// </summary>/// <param name="pdfBase64String">pdf对应的base64字符串</param>/// <returns>Pdf如果有多页,就返回多张图片(base64字符串集合)</returns>public static List<string> GetBase64StringArray(string pdfBase64String){if (pdfBase64String==null|| pdfBase64String.Length==0) return null;List<string> base64StringList = new List<string>();byte[] buffer=Convert.FromBase64String(pdfBase64String);if (buffer == null || buffer.Length == 0) return base64StringList;MemoryStream ms = new MemoryStream(buffer);var pdfDocument = PdfiumViewer.PdfDocument.Load(ms);for (int index = 0; index <pdfDocument.PageCount; index++){Image image = pdfDocument.Render(index, (int)pdfDocument.PageSizes[index].Width, (int)pdfDocument.PageSizes[index].Height, 300, 300, false);string base64Str=ImageToBase64String(image);if (base64Str != null && base64Str.Length > 0){base64StringList.Add(base64Str);}}//释放流资源return base64StringList;}/// <summary>/// Image对象转base64字符串/// </summary>/// <param name="Picture"></param>/// <returns></returns>private static string ImageToBase64String(Image Picture){MemoryStream ms = new MemoryStream();if (Picture == null)return null;Picture.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);byte[] BPicture = new byte[ms.Length];BPicture = ms.GetBuffer();//释放流资源return Convert.ToBase64String(BPicture);}}

调用:List<string>  imageBase64StringList=PdfToImageHelper.GetBase64StringArray("pdf对应的base64字符串");

2  第三方插件Spire.pdf (缺点:收费,有免费版的,但是pdf转换为图片有页数限制(最多3页) ,且转换后的图片很模糊)

使用PdfiumViewer不行后,开始使用Spire.pdf,通过vistual studio的nuget就可以拿到dll,如下图:

第一个Spire.PDF是收费的,转换后的图片左上角会带上如下图的水印信息

第二个FreeSpire.PDF是免费的,但是pdf如果超过3页,只能转前3页,后面的转换的都是空白页

代码就不贴了,网上有很多demo

3   第三方插件PDFRender4NET(O2S.Components.PDFRender4NET.dll,版本信息如下图)

下面贴出我略做修改后的代码:

using O2S.Components.PDFRender4NET;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;namespace iih.gdrmh.ca.PatientSign.bp
{public class PdfToImageHelper{public static List<string> GetBase64StringArrayByPdfPath(string pdfPath){if (pdfPath == null || pdfPath.Length == 0) return null;List<string> base64StringList = new List<string>();PDFFile pdfFile = PDFFile.Open(pdfPath);for (int index =0; index <pdfFile.PageCount; index++){Bitmap pageImage = pdfFile.GetPageImage(index, 56 * 10);string base64Str = BitmapToBase64String(pageImage);if (base64Str != null && base64Str.Length > 0){base64StringList.Add(base64Str);}}pdfFile.Dispose();return base64StringList;}private static string ImageToBase64String(Image Picture){MemoryStream ms = new MemoryStream();if (Picture == null)return null;Picture.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);byte[] BPicture = new byte[ms.Length];BPicture = ms.GetBuffer();return Convert.ToBase64String(BPicture);}private static string BitmapToBase64String(Bitmap bitmap){// 1.先将BitMap转成内存流MemoryStream ms = new MemoryStream();bitmap.Save(ms, ImageFormat.Png);ms.Seek(0, SeekOrigin.Begin);// 2.再将内存流转成byte[]并返回byte[] bytes = new byte[ms.Length];ms.Read(bytes, 0, bytes.Length);ms.Flush();ms.Close();ms.Dispose();return Convert.ToBase64String(bytes);}}
}

调用:List<string>  imageBase64StringList=PdfToImageHelper.GetBase64StringArrayByPdfPath("pdf对应的文件路径");

最后发现,转换后的图片,签章信息还在,转换后的图片清晰度比FreeSpire.PDF还高

拓展:

去stack overflow搜索发现,pdf转换图片的方案有很多,但推荐最多的是Ghostscript.NET. github地址为:https://github.com/jhabjan/Ghostscript.NET   demo代码:https://github.com/jhabjan/Ghostscript.NET/blob/master/Ghostscript.NET.Samples/Samples/RasterizerSample1.cs

stack overflow参考链接:

1  Convert Pdf to Image C# .NET - Stack Overflow

2  Converting pdf to image using c# and Ghostscript - Stack Overflow

3  asp.net - Convert PDF file to images using C# - Stack Overflow

利用C#实现Pdf转图片相关推荐

  1. Python 利用pymupdf将pdf转换为图片并拆分,后通过PIL合并生成一张图片

    文章主要内容主要参考几篇文章并合并在一起的,文章链接依次如下,第二和第三的文章链接是从第一篇文章找到的: (1).https://blog.csdn.net/qq_25115281/article/d ...

  2. JAVA利用pdfbox将pdf转图片

    1.引入依赖 <dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</ar ...

  3. 利用 Python 去除 PDF 水印(和图片水印原理一样)

    前言 今天介绍下用 Python 去除 PDF (图片)的水印.思路很简单,代码也很简洁. 首先来考虑 Python 如何去除图片的水印,然后再将思路复用到 PDF 上面. 原理 这张图片是前几天整理 ...

  4. C# 利用 Spire.PDF 实现.pdf转图片

    最近恰好遇到了一个需求,需要将存储发票信息的pdf文件,转换成为图片文件并提供下载.因为pdf的内容是固定的,只有一页,最终在经过了一番百度之后,最终选择使用了Spire.PDF. 引用的dll文件可 ...

  5. 利用jpedal进行pdf转换成jpeg,jpg,png,tiff,tif等格式的图片

    项目中运用到pdf文件转换成image图片,开始时使用pdfbox开源库进行图片转换,但是转换出来的文件中含有部分乱码的情况.下面是pdfBox 的pdf转换图片的代码示例. try{ String ...

  6. .net pdf转图片_PDF转图片怎么做?PDF一键转图片!

    在日常工作中,我们经常需要把文件资料传给其他人看.但如果文档是PDF格式的话,很可能他人的设备因缺少相应的阅读工具而无法打开.这时,最好的方法就是将PDF文件转换成图片!这样不管是在电脑还是在一些移动 ...

  7. .net pdf转图片_PDF转图片要怎么转?两分钟解决!

    PDF转图片要怎么转?在我们的实际工作当中,很多时候会碰到这样的问题:如何把我的PDF文档转换成图片进行分享呢? 有些小伙伴会用截图的方式把PDF文件存为图片,但是这样图片的质量不高,在手机里很难看清 ...

  8. 7个值得拥有收藏的免费在线 PDF转图片转换器

    PDF 格式在文档演示和交换中无处不在.但是PDF转图片的必要性也在于很多情况.这里我想提几点. 收件人可能没有打开您发送的 PDF 的应用程序,但可以使用系统提供的程序查看图像. 图像在浏览器中的显 ...

  9. java pdf 图片_java实现PDF转图片的方法

    本文实例为大家分享了java实现PDF转图片的具体代码,供大家参考,具体内容如下 1.首先利用maven引入所需jar包 org.apache.pdfbox fontbox 2.0.1 org.apa ...

  10. java pdfbox 转jpg_java实现PDF转图片的方法

    本文实例为大家分享了java实现PDF转图片的具体代码,供大家参考,具体内容如下 1.首先利用maven引入所需jar包 org.apache.pdfbox fontbox 2.0.1 org.apa ...

最新文章

  1. 加速你的Hibernate引擎(上)
  2. linux网络编程(二)TCP通讯状态
  3. 美国著名核物理学家,前半生为美国造核弹,后半生为中国放牛
  4. 研究生做毕设,用到深度学习,没有GPU该怎么办
  5. vs能运行python吗_vs怎么运行python(vs能运行python吗)
  6. JDK、TOMCAT 配置环境变量
  7. sqlilte 判断当天日期_Java8 必会的日期处理的实践,必须收藏!
  8. 一文读懂-Impala
  9. 文章根据时间段显示的微信名和微信号
  10. FISCO BCOS Solidity 智能合约Compiler error:Stack too deep, try removing local variables 如何传递超过16个参数变量
  11. [Oracle]ORA-600[kdBlkCheckError]LOB坏块处理
  12. 自动格式化SQL工具推荐
  13. docker安装nacos并配置mysql数据库教程
  14. android手机电量测试,Android手机app耗电量测试工具 - Gsam Battery Monitor
  15. 云计算安全需求分析与网络
  16. html另存word或excel,excel另存为word
  17. js pug 代码_pug模板引擎(原jade)
  18. #从零开始学HCIA起步
  19. opencv函数测试对话框
  20. IOS app蓝牙连接硬件设备 自动断开问题

热门文章

  1. [转]Ceph:OpenStack标配Linux PB级分布式文件系统详解
  2. 瞻博网络Contrail多云解决方案荣获最佳创新解决方案奖
  3. 微信开发者工具公众号网页调试跨域问题的解决
  4. K线技术指标实现详解—MACD
  5. android模拟器 分辨率,Android模拟器各个皮肤的分辨率
  6. MB/s MiB/s之间换算
  7. 百度搜索url参数详解
  8. 三阶魔方还原教程最详细
  9. excel小技巧1:修改的日期格式为什么要双击一下单元格才能变
  10. 01.第一章 初等概率论