C#使用pdfmaker直接转换word到PDF
看了Xmadog的博文http://blog.csdn.net/Xmadog/article/details/568890使用PDFMaker直接将word转换为PDF,受到启发,去找了pdfmaker,发现使用方法有点出入(可能本人菜鸟,没有理解原博主的意图),所以在此阐述一下自己的方法。。。
首先下载pdfmaker,这是个老外写的免费插件,解压后直接安装就OK了,可以在VS中直接引用。
http://download.cnet.com/PDF-Maker-DLL/3000-2206_4-10286388.html
在VS2005中引用com组件 Interop.PDFmaker.dll
然后介绍以下使用的函数
public virtual void CreatePDFfromWord(ref string strPDFFile, ref string strWordDocument, ref string strPassword, ref string strWritePassword, ref string strPages, ref bool blnLandscape, ref bool blnPageNumberInFooter, ref bool blnPageNumberInHeader, ref AlignType lngPageNumberAlignment, ref bool blnFirstPage, ref string strDistiller, ref bool blnShowWindow, ref bool blnSpoolJobs, ref string strJobOptionsFile, ref bool blnApplySecurity);
//2比1少了生成log文件,更快,但是会弹出pdf保存窗口,不能用于服务器
public virtual void CreatePDFfromWord2(ref string strPDFFile, ref string strWordDocument, ref string strPassword, ref string strWritePassword, ref string strPages, ref bool blnLandscape, ref bool blnPageNumberInFooter, ref bool blnPageNumberInHeader, ref AlignType lngPageNumberAlignment, ref bool blnFirstPage, ref string strDistiller, ref bool blnCloseAcrobat, ref string strSaveWindowCaption);
//比1多了一个打印机设置,但是觉得根本没调用
public virtual void CreatePDFfromWord3(ref string strPDFFile, ref string strWordDocument, ref string strPrinter, ref string strPassword, ref string strWritePassword, ref string strPages, ref bool blnLandscape, ref bool blnPageNumberInFooter, ref bool blnPageNumberInHeader, ref AlignType lngPageNumberAlignment, ref bool blnFirstPage, ref bool blnShowWindow, ref bool blnSpoolJobs, ref string strJobOptionsFile, ref bool blnApplySecurity);
*strPDFFile:生成的pdf路径
*strWordDocument:源word文档路径
*strPassword:word文档密码
*strWritePassword:word文档写密码
*strPages:打印的页数范围,例"1, 3, 6-8, 10"。默认值为全部
*blnLandscape:Page orientation.页次方向
*blnPageNumberInFooter:页脚是否包含页码
*blnPageNumberInHeader:页眉是否包含页码
*lngPageNumberAlignment:页码位置,值: 0=left, 1=center, 2=right
*blnFirstPage:首页是否包含页码
*strDistiller:Adobe Acrobat Distiller名称
*blnShowWindow:显示程序处理窗口
*blnSpoolJobs:是否脱机执行工作
*strJobOptionsFile:打印工作选项文件
*blnApplySecurity:应用Distiller安全设置
*blnCloseAcrobat:生成PDF后是否关闭Acrobat
*strSaveWindowCaption:保存窗口默认标题,即 "save pdf file as"
*strPrinter:打印机名称
具体实例如下:
1 using pdf2 = PDFmaker; 2 3 protected void ToPdfFile(string SaveURL, string Filename) 4 { 5 try 6 { 7 pdf2.CreatePDFClass c = new PDFmaker.CreatePDFClass(); 8 string disPDF=SaveURL + Filename + ".pdf"; 9 string srcDOC=SaveURL + Filename + ".doc"; 10 string LOG = SaveURL + Filename + ".log"; 11 string missing = ""; 12 bool f = false; 13 bool t = true; 14 PDFmaker.AlignType at = PDFmaker.AlignType.pdoAlignCenter; 15 16 //CreatePDFfromWord 17 //c.CreatePDFfromWord(ref disPDF, ref srcDOC, ref missing, ref missing, ref missing, ref f, ref f, ref f, ref at, ref f, ref missing, ref f, ref f, ref missing, ref f); 18 19 //CreatePDFfromWord2会显示存储选择界面 20 //c.CreatePDFfromWord2(ref Filename, ref srcDOC, ref missing, ref missing, ref missing, ref f, ref f, ref f, ref at, ref f, ref missing, ref t, ref SaveURL); 21 22 //CreatePDFfromWord3 23 string prt=”Adobe PDF”; //之前安装的虚拟打印机,但是没有被启动 24 c.CreatePDFfromWord3(ref disPDF, ref srcDOC, ref missing, ref missing, ref missing, ref missing, ref f, ref f, ref f, ref at, ref f, ref f, ref f, ref missing, ref f); 25 //删除doc和log 26 System.IO.File.Delete(srcDOC); 27 System.IO.File.Delete(LOG); 28 } 29 catch (Exception er) 30 { 31 MessageBox.Show(er.ToString()); 32 } 33 }
PS. 还是需要安装Adobe Acrobat7.0