[转]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. List<string>里 每个元素重复了多少次

    List<string>里 每个元素重复了多少次 static void Main(string[] args) { List<string> list = new List& ...

  2. Android各个文件夹对应的分辨率?

  3. swing 复选框

    通过   box1 和 box2的  public boolean isSelected()的方法 返回按钮的状态. 如果选定了切换按钮,则返回 true,否则返回 false.

  4. vyatta的fork开源版本

    https://www.reddit.com/r/networking/comments/3dvwfy/who_here_is_using_vyos/ Vyatta came in two flavo ...

  5. Dede后台验证码不显示解决方法详解(dedecms 5.7)

    今天朋友问我他本地与服务器上安装了dedecms5.7无法显示验证码,一般这种情况很少见,一般情况就是服务器设置问题,还有临时目录的权限问题 Dede后台验证码不显示或不正常分三种情况,下面来逐一分析 ...

  6. WP_3种磁贴效果设置

    private void ApplicationBarIconButton_Click_1(object sender, EventArgs e) { var tileData = new FlipT ...

  7. poj2503 哈希

    这题目主要是难在字符串处理这块. #include <stdio.h> #include <string.h> #include <stdlib.h> #defin ...

  8. 关于原生js的一些研究

    搬砖,原文地址:http://segmentfault.com/a/1190000002911253 callee和caller function inner(){ console.log(argum ...

  9. js控制div颜色

    <html><head></head><style>#div1{width:400px;height:400px;background-color:re ...

  10. ubuntu下使用nethogs监控网络流量

    NetHogs是一款小巧免费的开源命令行工具,用来按进程或程序实时统计网络带宽使用率. 对于使用类似于“repo tool”.“depot_tools”等工具checkout源码时非常有用,可以查看当 ...