使用MSOffice .NET API 将文档(Word \ Excel \ PowerPoint \ Visio \ text \ XML \ RTF \ CSV等)转换为PDF

源码下载地址:http://download.csdn.net/detail/kingmax54212008/9920401

将Word / Excel / PowerPoint / Visio文件转换为PDF的简单步骤



样品图像

内容

介绍

将Word / Excel / PowerPoint / Visio文件转换为PDF的简单步骤.NET提供了一个简单的MSOffice API来导出PDF。

系统要求

  1. 框架3.5
  2. MS Office 2010
  3. Visual Studio 2010

解决方案概述

添加Microsoft Office库(版本:14.0.0.0),步骤如下:

  1. 右键点击引用并选择'添加参考..'

    样品图像

  2. 在.NET选项卡选择库中列出如下:
    1. 的Microsoft.Office.Interop.Excel
    2. Microsoft.Office.Interop。幻灯片
    3. Microsoft.Office.Interop.Visio
    4. 的Microsoft.Office.Interop.Word
    5. 办公室

样品图像

I. Word \ TXT \ XML到PDF转换的代码

方法Word2Pdf有四个步骤将Word \ TXT \ XML转换为PDF。

  1. 以隐藏模式启动Word应用程序
    //
    // Start MS word application
    //
    Microsoft.Office.Interop.Word.Application msWordDoc = null;
    Microsoft.Office.Interop.Word.Document doc = null;
    
    // C# doesn't have optional arguments so we'll need a dummy value 
    object oMissing = System.Reflection.Missing.Value;
    
    msWordDoc = new Microsoft.Office.Interop.Word.Application
    {
        Visible = false,
        ScreenUpdating = false
    };
  2. 打开文档需要转换。
    //
    //Open Document
    //
    
    doc = msWordDoc.Documents.Open(ref originalDocPath, ref oMissing
          , ref oMissing, ref oMissing, ref oMissing, ref oMissing
          , ref oMissing,ref oMissing, ref oMissing, ref oMissing
          , ref oMissing, ref oMissing,ref oMissing, ref oMissing
          , ref oMissing, ref oMissing);
    ...
  3. 导出文件为PDF。
    //
    // save Document as PDF
    //
    if (doc != null)
    {
        doc.Activate();
        // save Document as PDF
        object fileFormat = WdSaveFormat.wdFormatPDF;
        doc.SaveAs(ref pdfPath,
        ref fileFormat, ref oMissing, ref oMissing,
        ref oMissing, ref oMissing, ref oMissing, ref oMissing,
        ref oMissing, ref oMissing, ref oMissing, ref oMissing,
        ref oMissing, ref oMissing, ref oMissing, ref oMissing);
    }
    else
    {
        Console.WriteLine("Error occured while converting office Word to PDF");
    }
    ...
  4. 释放占用对象。
    //
    //  Quit Word and release the ApplicationClass object
    //
     finally
    {
    // Close and release the Document object.
    if (doc != null)
    {
        object saveChanges = WdSaveOptions.wdDoNotSaveChanges;
        doc.Close(ref saveChanges, ref oMissing, ref oMissing);
        Util.releaseObject(doc);
    }
    // Quit Word and release the ApplicationClass object.
    ((_Application)msWordDoc).Quit(ref oMissing, ref oMissing, ref oMissing);
    Util.releaseObject(msWordDoc);
    msWordDoc = null;
    }
    ...

II。Excel转换PDF代码

方法Excel2Pdf有4个步骤将Excel转换为PDF

  1. 以隐藏模式启动Excel应用程序
    //
    // Create COM Objects
    //  
      Microsoft.Office.Interop.Excel.Application excelApplication = null;
      Microsoft.Office.Interop.Excel.Workbook excelWorkbook = null;
      object unknownType = Type.Missing;
    // Create new instance of Excel
    //open excel application
      excelApplication = new Microsoft.Office.Interop.Excel.Application
      {
        ScreenUpdating = false,
        DisplayAlerts = false
      };
    ...
  2. 打开需要转换的Excel。
    //
    //Open Excel \ CSV
    //
    if (excelApplication != null)
        excelWorkbook = excelApplication.Workbooks.Open(originalXlsPath, unknownType, unknownType,
        unknownType, unknownType, unknownType,
        unknownType, unknownType, unknownType,
        unknownType, unknownType, unknownType,
        unknownType, unknownType, unknownType);
    ...
  3. 将Excel导出为PDF。
    //
    // save Excel as PDF
    //
     
    // Call Excel's native export function (valid in Office 2007 and Office 2010, AFAIK)
    
    excelWorkbook.ExportAsFixedFormat(Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF,
        pdfPath,unknownType, unknownType, unknownType, unknownType, unknownType,
        unknownType, unknownType);
    ...
  4. 释放被占用对象。
    //
    //  Quit Excel and release the ApplicationClass object
    //
    
    finally
    {
    // Close the workbook, quit the Excel, and clean up regardless of the results...
        if (excelWorkbook != null)
        excelWorkbook.Close(unknownType, unknownType, unknownType);
        if (excelApplication != null) excelApplication.Quit();
    
        Util.releaseObject(excelWorkbook);
        Util.releaseObject(excelApplication);
    }
    ...

