Delphi 使用 Word ActiveX 将 doc 转换为 pdf [英] Delphi convert doc to pdf using Word ActiveX

查看:29
本文介绍了Delphi 使用 Word ActiveX 将 doc 转换为 pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有人已经使用 Word 2007 或 Word 2010 pdf 导出功能编写了将 doc 或 docx 转换为 pdf 的代码?

Did anyone already wrote code for converting doc or docx to pdf using Word 2007 or Word 2010 pdf export capabilities?

推荐答案

我使用以下 .vbs 脚本来完成.如果您在 Delphi 代码中需要它,那么转换起来就很容易了:

I do it with the following .vbs script. If you need it in Delphi code then it would be easy enough to convert:

Const wdDoNotSaveChanges = 0
Const wdRevisionsViewFinal = 0
Const wdFormatPDF = 17

Dim arguments
Set arguments = WScript.Arguments

Function DOC2PDF(sDocFile)

  Dim fso ' As FileSystemObject
  Dim wdo ' As Word.Application
  Dim wdoc ' As Word.Document
  Dim wdocs ' As Word.Documents

  Set fso = CreateObject("Scripting.FileSystemObject")
  sDocFile = fso.GetAbsolutePathName(sDocFile)
  sPdfFile = fso.GetParentFolderName(sDocFile) + "" + fso.GetBaseName(sDocFile) + ".pdf"

  Set wdo = CreateObject("Word.Application")
  Set wdocs = wdo.Documents
  WScript.Echo "Opening: " + sDocFile
  Set wdoc = wdocs.Open(sDocFile)
  if fso.FileExists(sPdfFile) Then
    fso.DeleteFile sPdfFile, True
  End If
  WScript.Echo "Converting to PDF: " + sPdfFile
  Set wview = wdoc.ActiveWindow.View
  wview.ShowRevisionsAndComments = False
  wview.RevisionsView = wdRevisionsViewFinal
  wdoc.SaveAs sPdfFile, wdFormatPDF
  WScript.Echo "Conversion completed"
  wdo.Quit wdDoNotSaveChanges

  Set fso = Nothing
  Set wdo = Nothing

End Function

If arguments.Count=1 Then 
  Call DOC2PDF(arguments.Unnamed.Item(0))
Else
  WScript.Echo "Generates a PDF file from a Word document using Word PDF export."
  WScript.Echo ""
  WScript.Echo "Usage: doc2pdf.vbs <doc-file>"
  WScript.Echo ""
End If

这篇关于Delphi 使用 Word ActiveX 将 doc 转换为 pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