[转]doc,ppt,xls文件格式转PDF格式

http://blog.csdn.net/lee353086/article/details/7920355

确实好用。

需要注意的是#import文件路径要和自己电脑上的文件路径对应

/*
功能:
 Office文件格式(docx、xlsx、pptx)转PDF格式文件
Author:
 Kagula by 2012-08-29
使用前提
 [1]Office 2007(Word,Excel,PPT)
 [2]Office 2007导PDF插件
编译环境:
 [1]VS2008SP1
 [2]WinXPSP3
*/

#pragma   warning(disable:4786) 

#import  "C:\Program Files\Common Files\Microsoft Shared\Office12\mso.dll" \
rename("RGB","_OfficeRGB")         

#import  "C:\Program Files\Common Files\Microsoft Shared\VBA\VBA6\VBE6EXT.OLB" \
    rename("Reference", "ignorethis")

#import   "C:\Program Files\Microsoft Office\Office12\msword.olb " \
    rename("FindText","_FindText")\
    rename("Rectangle","_Rectangle")\
    rename("ExitWindows","_ExitWindows")

#import   "C:\Program Files\Microsoft Office\Office12\MSPPT.OLB"

#import "c:\Program Files\Microsoft Office\Office12\EXCEL.exe" \
    rename("DialogBox","_DialogBox") \
    rename("RGB","_RGB") \
    exclude("IFont","IPicture")

#include <string>
#include <iostream>

int EXCEL2PDF(std::wstring inputFileName,std::wstring outputFileName)
{
    HRESULT hr;
    ;

    Excel::_ApplicationPtr pApplication = NULL;
    Excel::_WorkbookPtr pThisWorkbook = NULL ;
    BSTR szBstrInputFileName;
    BSTR szBstrOutputFileName;

     szBstrInputFileName=SysAllocString(inputFileName.c_str());
     szBstrOutputFileName=SysAllocString(outputFileName.c_str());

    if (FAILED(pApplication.CreateInstance(__uuidof(Excel::Application))))
    {
        wprintf(L"CreateInstance failed w/err 0x%08lx\n", hr);
        ;
    }
    try
    {
        pThisWorkbook = pApplication->GetWorkbooks()->Open(szBstrInputFileName);

        pThisWorkbook->ExportAsFixedFormat(Excel::XlFixedFormatType::xlTypePDF,szBstrOutputFileName);

        pThisWorkbook->Close();
        pThisWorkbook.Release();
        pThisWorkbook = NULL;
    }catch(...)
    {
        nR = ;
    }

    pApplication-> Quit();
    pApplication.Release();
    pApplication= NULL;

    return nR;
}

int PPT2PDF(std::wstring inputFileName,std::wstring outputFileName)
{
     PowerPoint::_ApplicationPtr spPpApp;
     BSTR szBstrInputFileName;
     BSTR szBstrOutputFileName;
     BSTR szBstrEmpty;
     HRESULT hr;
     PowerPoint::PresentationsPtr  spPres;
     PowerPoint::_PresentationPtr  pPre;
     ;

     szBstrInputFileName=SysAllocString(inputFileName.c_str());
     szBstrOutputFileName=SysAllocString(outputFileName.c_str());
     szBstrEmpty=SysAllocString(L"");

     if (FAILED(spPpApp.CreateInstance(__uuidof(PowerPoint::Application))))
     {
         wprintf(L"CreateInstance failed w/err 0x%08lx\n", hr);
         ;
     } 

     spPres = spPpApp->Presentations;
     if(spPres==NULL)
     {
         nR = ;
         goto _RELEASE_APP;
     }
     try{
         pPre = spPres->Open(szBstrInputFileName,
             Office::MsoTriState::msoTrue,Office::MsoTriState::msoFalse,Office::MsoTriState::msoFalse);
         if(pPre ==NULL)
         {
             nR = ;
             goto _RELEASE_APP;
         }

         pPre->ExportAsFixedFormat(szBstrOutputFileName,PowerPoint::PpFixedFormatType::ppFixedFormatTypePDF,
             PowerPoint::PpFixedFormatIntent::ppFixedFormatIntentPrint,Office::MsoTriState::msoTriStateMixed,
             PowerPoint::PpPrintHandoutOrder::ppPrintHandoutHorizontalFirst,PowerPoint::PpPrintOutputType::ppPrintOutputSlides,
             Office::MsoTriState::msoFalse,NULL,PowerPoint::PpPrintRangeType::ppPrintAll,szBstrEmpty,
             VARIANT_TRUE,VARIANT_FALSE,VARIANT_TRUE,VARIANT_TRUE,VARIANT_FALSE);

         pPre->Close();
         pPre.Release();
         pPre = NULL;
     }catch(...)
     {
         nR==;
     }
_RELEASE_APP:
    spPpApp-> Quit();
    spPpApp.Release();
    spPpApp = NULL;

    return nR;
}

