[转]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. java 中的fanal

    三.java中有final final 修饰符 关键字可用于修饰类,变量和方法,final关键字有点类似于C#里的sealed 关键字,用于表示它修饰的类,方法和变量不可改变. fina修饰变量时,表 ...

  2. uinavigation样式

    UINavigationController* pNav = [[UINavigationController alloc] initWithRootViewController:pView]; [p ...

  3. js设计模式-建造者模式

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

  4. Android手机与计算机间的”信任关系”

    在iDevices(如iPhone.iPad等等)的取证方面,那个代表”信任关系”的plist无疑是最为关键的迹证,只要有了它,哪怕是最新机型的iDevice及最新版的iOS,且有着指纹保护或pass ...

  5. thinkphp验证码点击更换js实现

    <img src="__CONTROLLER__/verify" alt="" onclick=this.src="__CONTROLLER__ ...

  6. IOS学习感想

    1.一开始学习的时候将会感到非常的难,即使自己曾经学过C/JAVA/HTML/CSS/JS/PHP等,但是对于学过C++的人来说,我就不知道了.因为它的语法不同于任何一门语言,所以说入门难是正常的.但 ...

  7. 5.21_启程日本二面_1 vs 1

    昨天上午刚群面完,晚上7点左右就接到了电话.面试官就两位菇凉,看来她们也是很辛苦.今天下午3点 1 vs 1,在一家咖啡店里,主要是询问下去日本的意愿是否足够强烈.太老实,这里实话实说,也没有表现出非 ...

  8. Vue.js学习 Item12 – 内部响应式原理探究

    深入响应式原理 大部分的基础内容我们已经讲到了,现在讲点底层内容.Vue.js 最显著的一个功能是响应系统 —— 模型只是普通对象,修改它则更新视图.这让状态管理非常简单且直观,不过理解它的原理也很重 ...

  9. SQL Server编程(04)基本语法

    一.定义变量 --简单赋值 declare @a int set @a=5 print @a   --使用select语句赋值 declare @user1 nvarchar(50) select @ ...

  10. jQuery 遍历each()的使用方法

    .each()是一个for循环的包装迭代器.each()通过回调的方式处理,并且会有2个固定的实参,索引与元素(从0开始计数).each()回调方法中的this指向当前迭代的dom元素 遍历方法: & ...