一、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;
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的路径名,第二个导出路径+导出文件名
二、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);
这个文写完了。。。没有了。。。