作者:kenvilen_106 | 来源:互联网 | 2024-11-12 16:19
[转]doc,ppt,xls文件格式转PDF格式http:blog.csdn.netlee353086articledetails7920355确实好用。需要注意的是#import
[转]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
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\n " , 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\n " , 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] " <<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: " <std::endl;
return nR;
}