介绍用编程解决Word转PDF的几种方案

一、VC运用OLE编程,office的编程接口,参考网上的,自己更改优化后加心得转换具体用法:

1添加dll路径

#import "C:\\Program Files (x86)\\Common Files\\Microsoft Shared\\OFFICE14\\MSO.DLL" \
	rename("RGB", "MSORGB") \
	rename("DocumentProperties", "MSODocumentProperties")

using namespace Office;


#import "C:\\Program Files (x86)\\Common Files\\Microsoft Shared\\VBA\\VBA6\\VBE6EXT.OLB"

using namespace VBIDE;


#import "C:\\Program Files (x86)\\Microsoft Office\\Office14\\MSWORD.OLB" rename_namespace("MSWord") \
	auto_search \
	auto_rename \
	no_auto_exclude \
	rename("ExitWindows", "WordExitWindows")

using namespace MSWord;

2.转换的代码

BOOL CConverter::Convert(CString strSourcePath, CString strTargetPath, MSWord::WdExportFormat wdExportFormat)
{

	BOOL						result = FALSE;
	MSWord::_ApplicationPtr		pWdApplicationPtr;
	MSWord::_DocumentPtr		pWdDocumentPtr;

	COleVariant					sourcePath = strSourcePath;
	COleVariant					targetPath = strTargetPath;
	COleVariant					vTrue((short)TRUE);
	COleVariant					vFalse((short)FALSE);
	COleVariant					vZero((short)0);
	COleVariant					vOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);

	CoInitialize(NULL);
	try
	{
		HRESULT hResult = pWdApplicationPtr.CreateInstance("Word.Application");
		if (hResult != S_OK)
		{
			AfxMessageBox(_T("Application创建失败,请确保安装了word 2000或以上版本!"), MB_OK|MB_ICONWARNING);
			CoUninitialize();
			return result;
		}
		pWdDocumentPtr = pWdApplicationPtr->Documents->Open(sourcePath, 
						vTrue,				// Confirm Conversion.
						vTrue,				// ReadOnly.
						vFalse,				// AddToRecentFiles.
						vOptional,			// PasswordDocument.
						vOptional,			// PasswordTemplate.
						vOptional,			// Revert.
						vOptional,			// WritePasswordDocument.
						vOptional,			// WritePasswordTemplate.
						vOptional,			// Format. // Last argument for Word 97
						vOptional,			// Encoding // New for Word 2000/2002
						vFalse,				// visible
						vOptional,			// openAndRepair
						vZero,				// docDirection
						vOptional,			// NoEncodingDialog
						vOptional);
		//pWdDocumentPtr = pWdApplicationPtr->Documents->Open2000(sourcePath, 
		//						vTrue,				// Confirm Conversion.
		//						vFalse,				// ReadOnly.
		//						vFalse,				// AddToRecentFiles.
		//						vOptional,			// PasswordDocument.
		//						vOptional,			// PasswordTemplate.
		//						vOptional,			// Revert.
		//						vOptional,			// WritePasswordDocument.
		//						vOptional,			// WritePasswordTemplate.
		//						vOptional,			// Format. // Last argument for Word 97
		//						vOptional,			// Encoding // New for Word 2000/2002
		//						vOptional);			// Visible

		if(pWdDocumentPtr == NULL)
		{
			CoUninitialize();
			return result;
		}
		hResult = pWdDocumentPtr->ExportAsFixedFormat((_bstr_t )strTargetPath, // target path
					wdExportFormat,					// export format
					FALSE,							// Open After Export
					wdExportOptimizeForPrint,		// Export OptimizeFor
					wdExportAllDocument,			// Export Range
					0,								// start page
					0,								// end page
					wdExportDocumentContent,		// Export Item
					TRUE,							// Include Doc Props
					TRUE,							// Keep IRM
					wdExportCreateHeadingBookmarks,	// Export Create Bookmarks
					TRUE,							// Doc Structure Tags
					TRUE,							// Bitmap Missing Fonts
					FALSE);							// Use ISO19005_1
		if (hResult == S_OK)
		{
			result = TRUE;
		}
	}
	catch(CException* e)
	{
		TCHAR   szError[1024];   
		e->GetErrorMessage(szError,1024);   //  e.GetErrorMessage(szError,1024); 
		AfxMessageBox(_T("WORD文档转PDF异常:") + (CString)szError); 
	}
	if(pWdDocumentPtr != NULL)
	{
		pWdDocumentPtr->Close();
		pWdDocumentPtr = NULL;
	}
	if(pWdApplicationPtr != NULL)
	{
		pWdApplicationPtr->Quit();
		pWdApplicationPtr = NULL;
	}
	CoUninitialize();
	return result;
}
3.具体调用是酱紫的

bResult = CConverter::Convert(strInput, strOutput + strOutputName, wdExportFormatPDF);//第一个word的路径名,第二个导出路径+导出文件名

4特别说明一下:转换代码参数中  wdExportCreateHeadingBookmarks这个参数比较重要,转的时候 带书签哦。如果参数是其他参数的话就没有书签就转不过去哦。


二、C#运用 Aspose.Word.dll库   具体用法:


引入:

using Aspose.Words;
主要处理就是两句话:strinputName就是需要转换的文档

                Document doc = new Document(strinputName);
                doc.SaveToPdf(savePath);
完了。就这么简单


三、VC运用 PDF虚拟打印机的方法。PDF虚拟打印机一搜一大把,顺便一个,但是安装好了要设置成默认打印机,接下来看代码:

ShellExecute(NULL,_T("print"),strInputPath,NULL,NULL,SW_HIDE);


没有搞错,就只有一句代码,我的天呐,就是这么神奇。
解释下 strInputpath是word文件的路径。SW_HIDE主要是用来隐藏打印窗口的。


这个文写完了。。。没有了。。。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值