III。PowerPoint到PDF转换的代码

方法Powerpoint 2Pdf有4个步骤将PowerPoint转换为PDF。

  1. 以隐藏模式启动PowerPoint应用程序
    //
    // Create COM Objects
    //  
      PowerPoint.Application pptApplication = null;
      PowerPoint.Presentation pptPresentation = null;
    
        object unknownType = Type.Missing;
    
    //start power point 
        pptApplication = new PowerPoint.Application();
    ...
  2. 打开需要转换的PowerPoint
    //
    //open powerpoint document
    //
    pptPresentation = pptApplication.Presentations.Open((string)originalPptPath,
        Microsoft.Office.Core.MsoTriState.msoTrue,Microsoft.Office.Core.MsoTriState.msoTrue,
        Microsoft.Office.Core.MsoTriState.msoFalse);
    
    ...
  3. PowerPoint导出为PDF。
    //
    // save PowerPoint as PDF
    //    
    pptPresentation.ExportAsFixedFormat((string)pdfPath,
        PowerPoint.PpFixedFormatType.ppFixedFormatTypePDF,
        PowerPoint.PpFixedFormatIntent.ppFixedFormatIntentPrint,
        MsoTriState.msoFalse,PowerPoint.PpPrintHandoutOrder.ppPrintHandoutVerticalFirst,
        PowerPoint.PpPrintOutputType.ppPrintOutputSlides,MsoTriState.msoFalse, null,
        PowerPoint.PpPrintRangeType.ppPrintAll, string.Empty,true, true, true,
        true, false, unknownType);
    ...
  4. 释放被占用对象。
    //
    //  Quit PowerPoint and release the ApplicationClass object
    //
    
    finally
    {
        // Close and release the Document object.
        if (pptPresentation != null)
        {
          pptPresentation.Close();
          Util.releaseObject(pptPresentation);
          pptPresentation = null;
        }
        // Quit Word and release the ApplicationClass object.
        pptApplication.Quit();
        Util.releaseObject(pptApplication);
        pptApplication = null;
    }
    ...

IV。Visio转PDF代码

方法Visio2Pdf有4个步骤将Visio转换为PDF

  1. 以隐藏模式启动Visio应用程序
    //
    // Create COM Objects
    //  
        Microsoft.Office.Interop.Visio.ApplicationClass msVisioDoc = null;
        Visio.Document vsdDoc = null;
    //Start application
        msVisioDoc = new Visio.ApplicationClass { Visible = false };
    
    ...
  2. 打开需要转换的Visio。
    //
    //Open Visio Document
    //
        vsdDoc = msVisioDoc.Documents.Open(originalVsdPath);
    ...
  3. 导出Visio为PDF。
    //
    // Export Visio as PDF
    //
    vsdDoc.ExportAsFixedFormat(Visio.VisFixedFormatTypes.visFixedFormatPDF, pdfPath,
        Visio.VisDocExIntent.visDocExIntentScreen,Visio.VisPrintOutRange.visPrintAll,
        1, vsdDoc.Pages.Count, false, true, true, true, true,System.Reflection.Missing.Value);
    ...
  4. 释放占用对象。
    //
    //  Quit Visio and release the ApplicationClass object
    //
    
    finally
    {
    // Close and release the Document object.
        if (vsdDoc != null)
        {
        vsdDoc.Close();
        Util.releaseObject(vsdDoc);
        }
    
    // Quit Word and release the ApplicationClass object.
        msVisioDoc.Quit();
        Util.releaseObject(msVisioDoc);
    }
    ...

释放对象

方法releaseObject具有发布Office COM对象的步骤

//
// Create COM Objects
//  
try
{
    System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
    obj = null;
}
catch (Exception exReleaseObject)
{
    obj = null;
    Console.WriteLine("Release of COM Object Fail:"+ exReleaseObject);
}
finally
{
    GC.Collect();
    GC.WaitForPendingFinalizers();
    GC.Collect();
    GC.WaitForPendingFinalizers();
}
...

如何运行演示程序

  1. 提取“592957 / Converter_demo.zip”文件,并运行“Converter.exe”。
  2. 新窗口打开,点击“浏览”按钮。

  3. 选择要转换的文件类型。

  4. 选择文件,然后按“打开”。

  5. 单击“转换”按钮,将输入文件转换为PDF,并显示“成功邮件”PDF文件的输出文件。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值