docx文件格式转PDF格式

/*
功能:
 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;
	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] <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;
}

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kagula086

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值