服务器端打开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             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

 

转载于:https://www.cnblogs.com/mr-hero/p/3978630.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值