Ivan Peshev
Ivan Peshev

Reputation: 455

How to send .docx and .rtf file to "Microsoft Print to PDF" printer in Delphi?

I want to print exising docx or rtf file to the build-on "Microsoft Print to PDF" printer. I have a simple Delphi code:

uses
  Printers;

procedure TfrmMain.Button1Click(Sender: TObject);
var
  DeviceMode: THandle;
  Device, Driver, Port: array[0..80] of Char;
  Printer: TPrinter;
begin
  Printer.PrinterIndex := Printer.Printers.IndexOf('Microsoft Print to PDF');
  Printer.GetPrinter(Device, Driver, Port, DeviceMode);
  Printer.SetPrinter(Device, Driver, 'C:\Temp\Test.pdf', 0);
  Printer.BeginDoc;
  ......... I can use canvas to print simple texts BUT.....
  .........? how to print an existing docx or rtf file ?....
  Printer.EndDoc;
end;

Upvotes: 1

Views: 1145

Answers (1)

fpiette
fpiette

Reputation: 12322

You can only "print" what the TCanvas class support. And unfortunaley, it does not support rendering docx nor rtf documents.

You can print RTF or DOCX files by invoking ShellExecute with the command 'print'. This will start the associated application requesting it to print the document file you pass.

Or (Better control) you can use the WORD component provided with Delphi (Not installed by default) to automate Word, make it load and print the RTF or DOCX document. You can also automate your own rendering into the document provided it is possible to do it with Word (And chances are big that it is the case).

Long time ago I write a blog post about printing with Word from a Delphi application. See http://francois-piette.blogspot.com/2014/02/automate-word-document-print-using.html

Upvotes: 1

Related Questions