PowerShell
看起来你可以很容易地在命令行上使用 Powershell 来完成。
打开QProcess,然后运行:
http://technet.microsoft.com/en-us/library/hh847736.aspx
powershell -Command "Import-Module MSOnline";"C:\Myscript.ps1"
(下载此脚本或将其转换为 C++ com 调用。)
https://gallery.technet.microsoft.com/office/Script-to-convert-Word-f702844d
powershell -Command "Import-Module ConvertWordPocumentToPDF.psm1";"ConvertTo-OSCPDF -Path C:\path\to\document.docx"
所以 Qt 的结束代码可能是这样的:
QProcess * process = new QProcess();
// put code to pipe stdout and stderr from the QProcess to this exe's stdout, or to a TextEdit.
process->start("powershell.exe -Command \"Import-Module ConvertWordPocumentToPDF.psm1\";\"ConvertTo-OSCPDF -Path C:\\path\\to\\document.docx\"");
Com 对象访问
DocumentBase.ExportAsFixedFormat 方法
http://msdn.microsoft.com/en-us/library/microsoft.office.tools.word.documentbase.exportasfixedformat.aspx
public void ExportAsFixedFormat(
string outputFileName,
WdExportFormat exportFormat,
bool openAfterExport,
WdExportOptimizeFor optimizeFor,
WdExportRange range,
int from,
int to,
WdExportItem item,
bool includeDocProps,
bool keepIRM,
WdExportCreateBookmarks createBookmarks,
bool docStructureTags,
bool bitmapMissingFonts,
bool useISO19005_1,
ref Object fixedFormatExtClassPtr
)
首先获取Word.Application的实例
然后使用wdApplication.Documents.Open获取文档实例
然后调用wdDocument.ExportAsFixedFormat,参数为:
$wdExportFormat = [Microsoft.Office.Interop.Word.WdExportFormat]::wdExportFormatPDF
$OpenAfterExport = $false
$wdExportOptimizeFor = [Microsoft.Office.Interop.Word.WdExportOptimizeFor]::wdExportOptimizeForOnScreen
$wdExportItem = [Microsoft.Office.Interop.Word.WdExportItem]::wdExportDocumentContent
$IncludeDocProps = $true
$KeepIRM = $true
$wdExportCreateBookmarks = [Microsoft.Office.Interop.Word.WdExportCreateBookmarks]::wdExportCreateWordBookmarks
$DocStructureTags = $true
$BitmapMissingFonts = $true
$UseISO19005_1 = $false
$wdStartPage = 0
$wdEndPage = 0
(但格式化为 c++,而不是 powershell)
然后当它完成后,释放 com 对象。
http://msdn.microsoft.com/en-us/library/kw65a0we.aspx
这两种方法我都没有测试过,但我之前从 Qt 项目中运行过 Com 对象。希望对您有所帮助。