转载于: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;
}