zoukankan      html  css  js  c++  java
  • 服务器端打开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
  • 相关阅读:
    多个数字和数字字符串混合运算规则
    关于js对象引用的小例子
    实现函数 isInteger(x) 来判断 x 是否是整数
    写一个少于 80 字符的函数,判断一个字符串是不是回文字符串
    关于数组排序
    事件委托(事件代理)的原理以及优缺点是什么?
    将url的查询参数解析成字典对象
    js dom操作获取节点的一些方法
    js中arguments的应用
    深度克隆---js对象引用
  • 原文地址:https://www.cnblogs.com/mr-hero/p/3978630.html
Copyright © 2011-2022 走看看