int Word2PDF(std::wstring inputFileName,std::wstring outputFileName)
{
    ;
    Word::_ApplicationPtr   pWordApp   =   NULL;
    Word::_DocumentPtr   pDoc   =   NULL;
    HRESULT hr;
    BSTR szBstrOutputFileName;

    szBstrOutputFileName=SysAllocString(outputFileName.c_str());
    hr = pWordApp.CreateInstance(__uuidof(Word::Application));
    if(hr!=S_OK)
    {
        ;
    }

    Word::DocumentsPtr   pDocs   =   NULL;
    pWordApp-> get_Documents(&pDocs);
    if(pDocs==NULL)
    {
        nR = ;
        goto _RELEASE_APP;
    }

    try
    {
        pDoc = pDocs->Open(&(_variant_t(inputFileName.c_str())));
        if(pDoc==NULL)
            goto _RELEASE_APP;

        pDoc->ExportAsFixedFormat(szBstrOutputFileName,Word::WdExportFormat::wdExportFormatPDF,VARIANT_FALSE,
            Word::WdExportOptimizeFor::wdExportOptimizeForPrint,Word::WdExportRange::wdExportAllDocument,,,
            Word::WdExportItem::wdExportDocumentContent,VARIANT_TRUE,VARIANT_TRUE,
            Word::WdExportCreateBookmarks::wdExportCreateNoBookmarks,VARIANT_TRUE,VARIANT_TRUE,VARIANT_FALSE);

        pDoc-> Close();
        pDoc.Release();
        pDoc   =   NULL;
    }catch(...)
    {
        nR = ;
    }

_RELEASE_APP:
    pWordApp-> Quit();
    pWordApp.Release();
    pWordApp   =   NULL; 

    return nR;
}

int _tmain(int argc, _TCHAR* argv[])
{
    ;

    CoInitialize(NULL); 

    std::wstring wsCmd;
    std::wstring wsS;
    std::wstring wsD;

    )
    {
        std::cout<<"Command Usage: Office2PDF -[e|p|w] <source file name> <destination file name>"<<std::endl;
        std::cout<<"         e.g.: Office2PDF -e myName.xlsx myName.pdf"<<std::endl;
        ;
    }

    wsCmd = argv[];
    wsS   = argv[];
    wsD   = argv[];

    if(wsCmd==L"-e")
        nR = EXCEL2PDF(wsS.c_str(),wsD.c_str());
    else if(wsCmd==L"-p")
        nR = PPT2PDF(wsS.c_str(),wsD.c_str());
    else if(wsCmd==L"-w")
        nR = Word2PDF(wsS.c_str(),wsD.c_str());

    CoUninitialize(); 

    )
        std::cout<<"Error:"<<nR<<std::endl;

    return nR;
}

