【问题标题】:Convert Office 2007-2012 docs to pdf and image in Qt在 Qt 中将 Office 2007-2012 文档转换为 pdf 和图像
【发布时间】:2018-10-24 01:20:41
【问题描述】:

我们开发了一款应用,可以使用 LibreOffice 将 DOC/XLS 文件作为图像导入。但是,对于已经安装了 Office 的 PC,我们希望能够使用已安装的 MS Office。大多数人都使用 MS Office 版本 2007-2010。我知道 MS 为此目的提供了某种互操作性 DLL,但我还没有看到任何与 Qt 一起使用的示例。

更新 - 这个答案看起来很有趣,在 Qt 中可以做同样的事情吗?

Convert Word file pages to jpg images using C#

【问题讨论】:

  • 你能澄清你的问题吗?

标签: c++ qt ms-office


【解决方案1】:

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 对象。希望对您有所帮助。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-29
  • 2018-01-25
  • 1970-01-01
相关资源
最近更新 更多