码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-09-18 11:19:03      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   ar   for   文件   

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

bubuko.com,布布扣
 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             try
16             {
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                 try
36                 {
37                     System.IO.File.Delete(saveasPath.ToString());
38                 }
39                 catch
40                 {
41                 }
42                 string o1 = printFileName.ToString();//与生产的PS文件同步
43                 string o2 = pdfPath;
44                 string o3 = "";
45                 //引用将PS转换成PDF的对象
46                 try
47                 {
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                         try
80                         {
81                             proc.Kill();
82                         }
83                         catch { }
84                         break;
85                     }
86                 }
87             }
88             catch(Exception ex)
89             {
90 
91             }
92 
93             return ret;
94             #endregion
95         }
View Code

 

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

标签:style   blog   http   color   io   os   ar   for   文件   

原文地址:http://www.cnblogs.com/mr-hero/p/3978630.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!