Delphi convert doc to pdf using Word ActiveX

10,850

Solution 1

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

Solution 2

I haven’t so far, but it shouldn’t be difficult:

Here's a basic skeleton:

uses
  ComObj;
const
  wdExportFormatPDF = 17;
var 
  Word, Doc: OleVariant;
begin
  Word := CreateOLEObject('Word.Application');
  Doc := Word.Documents.Open('C:\Document.docx');
  Doc.ExportAsFixedFormat('C:\Document.pdf', wdExportFormatPDF);
end;

Note that I’ve declare both the Word and Doc variables as OleVariants, so as to be version-indepent (i.e. this code will work whether you’re using Word 2007 or 2010). You could also use the VCL Office component libraries if you wanted. If you were to do a lot of processing in the document itself, that would definitely be faster.

Share:
10,850
UnDiUdin
Author by

UnDiUdin

I am a Delphi Developer using SQL Server mostly as backend database. I have some web development and Unity/webgl skills. I currenly mostly Instant Developer Foundation by Progamma srl, check it out: https://www.instantdeveloper.com/en/products/instant-developer-foundation/ Any fool can write code that a computer can understand. Good programmers write code that humans can understand. — M. Fowler The reason why I choose "UnDiUdin" as username is that where I live (Friuli, Italy) when there is a need to search for an expert we mention "a person from Udine", "un di Udin" in the frioulan language. So even if i am not from Udine I give myself the name of an expert...

Updated on June 04, 2022

Comments

  • UnDiUdin
    UnDiUdin almost 2 years

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