.net call com 已经是比较麻烦了,尤其是调用office , 我感觉office有些方法简直变态,20几个参数,vb 调用还好些,有些可以不添,c++ , 调用简直是灾难。
这里你还非用反射,有些自找麻烦的味道

解决方案 »

  1.   

    我是把网上的一片文章给转过来 看看 结果没想到  那么复杂有没有解决的办法啊 老大们  公司里急着要 WORD转换PDF
      

  2.   

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using oWord = Microsoft.Office.Interop.Word;
    using System.Threading;
    using System.Diagnostics;
    using Ytcinfo.Utils;
    namespace Ytcinfo.Applications.ArchivesFlow.Utils
    {
        public class ThreadCreatePDF
        {
            private static ThreadCreatePDF _instance = null;        private ThreadCreatePDF(){
                
            }        private string FilePath
            {
                get;
                set;
            }        private Stream Content
            {
                get;
                set;
            }        public static ThreadCreatePDF getInstance()
            {
                if (_instance == null)
                {
                    _instance = new ThreadCreatePDF();
                }
                return _instance;
            }        /// <summary>
            /// 把指定路径的word文件转化为PDF文件
            /// </summary>
            /// <param name="_filePath"></param>
            /// <param name="_content"></param>
            public void saveFileToDisk(string _filePath,Stream _content)
            {
                this.FilePath = dealFileLastName(_filePath);
                this.Content = _content;
                Thread _thread = new Thread(new ThreadStart(saveFileToDisk));
                _thread.Start();
            }        /// <summary>
            /// 去除文件的后缀名称
            /// </summary>
            /// <param name="?"></param>
            /// <returns></returns>
            private string dealFileLastName(string _filePath)
            {
                if(_filePath.Contains("."))
                {
                    _filePath = _filePath.Substring(0, _filePath.LastIndexOf("."));
                }
                return _filePath;
            }        private void saveFileToDisk()
            {
                lock (this)
                {
                    FileInfo fi = new FileInfo(FilePath);
                    if (!fi.Directory.Exists)
                        fi.Directory.Create();
                    using (FileStream fs = new FileStream(FilePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None))
                    {
                        byte[] buffer = new byte[256 * 1024];
                        int bytes;
                        Content.Position = 0;                    while ((bytes = Content.Read(buffer, 0, 256 * 1024)) > 0)
                            fs.Write(buffer, 0, bytes);
                        fs.Close();
                        Content.Close();
                    }
                    WordConvert();
                    delOtherFile();
                }
            }        private void WordConvert()
            {            oWord.ApplicationClass word = new Microsoft.Office.Interop.Word.ApplicationClass();
                Type wordType = word.GetType();            //打开WORD文档
                /*对应脚本中的
                 var word = new ActiveXObject("Word.Application");
                 var doc  = word.Documents.Open(docfile);
                */
                oWord.Documents docs = word.Documents;
                Type docsType = docs.GetType();
                object objDocName = FilePath;
                oWord.Document doc = (oWord.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { objDocName, true, true });            //打印输出到指定文件
                //你可以使用 doc.PrintOut();方法,次方法调用中的参数设置较繁琐,建议使用 Type.InvokeMember 来调用时可以不用将PrintOut的参数设置全,只设置4个主要参数
                Type docType = doc.GetType();
                object printFileName = FilePath + ".ps";
                docType.InvokeMember("PrintOut", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { false, false, oWord.WdPrintOutRange.wdPrintAllDocument, printFileName });
                //new object[]{false,false,oWord.WdPrintOutRange.wdPrintAllDocument,printFileName}
                //对应脚本中的word.PrintOut(false, false, 0, psfile);的参数            //退出WORD
                //对应脚本中的word.Quit();
                doc = null;
                wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
                object o1 = FilePath + ".ps";
                object o2 = FilePath + ".pdf";
                object o3 = "";            //引用将PS转换成PDF的对象
                //try catch之间对应的是脚本中的 PDF.FileToPDF(psfile,pdffile,"");   //你可以使用 pdfConvert.FileToPDF("c:\\test.ps","c:\\test.pdf","");这样的转换方法,本人只是为了保持与WORD相同的调用方式
                try
                {
                    ACRODISTXLib.PdfDistillerClass pdf = new ACRODISTXLib.PdfDistillerClass();
                    pdf.bShowWindow = 0;
                    pdf.bSpoolJobs = 0;
                    pdf.FileToPDF(FilePath + ".ps", FilePath + ".pdf", @"Standard");
                    //Type pdfType = pdfConvert.GetType();
                    //pdfType.InvokeMember("FileToPDF", System.Reflection.BindingFlags.InvokeMethod, null, pdf, new object[] { o1, o2, o3 });
                    pdf = null;
                }
                catch { } //读者自己补写错误处理            //为防止本方法调用多次时发生错误,必须停止acrodist.exe进程
                foreach (Process proc in System.Diagnostics.Process.GetProcesses())
                {
                    int begpos;
                    int endpos;                string sProcName = proc.ToString();
                    begpos = sProcName.IndexOf("(") + 1;
                    endpos = sProcName.IndexOf(")");                sProcName = sProcName.Substring(begpos, endpos - begpos);                if (sProcName.ToLower().CompareTo("acrodist") == 0)
                    {
                        try
                        {
                            proc.Kill(); //停止进程
                        }
                        catch { }  //读者自己补写错误处理
                        break;
                    }
                }
            }        private void delOtherFile()
            {
                File.Delete(FilePath);
                File.Delete(FilePath + ".log");
                File.Delete(FilePath + ".ps");
            }
        }
    }