首页 > 代码库 > 服务器端打开office然后采用虚拟打印 转换成pdf

服务器端打开office然后采用虚拟打印 转换成pdf

服务器端打开office然后采用虚拟打印 转换成pdf

 1 [WebMethod] 2         public bool ConvertWordTOPDF(string WordPath) 3         { 4             bool ret=false; 5             #region 6             //string dataName = collection[i].FileName;//本地文件名称(带路径) 7             //string ip=Request.UserHostAddress; 8             //string wordname=WordPath; 9             string wordPath=WordPath;10             string pdfPath=wordPath.Split(.)[0]+".pdf";11             oWord._Document m_Document = null;12             oWord._Application m_wordApplication = null;13             object oMissing = Type.Missing;  14             oWord.ApplicationClass word = new Microsoft.Office.Interop.Word.ApplicationClass();15             try16             {17                 object obj = System.Reflection.BindingFlags.InvokeMethod;18                 Type wordType = word.GetType();19                 oWord.Documents docs = word.Documents;20                 Type docsType = docs.GetType();21                 object objDocName = wordPath;22                 oWord.Document doc = (oWord.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { objDocName, true, true });23                 //打印输出到指定文件24                 Type docType = doc.GetType();25                 object printFileName =wordPath.Split(.)[0]+".ps";26                 docType.InvokeMember("PrintOut", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { false, false, oWord.WdPrintOutRange.wdPrintAllDocument, printFileName });27                 object savechanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdSaveChanges;28                 object saveasPath =wordPath.Split(.)[0]+"new.doc";29                 //必须另存为!30                 doc.SaveAs(ref saveasPath, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);31                 //必须关闭32                 doc.Close(ref savechanges, ref oMissing, ref oMissing);33                 wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null,word, null);34                 //删除另存为文件35                 try36                 {37                     System.IO.File.Delete(saveasPath.ToString());38                 }39                 catch40                 {41                 }42                 string o1 = printFileName.ToString();//与生产的PS文件同步43                 string o2 = pdfPath;44                 string o3 = "";45                 //引用将PS转换成PDF的对象46                 try47                 {48                     ACRODISTXLib.PdfDistillerClass pdf = new ACRODISTXLib.PdfDistillerClass();49                     Type pdfType = pdf.GetType();50                     pdfType.InvokeMember("FileToPDF", System.Reflection.BindingFlags.InvokeMethod, null, pdf, new object[] { o1, o2, o3 });51                     pdf = null;52                     //System.IO.File.Delete( "123.log");//清除转换日志文件53                 }54                 catch 55                 { //throw new Exception("PS转PDF处出错!");56                 }57                 System.IO.File.Delete( printFileName.ToString());//清除ps文件58                 59                 System.IO.File.Delete( printFileName.ToString().Split(.)[0]+".log");//清除转换日志文件60 61                 if(System.IO.File.Exists(pdfPath))62                 {63                     ret=true;64                 }65                 //为防止本方法调用多次时发生错误,必须停止acrodist.exe进程66                 foreach (Process proc in System.Diagnostics.Process.GetProcesses())67                 {68                     int begpos;69                     int endpos;70 71                     string sProcName = proc.ToString();72                     begpos = sProcName.IndexOf("(") + 1;73                     endpos = sProcName.IndexOf(")");74 75                     sProcName = sProcName.Substring(begpos, endpos - begpos);76 77                     if (sProcName.ToLower().CompareTo("acrodist") == 0)78                     {79                         try80                         {81                             proc.Kill();82                         }83                         catch { }84                         break;85                     }86                 }87             }88             catch(Exception ex)89             {90 91             }92 93             return ret;94             #endregion95         }
View Code

 

服务器端打开office然后采用虚拟打印 转换成pdf