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
  • 相关阅读:
    matlab 绘制条状图形
    细思恐极 天价房都被谁买去了?——如何操作?
    matlab中的containers.Map()
    林彪:怎样当好一个师长?
    matlab 怎么建立结构体数组?
    matlab中patch函数的用法
    Ubuntu 安装配置MySQL,并使用VS的Server Explorer UI界面远程管理MySQL
    CLIQUE 聚类算法以及Java实现+多线程
    R 中同步进行的多组比较的包:npmc
    基于D3JS绘制中国地图
  • 原文地址:https://www.cnblogs.com/mr-hero/p/3978630.html
Copyright © 2011-2022 走看看