Delphi使用Word ActiveX将doc转换为pdf

前端之家收集整理的这篇文章主要介绍了Delphi使用Word ActiveX将doc转换为pdf前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
有没有人已经使用Word 2007或Word 2010 pdf导出功能编写了将doc或docx转换为pdf的代码

解决方法

我使用以下.vbs脚本执行此操作.如果你需要Delphi代码,那么转换就很容易了:
  1. Const wdDoNotSaveChanges = 0
  2. Const wdRevisionsViewFinal = 0
  3. Const wdFormatPDF = 17
  4.  
  5. Dim arguments
  6. Set arguments = WScript.Arguments
  7.  
  8. Function DOC2PDF(sDocFile)
  9.  
  10. Dim fso ' As FileSystemObject
  11. Dim wdo ' As Word.Application
  12. Dim wdoc ' As Word.Document
  13. Dim wdocs ' As Word.Documents
  14.  
  15. Set fso = CreateObject("Scripting.FileSystemObject")
  16. sDocFile = fso.GetAbsolutePathName(sDocFile)
  17. sPdfFile = fso.GetParentFolderName(sDocFile) + "\" + fso.GetBaseName(sDocFile) + ".pdf"
  18.  
  19. Set wdo = CreateObject("Word.Application")
  20. Set wdocs = wdo.Documents
  21. WScript.Echo "opening: " + sDocFile
  22. Set wdoc = wdocs.Open(sDocFile)
  23. if fso.FileExists(sPdfFile) Then
  24. fso.DeleteFile sPdfFile,True
  25. End If
  26. WScript.Echo "Converting to PDF: " + sPdfFile
  27. Set wview = wdoc.ActiveWindow.View
  28. wview.ShowRevisionsAndComments = False
  29. wview.RevisionsView = wdRevisionsViewFinal
  30. wdoc.SaveAs sPdfFile,wdFormatPDF
  31. WScript.Echo "Conversion completed"
  32. wdo.Quit wdDoNotSaveChanges
  33.  
  34. Set fso = Nothing
  35. Set wdo = Nothing
  36.  
  37. End Function
  38.  
  39. If arguments.Count=1 Then
  40. Call DOC2PDF(arguments.Unnamed.Item(0))
  41. Else
  42. WScript.Echo "Generates a PDF file from a Word document using Word PDF export."
  43. WScript.Echo ""
  44. WScript.Echo "Usage: doc2pdf.vbs <doc-file>"
  45. WScript.Echo ""
  46. End If

猜你在找的Delphi相关文章