如何将.docx和.rtf文件发送到;“Microsoft打印到PDF”;Delphi中的打印机?

如何将.docx和.rtf文件发送到;“Microsoft打印到PDF”;Delphi中的打印机?,delphi,pdf,printing,Delphi,Pdf,Printing,我想将现有的docx或rtf文件打印到内置的“Microsoft打印到PDF”打印机上。 我有一个简单的Delphi代码: uses Printers; procedure TfrmMain.Button1Click(Sender: TObject); var DeviceMode: THandle; Device, Driver, Port: array[0..80] of Char; Printer: TPrinter; begin Printer.PrinterInd

我想将现有的docx或rtf文件打印到内置的“Microsoft打印到PDF”打印机上。 我有一个简单的Delphi代码:

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;
您只能“打印”TCanvas类支持的内容。不幸的是,它不支持呈现docx或rtf文档

您可以通过使用命令“print”调用ShellExecute来打印RTF或DOCX文件。这将启动相关应用程序,请求它打印您传递的文档文件

或者(更好的控制)您可以使用Delphi提供的WORD组件(默认情况下未安装)来自动化WORD,使其加载并打印RTF或DOCX文档。您还可以自动将自己的渲染结果呈现到文档中,前提是可以使用Word(这种情况的可能性很大)


很久以前,我写了一篇关于用Delphi应用程序中的Word打印的博客文章。请参见

困难部分是
docx
部分。您需要Microsoft Word或其他软件来打印此类文件。如果系统上安装了Microsoft Word,请使用OLE自动执行Microsoft Word。因此,如果我在打印docx文件时理解正确,“魔法”就在MSWord本身,而不是打印机中。我想rtf也是这样。我明白了。是的,实现自动化不是一个问题。我想知道我自己的猜测是“Microsoft打印到PDF”起了作用:)我只是错过了它使用TCanvas,没有更多。我需要在未安装word应用程序的情况下打印到.pdf,因此不能选择自动化。显然,我需要一些外部库或组件来在我的delphi应用程序中使用。“显然,我需要一些外部库或组件来在我的delphi应用程序中使用。”据我所知,没有实现Word DOCX文件格式所有功能的库。一些自由软件或多或少实现了一个完整的子集。上次我检查一个,它丢失了很多东西。