c/c++常用代码---doc,ppt,xls文件格式转PDF格式[转]的更多相关文章

  1. 文档资源搜索小工具 - 支持PDF,DOC,PPT,XLS

    最近做了一个文档搜索小工具,当然不是网盘搜索工具,这个工具支持四种文件格式搜索(pdf,doc,ppt,xls),你只需要在搜索框中输入你想要搜索资源的关键词,点击搜索按钮即可获取相关资源,点击下载按 ...

  2. NSIS常用代码整理

    原文 NSIS常用代码整理 这是一些常用的NSIS代码,少轻狂特意整理出来,方便大家随时查看使用.不定期更新哦~~~ 1 ;获取操作系统盘符 2 ReadEnvStr $R0 SYSTEMDRIVE ...

  3. XSS(跨站脚本攻击) - 常用代码大全

    XSS(跨站脚本攻击) - 常用代码大全-版本一 1'"()&%<acx><ScRiPt >prompt(915149)</ScRiPt> < ...

  4. GCD 常用代码

    GCD 常用代码 体验代码 异步执行任务 - (void)gcdDemo1 { // 1. 全局队列 dispatch_queue_t q = dispatch_get_global_queue(0, ...

  5. 转--Android实用的代码片段 常用代码总结

    这篇文章主要介绍了Android实用的代码片段 常用代码总结,需要的朋友可以参考下     1:查看是否有存储卡插入 复制代码 代码如下: String status=Environment.getE ...

  6. 刀哥多线程之03GCD 常用代码

    GCD 常用代码 体验代码 异步执行任务 - (void)gcdDemo1 { // 1. 全局队列 dispatch_queue_t q = dispatch_get_global_queue(0, ...

  7. jquery常用代码集锦

    1. 如何修改jquery默认编码(例如默认GB2312改成 UTF-8 ) 1 2 3 4 5 $.ajaxSetup({     ajaxSettings : {         contentT ...

  8. IOS 使用webview 显示 doc/docx/xls/pdf等

    在一款项目里添加阅读各种文档功能 那么对在线的文档或者是下载后的文档 进行阅读,比如 doc/docx/xls/pdf等文件 有两种方法总结如下: 1. - (void)viewDidLoad { [ ...

  9. Mysql:常用代码

    C/S: Client Server B/S: Brower Server Php主要实现B/S .net IIS Jave TomCat LAMP:L Mysql:常用代码 Create table ...

随机推荐

  1. sql server查询出的结果中添加一列序列行

    select row_number()over(order by id) as rownum ,[UserName] ,[Password] ,[Mobile] ,[Email] ,[Sex] ,[C ...

  2. Leetcode006 ZigZag Conversion

    /* simple simulation algorithm * we cann`t make sure the size of the string, * so it had better to s ...

  3. jquery 设置元素内容html(),text(),val()

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. 学习STL-介绍一下STL

    从大学时就开始学习C++,到现在近5年的时间了却很少用到STL.现在想想真得是对不起这门语言,也对不起宝贵的五年光阴.我钟爱C++,所以一定要完全搞懂它,理解它.爱一个人的前提是要懂他(她),爱一门语 ...

  5. 修改VS解决方案及工程名,解决如何打开高/版本VS项目

    对于VS2008等低版本与高版本VS之间的转换问题: 对照下面2个版本的不同点自由修改,切换到相应的版本文件(红字修改,灰色删除) ---------------------------------- ...

  6. Linux:系统的密码忘记了,登录不上

    可先通过进入单用户模式,修改下密码再登录记录. 第一步: 重启系统,在进入系统之前不断的按键盘左上角的“Esc”键,会进入如下页面: 然后按e进入编辑页面 第二步: 进入如下页面后,通过键盘的上下方向 ...

  7. hbase 各个概念,region,storefile

    HBase中有两张特殊的Table,-ROOT-和.META. .META.:记录了用户表的Region信息,它可以有多高region(这的意思是说.META.表可以分 裂成多个region,和用户表 ...

  8. Android:控件布局(表格布局)TableLayout

    TableLayout继承LinearLayout 实例:用表格布局实现计算机布局>>>>>>>>>>>> 有多少个TableR ...

  9. DOM 中 Property 和 Attribute 的区别

    原文地址:http://web.jobbole.com/83129/ property 和 attribute非常容易混淆,两个单词的中文翻译也都非常相近(property:属性,attribute: ...

  10. linux内核中的min(x, y)和max(x, y)宏定义

    /linux/include/linux/kernel.h中有min(x, y)和max(x, y)的定义如下: #define min(x, y) ({ \ typeof(x) _min1 = x; ...