zoukankan      html  css  js  c++  java
  • 使用Office API 将word文档转换为Pdf

    Office API提供了Word到Pdf的转换功能,准确的说,应该是Office API提供了打开Word文档并且保存为PDF的功能,调用如下方法即可:

      doc.SaveAs(ref outputFileName,
                        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);
    

      其中,doc是Microsoft.Office.Interop.Word.Document的实例,第二个参数是一个枚举Microsoft.Office.Interop.Word.WdSaveFormat。该枚举具体内容如下:

    namespace Microsoft.Office.Interop.Word
    {
        public enum WdSaveFormat
        {
            wdFormatDocument97 = 0,
            wdFormatDocument = 0,
            wdFormatTemplate97 = 1,
            wdFormatTemplate = 1,
            wdFormatText = 2,
            wdFormatTextLineBreaks = 3,
            wdFormatDOSText = 4,
            wdFormatDOSTextLineBreaks = 5,
            wdFormatRTF = 6,
            wdFormatEncodedText = 7,
            wdFormatUnicodeText = 7,
            wdFormatHTML = 8,
            wdFormatWebArchive = 9,
            wdFormatFilteredHTML = 10,
            wdFormatXML = 11,
            wdFormatXMLDocument = 12,
            wdFormatXMLDocumentMacroEnabled = 13,
            wdFormatXMLTemplate = 14,
            wdFormatXMLTemplateMacroEnabled = 15,
            wdFormatDocumentDefault = 16,
            wdFormatPDF = 17,
            wdFormatXPS = 18,
            wdFormatFlatXML = 19,
            wdFormatFlatXMLMacroEnabled = 20,
            wdFormatFlatXMLTemplate = 21,
            wdFormatFlatXMLTemplateMacroEnabled = 22,
        }
    }
    

      可以看到有一项叫wdFormatPDF,所以理论上调用该API就可以进行Word到Pdf的转换了,但是不幸的是我用此方法后提示调用ComXXX错误。听同事说Word 2007 自带导出Pdf的功能,这更让我相信使用这个Office API应该是没问题的,于是我将电脑上的office 2003 换成了office 2007,可是发现word并没有传说中的导出Pdf的功能,经过同事的帮忙,发现在微软官网上提供一个导出Pdf功能插件,抱着试试看的心态,我下载了这个插件,安装完毕后再次调用Offcie API,惊奇地发现可以了。希望这篇随笔可以帮助遇到相同问题的朋友们。插件的下载地址:http://www.microsoft.com/downloads/en/details.aspx?FamilyId=4D951911-3E7E-4AE6-B059-A2E79ED87041&displaylang=en

      不过我不知道这个API是否也适用于word 2003 或其他版本,不知道其他版本的word是否有这个插件,可能在更高级版本的word里已经无需安装插件而自带这个功能了吧。

  • 相关阅读:
    春招已近,这份GitHub万星的ML算法面试大全请收下
    [资源推荐] 必须收藏的两个查找论文和代码实现的网站!
    windows下安装pycocotools,亲测有效!
    GAN原理
    2018-07-02
    虚函数和纯虚函数
    友元
    string字符串
    实参和形参
    C/C++学习笔记汇总
  • 原文地址:https://www.cnblogs.com/zzy0471/p/1916624.html
Copyright © 2011-2022 走看看