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

时间:2022-08-26 14:19:32

[转]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;
}