国庆放假前,公司有个项目里要用到office、pdf以及图片的互转,自己以前没有接触过,所以整理了网上林林总总的办法,也算是总结出了最简单有效的办法:office -> pdf 应用Adobe Acrobat 8 Pro的一个PDFMakerAPI.dll程序集;pdf -> png(jpg,gif...)应用Ghostscript。下面详述说明:
一、准备工作:
1.安装Adobe Acrobat 8 Pro,本人安装的是8.1.2版本,在你的安装目录下(例如我自己的:C:\Program Files\Adobe\Acrobat 8.0\PDFMaker\Common\)common目录中找到PDFMakerAPI.dll程序集,拷贝出到项目中放DLL的文件夹(此文件夹为用户保存DLL文件的文件夹,名称以自己项目为准),并在项目里对其添加引用。
2.安装Ghostscript,本人安装的是8.63版本,需要用的的其他DLL:FontBox-0.1.0-dev.dll,IKVM.GNU.Classpath.dll,IKVM.Runtime.dll,PDFBox-0.7.3.dll,其中IKVM.GNU.Classpath.dll,PDFBox-0.7.3.dll要在项目里对其添加引用,其他两个(4个dll均放到)放到DLL文件夹里即可。
3.为Ghostscript配置Web.config:
<appSettings>
<add key="GhostScriptView" value="C:/Program Files/gs/gs8.63/bin"/>
<add key="GhostScriptArguments" value="-dSAFER -dBATCH -dNOPAUSE -r150 -sDEVICE=jpeg -dGraphicsAlphaBits=4"/>
</appSettings>
找到自己对应的Ghostscript安装目录,自行修改。
二、应用:
1.office -> pdf
引用命名空间:using PDFMAKERAPILib;关键代码如下:
1 2 3 4 5 6 7 8 | /// ///参数:docfile,源office文件绝对路径及文件名(C:\office\myDoc.doc);printpath,pdf文件保存路径(D:\myPdf);printFileName,保 ///存pdf文件的文件名(myNewPdf.pdf) /// objectmissing = System.Type.Missing; PDFMakerAppapp = new PDFMakerApp(); app.CreatePDF(docfile, printpath + printFileName, PDFMakerSettings.kConvertAllPages, false , true , true , missing); |
2.pdf-> 图片
引用命名空间:using org.pdfbox.pdmodel;关键代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | /// /// <param name="pdfFile">PDF文档物理路径</param> /// <param name="imgPath">转换成的图片文件的存放物理路径</param> /// public static void PdfToImages( string pdfFile, string imgPath) { PDDocument doc = PDDocument.load(pdfFile); int pageCount = doc.getDocumentCatalog().getAllPages().size(); //计算pdf文档的总页数 string pdfFileName = Path.GetFileName(pdfFile); int index = pdfFileName.LastIndexOf( '.' ); if (index != -1) pdfFileName = pdfFileName.Substring(0, index); string imgFile = Path.Combine(imgPath, pdfFileName); //转换成的图片文件 if (pageCount == 0) return ; if (pageCount == 1) { imgFile += ".png" ; if (File.Exists(imgFile)) { File.Delete(imgFile); } } else { for ( int i = 0; i < pageCount; i++) { string _imgFile = imgFile + (i + 1).ToString() + ".png" ; if (File.Exists(_imgFile)) { File.Delete(_imgFile); } } imgFile += "%d.png" ; } ProcessStartInfo info = new ProcessStartInfo(); info.CreateNoWindow = true ; info.WindowStyle = ProcessWindowStyle.Hidden; info.WorkingDirectory = System.Configuration.ConfigurationManager.AppSettings[ "GhostScriptView" ]; info.Arguments = System.Configuration.ConfigurationManager.AppSettings[ "GhostScriptArguments" ] + @" -sOutputFile=" + imgFile + " " + pdfFile; info.FileName = @"gswin32c.exe" ; Process subProcess = new Process(); subProcess.StartInfo = info; subProcess.Start(); subProcess.WaitForExit( int .MaxValue); } |
完成上述几步即可简便完美的完成office到彩色图片的转换,大家不妨也试试。
3.总结和说明:
Acrobat 8 Pro需要激活和注册;
Acrobat 8 Pro生成pdf的时候会有自己的虚拟打印机弹出框,当office文件为ppt或者xls的时候会打开文件然后再关闭,doc则不会;
office转换的pdf存放路径不要带有中文;
最后:如有程序安装包和DLL需求消息我就可以了,祝大家工作愉快。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库