program WordToPDF;
{$APPTYPE CONSOLE}
{$DEFINE LATE_BINDING}
uses
SysUtils,
Variants,
Dialogs,
ActiveX,
Windows,
Word_TLB,
UDC_TLB;
procedure PrintWordToPDF(WordDocFilePath: string);
var
objUDC: IUDC;
Printer: IUDCPrinter;
Profile: IProfile;
WordApp: WordApplication;
WordDoc: WordDocument;
FilePath: OleVariant;
ReadOnly: OleVariant;
Missing: OleVariant;
Background: OleVariant;
SaveChanges: OleVariant;
begin
objUDC := CoAPIWrapper.Create;
Printer := objUDC.get_Printers('Universal Document Converter');
Profile := Printer.Profile;
Profile.PageSetup.ResolutionX := 600;
Profile.PageSetup.ResolutionY := 600;
Profile.FileFormat.ActualFormat := FMT_PDF;
Profile.FileFormat.PDF.ColorSpace := CS_TRUECOLOR;
Profile.FileFormat.PDF.Multipage := MM_MULTI;
Profile.OutputLocation.Mode := LM_PREDEFINED;
Profile.OutputLocation.FolderPath := 'c:\UDC Output Files';
Profile.OutputLocation.FileName := '&[DocName(0)] -- &[Date(0)] -- &[Time(0)].&[ImageType]';
Profile.OutputLocation.OverwriteExistingFile := False;
WordApp := CoWordApplication.Create;
FilePath := WordDocFilePath;
ReadOnly := True;
Missing := Variants.EmptyParam; {$IFDEF LATE_BINDING}
WordDoc := IDispatch(OleVariant(WordApp).Documents.Open(FileName := FilePath, ReadOnly := ReadOnly)) as WordDocument;
{$ELSE}
WordDoc := WordApp.Documents.Open(FilePath, Missing, ReadOnly, Missing, Missing, Missing,
Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing);
{$ENDIF}
Background := False;
WordApp.ActivePrinter := 'Universal Document Converter';
{$IFDEF LATE_BINDING}
OleVariant(WordApp).PrintOut(Background := False);
{$ELSE}
WordApp.PrintOut(Background, Missing, Missing, Missing, Missing, Missing, Missing,
Missing, Missing, Missing, Missing, Missing, Missing,
Missing, Missing, Missing, Missing, Missing, Missing);
{$ENDIF}
SaveChanges := False;
{$IFDEF LATE_BINDING}
OleVariant(WordDoc).Close(SaveChanges := SaveChanges);
{$ELSE}
WordDoc.Close(SaveChanges, Missing, Missing);
{$ENDIF}
{$IFDEF LATE_BINDING}
OleVariant(WordApp).Quit;
{$ELSE}
WordApp.Quit(Missing, Missing, Missing);
{$ENDIF}
end;
var
TestFilePath: string;
begin
TestFilePath := ExtractFilePath(ParamStr(0)) + 'TestFile.doc';
try
CoInitialize(nil);
try
PrintWordToPDF(TestFilePath);
finally
CoUninitialize;
end;
except
on E: Exception do
MessageDlg(E.ClassName + ' : ' + E.Message, mtError, [mbOK], 0);
end;
end.
|