.PDF虚拟打印机将Word转PDF

在WebForm下转换复杂点,因为上面的代码在IIS时,就是转换不成功,花了点时间,发现原因是两个的用户不同,一个是管理员,一个是Asp.net 帐户或者是network Service 看IIS是5.0 还是6.0。
从网上搜索到下面这个办法,也是比较简单的。
前提条件:
(1)安装虚拟打印机:进入WINDOWS的控制面板,进入打印机,点击"添加打印机"图标,在安装对话框上"按一步",出现选择打印机时,在制造商一栏中选择"Generic",在打印机一栏中,选择"MS Publisher Color Printer",然后一路按下一步,知道安装结束。以下代码并没有提供当前的打印机,而是使用系统默认的,即设置的"MS Publisher Color Printer"。
(2)必须添加引用Acrobat Distiller与WORD,可以从COM组件中添加。
using Microsoft.Office.Interop.Word;
using ACRODISTXLib;
//方法三
        //必须添加引用Acrobat Distiller与WORD,可以从COM组件中添加。
        private void WordConvert()
        {
            Microsoft.Office.Interop.Word.ApplicationClass word = new Microsoft.Office.Interop.Word.ApplicationClass();
            Type wordType = word.GetType();
            //打开WORD文档
            Microsoft.Office.Interop.Word.Documents docs = word.Documents;
            Type docsType = docs.GetType();
 
            object objDocName = @"C:\Temp\Test.docx";
            Microsoft.Office.Interop.Word.Document doc = (Microsoft.Office.Interop.Word.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { objDocName, true, true });
            //打印输出到指定文件
            Type docType = doc.GetType();
            object printFileName = @"c:\Temp\test.ps";
            docType.InvokeMember("PrintOut", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { false, false, Microsoft.Office.Interop.Word.WdPrintOutRange.wdPrintAllDocument, printFileName });
            wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
            string o1 = "c:\\Temp\\test.ps";
            string o2 = "c:\\Temp\\test.pdf";
            string o3 = "";
 
            //引用将PS转换成PDF的对象
            try
            {
                ACRODISTXLib.PdfDistillerClass pdf = new ACRODISTXLib.PdfDistillerClass();
                pdf.FileToPDF(o1, o2, o3);
            }
            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;
                }
            }
        }
 
 
//webConfig
    <identity impersonate="true" userName="Administrator" password="xuan444150934"/>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值