Word转pdf方法小结

 

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

1.在Winform下实现转换

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

Code
        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, false, true, true, 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组件中添加。

Code
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, true, true });

    
//打印输出到指定文件
     Type docType = doc.GetType();
    
object printFileName = @"c:\tmp\test.ps";
     docType.InvokeMember(
"PrintOut", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { false, false, 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值