现在的位置: 首页 > 综合 > 正文

Word转pdf方法小结

2013年02月28日 ⁄ 综合 ⁄ 共 2920字 ⁄ 字号 评论关闭

前提条件:安装Acrobat Distillr,可以通过安装Acrobat Professional 版本实现。

1.在Winform下实现转换

很简单,需要引用COM组件 PDFMake


        public static string ConvertPdf(string p_sFileSrc,string sTgtFile)
        {
            
object missing = System.Type.Missing;
            
try
            {
                PDFMAKERAPILib.PDFMakerApp app 
= new PDFMAKERAPILib.PDFMakerApp();
  
                
if (File.Exists(sSrcFile))
                {
                    
//获取PDF生成路径
                    int bSuccess = app.CreatePDF(sSrcFile, sTgtFile, PDFMAKERAPILib.PDFMakerSettings.kConvertAllPages, falsetruetrue, missing);
                    
return Enum.GetName(typeof(PDFMAKERAPILib.PDFMakerRetVals), bSuccess);
                }
                
else
                {
                    
return "源文件不存在!";
                }
            }
            
catch (Exception er)
            {
                
return "文件转换失败!";
            }
        }
    }

 

2.在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组件中添加。


private void WordConvert()
{
    oWord.ApplicationClass word 
= new Microsoft.Office.Interop.Word.ApplicationClass();
    Type wordType 
= word.GetType();

    oWord.Documents docs = word.Documents;
    Type docsType 
= docs.GetType();
    
object objDocName = @"c:/tmp/test.doc";
    oWord.Document doc 
= (oWord.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { objDocName, truetrue });

    //打印输出到指定文件
    Type docType = doc.GetType();
    
object printFileName = @"c:/tmp/test.ps";
    docType.InvokeMember(
"PrintOut", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { falsefalse, oWord.WdPrintOutRange.wdPrintAllDocument, printFileName });
    wordType.InvokeMember(
"Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);

    string o1 = "c://tmp//test.ps";
    
string o2 = "c://tmp//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;
        }
    }
}

抱歉!评论已关闭.