在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();
//必须添加引用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);
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 { }
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;
foreach (Process proc in System.Diagnostics.Process.GetProcesses())
{
int begpos;
int endpos;
string sProcName = proc.ToString();
begpos = sProcName.IndexOf("(") + 1;
endpos = sProcName.IndexOf(")");
begpos = sProcName.IndexOf("(") + 1;
endpos = sProcName.IndexOf(")");
sProcName = sProcName.Substring(begpos, endpos - begpos);
if (sProcName.ToLower().CompareTo("acrodist") == 0)
{
try
{
proc.Kill();
}
catch { }
break;
}
}
}
{
try
{
proc.Kill();
}
catch { }
break;
}
}
}
//webConfig
<identity impersonate="true" userName="Administrator" password="xuan444150934"/>