VC 实现Word、Excel、PPT转PDF

转载于:https://www.cnblogs.com/MakeView660/p/6929982.html

// Office2Pdf.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

/*
    功能:Office文件格式(doc/docx、xls/xlsx、ppt/pptx)转PDF格式文件
    作者: ReOrganized by Michael Joessy 2017-06-01 Happy Children's Day
    使用前提
    [1]Office 2010(Word, Excel, PPT)
    编译环境:
    [1]VS2008
    [2]Win7 x64
    工程类型:Win32 Console Application
*/


//导入Office类型库
#pragma warning(disable:4786) 
#import "C:\Program Files (x86)\Common Files\microsoft shared\OFFICE14\MSO.DLL" \
    rename("RGB","_OfficeRGB")         

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



#import "C:\Program Files (x86)\Microsoft Office\Office14\MSWORD.OLB " \
    rename("FindText","_FindText")\
    rename("Rectangle","_Rectangle")\
    rename("ExitWindows","_ExitWindows")


#import "C:\Program Files (x86)\Microsoft Office\Office14\MSPPT.OLB"
#import "C:\Program Files (x86)\Microsoft Office\Office14\EXCEL.EXE" \
    rename("DialogBox","_DialogBox") \
    rename("RGB","_RGB") \
    exclude("IFont","IPicture")


//使用头文件
#include <string>
#include <iostream>


//声明
int Word2Pdf(std::wstring inputFileName,std::wstring outputFileName);
int Excel2Pdf(std::wstring inputFileName,std::wstring outputFileName);
int Ppt2Pdf(std::wstring inputFileName,std::wstring outputFileName);


//主函数
int _tmain(int argc, _TCHAR* argv[])
{
    int nRet = 0;
    HRESULT hr = CoInitialize(NULL);
    if (FAILED(hr))
    {
        std::cout << "Initialize COM failed..." << std::endl;
        return 0;
    }

    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 MyFile.xlsx MyFile.pdf" << std::endl;
        return 1;
    }

    wsCmd = argv[1];
    wsS   = argv[2];
    wsD   = argv[3];

    if(wsCmd == L"-e")
    {
        nRet = Excel2Pdf(wsS.c_str(),wsD.c_str());
    }
    else if(wsCmd == L"-p")
    {
        nRet = Ppt2Pdf(wsS.c_str(),wsD.c_str());
    }
    else if(wsCmd == L"-w")
    {
        nRet = Word2Pdf(wsS.c_str(),wsD.c_str());
    }
 
    CoUninitialize(); 

    if(nRet != 0)
    {
        std::cout << "Error:" << nRet << std::endl;
    }

    return nRet;
}



//实现Word->PDF
int Word2Pdf(std::wstring inputFileName,std::wstring outputFileName)  
{
    int nRet = 0;
    Word::_ApplicationPtr  pWordApp = NULL; 
    Word::_DocumentPtr   pDoc = NULL; 
    HRESULT hr = S_OK;

    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)
    {
        nRet = 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(...)
    {
        nRet = 3;
    }

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

    return nRet;
}


//EXCEL->PDF
int Excel2Pdf(std::wstring inputFileName,std::wstring outputFileName)
{
    HRESULT hr = S_OK;
    int nRet = 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\n", hr);
        return 1;
    } 
    try
    {
        pThisWorkbook = pApplication->GetWorkbooks()->Open(szBstrInputFileName);
        pThisWorkbook->ExportAsFixedFormat(Excel::XlFixedFormatType::xlTypePDF, szBstrOutputFileName);
        pThisWorkbook->Close();
        pThisWorkbook.Release();
        pThisWorkbook = NULL;

    }catch(...)
    {
        nRet = 2;
    }

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

    return nRet;
}


//PPT->PDF
int Ppt2Pdf(std::wstring inputFileName,std::wstring outputFileName)
{
    PowerPoint::_ApplicationPtr spPpApp = NULL; 
    PowerPoint::PresentationsPtr  spPres = NULL;
    PowerPoint::_PresentationPtr  pPre = NULL;

    BSTR szBstrInputFileName;
    BSTR szBstrOutputFileName;
    BSTR szBstrEmpty;
    HRESULT hr = S_OK;
    int nRet = 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\n", hr); 
        return 1; 
    } 

    spPres = spPpApp->Presentations; 
    if(spPres==NULL)
    {
        nRet = 2;
        goto _RELEASE_APP;
    }

    try
    {
        pPre = spPres->Open(szBstrInputFileName,
            Office::MsoTriState::msoTrue,Office::MsoTriState::msoFalse,Office::MsoTriState::msoFalse);
        if(pPre ==NULL)
        {
            nRet = 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(...)
    {
        nRet = 4;
    }

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

    return nRet;
}

 

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值