http://www.2cto.com/kf/201110/107592.html
国庆放假前,公司有个项目里要用到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:
找到自己对应的Ghostscript安装目录,自行修改。
二、应用:
1.office -> pdf
引用命名空间:using PDFMAKERAPILib;关键代码如下:
1 ///
2 ///参数:docfile,源office文件绝对路径及文件名(C:\office\myDoc.doc);printpath,pdf文件保存路径(D:\myPdf);printFileName,保
3 ///存pdf文件的文件名(myNewPdf.pdf)
4 ///
5
6 objectmissing = System.Type.Missing;
7 PDFMakerAppapp = new PDFMakerApp();
8 app.CreatePDF(docfile, printpath + printFileName, PDFMakerSettings.kConvertAllPages, false, true, true, missing);
2.pdf-> 图片
引用命名空间:using org.pdfbox.pdmodel;关键代码如下:
01 ///
02 /// PDF文档物理路径
03 /// 转换成的图片文件的存放物理路径
04 ///
05 public static void PdfToImages(string pdfFile, string imgPath)
06 {
07 PDDocument doc = PDDocument.load(pdfFile);
08 int pageCount = doc.getDocumentCatalog().getAllPages().size();//计算pdf文档的总页数
09
10 string pdfFileName = Path.GetFileName(pdfFile);
11 int index = pdfFileName.LastIndexOf('.');
12 if (index != -1)
13 pdfFileName = pdfFileName.Substring(0, index);
14
15 string imgFile = Path.Combine(imgPath, pdfFileName);//转换成的图片文件
16
17 if (pageCount == 0) return;
18 if (pageCount == 1)
19 {
20 imgFile += ".png";
21 if (File.Exists(imgFile))
22 {
23 File.Delete(imgFile);
24 }
25 }
26 else
27 {
28 for (int i = 0; i < pageCount; i++)
29 {
30 string _imgFile = imgFile + (i + 1).ToString() + ".png";
31 if (File.Exists(_imgFile))
32 {
33 File.Delete(_imgFile);
34 }
35 }
36 imgFile += "%d.png";
37 }
38
39 ProcessStartInfo info = new ProcessStartInfo();
40 info.CreateNoWindow = true;
41 info.WindowStyle = ProcessWindowStyle.Hidden;
42 info.WorkingDirectory = System.Configuration.ConfigurationManager.AppSettings["GhostScriptView"];
43 info.Arguments = System.Configuration.ConfigurationManager.AppSettings["GhostScriptArguments"] + @" -sOutputFile=" + imgFile + " " + pdfFile;
44 info.FileName = @"gswin32c.exe";
45 Process subProcess = new Process();
46 subProcess.StartInfo = info;
47 subProcess.Start();
48 subProcess.WaitForExit(int.MaxValue);
49 }
完成上述几步即可简便完美的完成office到彩色图片的转换,大家不妨也试试。
3.总结和说明:
Acrobat 8 Pro需要激活和注册;
Acrobat 8 Pro生成pdf的时候会有自己的虚拟打印机弹出框,当office文件为ppt或者xls的时候会打开文件然后再关闭,doc则不会;
office转换的pdf存放路径不要带有中文;
最后:如有程序安装包和DLL需求消息我就可以了,祝大家工作愉快
摘自:小宝LOVE继艾斯
http://blog.csdn.net/musical_insect/article/details/5765098
在网上找遍了有关 C# 如何将PDF文档转换成图片的资料,最终在CSDN上面得到了一位高手的指点,根据他的指点,我做了这个项目,希望这个项目能对和我之前有同样需求的人有所帮助!
1、项目中用到了GhostScript,有关GhostScript的详细说明,请参考:http://downloads.sourceforge.net/ghostscript/gs861w32.exe?modtime=1196280996&big_mirror=1
2、在运行本项目前,你必须确保在本地计算机上已安装GhostScript,至于GhostScript的安装目录你可随意选择,但在项目中的配置文件中的
3、根目录中的 gs871w32.exe 为 GhostScript 安装文件,可直点击安装到本地计算机。
4、根目录中还有 IKVM.GNU.Classpath.dll、PDFBox-0.7.3.dll、IKVM.Runtime.dll、FontBox-0.1.0-dev.dll 这四个程序集文件,它们是PDFBox(有关PDFBox的详细说明请参考:http://sourceforge.net/projects/pdfbox/)中的程序集文件,主要用于辅助操作pdf文档,因为使用PDFBox将PDF文档转为图片的.net版本目前还有些BUG,所以在项目中用了GhostScript来实现转换。请将 IKVM.GNU.Classpath.dll、PDFBox-0.7.3.dll引入项目中,再把 IKVM.Runtime.dll、FontBox-0.1.0-dev.dll置于运行目录下,具体请看项目及其代码。
项目很简单,下面贴出了主要的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;
using System.Diagnostics;
using org.pdfbox.util;
using org.pdfbox.pdmodel;
#region 转换PDF文档
///
/// 将PDF文档转换成文本
///
/// PDF文档物理路径
///
public string PdfToText(string pdfFile)
{
PDDocument doc = PDDocument.load(pdfFile);
PDFTextStripper stripper = new PDFTextStripper();
return stripper.getText(doc);
}
///
/// 将PDF文档转换成图片
///
/// PDF文档物理路径
/// 转换成的图片文件的存放物理路径
/// 转换成图片文件以后是否删除原PDF文档
///
public IList
{
IList
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 null;
if (pageCount == 1)
{
imgFile += ".jpg";
imgList.Add(imgFile);
if (File.Exists(imgFile)) File.Delete(imgFile);
}
else
{
for (int i = 0; i < pageCount; i++)
{
string _imgFile = imgFile + (i + 1).ToString() + ".jpg";
imgList.Add(_imgFile);
if (File.Exists(_imgFile)) File.Delete(_imgFile);
}
imgFile += "%d.jpg";
}
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);
if (isDeletePDF)
{
File.Delete(pdfFile);
}
return imgList;
}
#endregion
代码中会用到的配置信息:
简单吧,下面请看看本项目运行的效果图:
注:完整项目解决方案我已上传为资源,欢迎大家前去下载使用!