• c/c++常用代码---doc,ppt,xls文件格式转PDF格式[转]


    [转]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 FilesCommon FilesMicrosoft SharedOffice12mso.dll" 
    rename("RGB","_OfficeRGB")         
    
    #import  "C:Program FilesCommon FilesMicrosoft SharedVBAVBA6VBE6EXT.OLB" 
        rename("Reference", "ignorethis")
    
    #import   "C:Program FilesMicrosoft OfficeOffice12msword.olb " 
        rename("FindText","_FindText")
        rename("Rectangle","_Rectangle")
        rename("ExitWindows","_ExitWindows")
    
    #import   "C:Program FilesMicrosoft OfficeOffice12MSPPT.OLB"
    
    #import "c:Program FilesMicrosoft OfficeOffice12EXCEL.exe" 
        rename("DialogBox","_DialogBox") 
        rename("RGB","_RGB") 
        exclude("IFont","IPicture")
    
    #include <string>
    #include <iostream>
    
    int EXCEL2PDF(std::wstring inputFileName,std::wstring outputFileName)
    {
        HRESULT hr;
        int nR = 0;
    
    
        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
    ", hr);
            return 1;
        } 
        try
        {
            pThisWorkbook = pApplication->GetWorkbooks()->Open(szBstrInputFileName);
    
            pThisWorkbook->ExportAsFixedFormat(Excel::XlFixedFormatType::xlTypePDF,szBstrOutputFileName);
    
            pThisWorkbook->Close();
            pThisWorkbook.Release();
            pThisWorkbook = NULL;
        }catch(...)
        {
            nR = 2;
        }
    
        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;
         int     nR = 0;
         
         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
    ", hr); 
             return 1; 
         } 
     
         spPres = spPpApp->Presentations; 
         if(spPres==NULL)
         {
             nR = 2;
             goto _RELEASE_APP;
         }
         try{
             pPre = spPres->Open(szBstrInputFileName,
                 Office::MsoTriState::msoTrue,Office::MsoTriState::msoFalse,Office::MsoTriState::msoFalse);
             if(pPre ==NULL)
             {
                 nR = 3;
                 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==4;
         }
    _RELEASE_APP:
        spPpApp-> Quit(); 
        spPpApp.Release(); 
        spPpApp = NULL;
    
        return nR;
    }
    
    int Word2PDF(std::wstring inputFileName,std::wstring outputFileName)  
    {
        int nR = 0;
        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)
        {
            return 1;
        }
    
        Word::DocumentsPtr   pDocs   =   NULL; 
        pWordApp-> get_Documents(&pDocs);
        if(pDocs==NULL)
        {
            nR = 2;
            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,1,1,
                Word::WdExportItem::wdExportDocumentContent,VARIANT_TRUE,VARIANT_TRUE,
                Word::WdExportCreateBookmarks::wdExportCreateNoBookmarks,VARIANT_TRUE,VARIANT_TRUE,VARIANT_FALSE);
    
            pDoc-> Close(); 
            pDoc.Release(); 
            pDoc   =   NULL; 
        }catch(...)
        {
            nR = 3;
        }
    
    _RELEASE_APP:
        pWordApp-> Quit(); 
        pWordApp.Release(); 
        pWordApp   =   NULL; 
    
        return nR;
    }
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        int nR = 0;
    
        CoInitialize(NULL); 
    
        std::wstring wsCmd;
        std::wstring wsS;
        std::wstring wsD;
    
        if(argc!=4)
        {
            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;
            return 1;
        }
    
        wsCmd = argv[1];
        wsS   = argv[2];
        wsD   = argv[3];
        
        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(); 
    
        if(nR!=0)
            std::cout<<"Error:"<<nR<<std::endl;
    
        return nR;
    }
  • 相关阅读:
    Web的26项基本概念和技术
    StringComparison枚举
    WebDriver使用指南(完整篇)
    Sublime Text 2 入门及技巧
    每天一个 Linux 命令(16):which whereis locate命令
    每天一个 Linux 命令(13):less 命令
    每天一个 Linux 命令(12):more命令
    每天一个linux命令(11):nl命令
    每天一个linux命令(10):cat 命令
    每天一个命令 ls
  • 原文地址:https://www.cnblogs.com/vc60er/p/4043246.html
Copyright © 2020-2023  润新知