打印到“Microsoft打印到PDF”时如何设置文件名在德尔福 [英] How to set filename when printing to "Microsoft print to PDF" in delphi

查看:1701
本文介绍了打印到“Microsoft打印到PDF”时如何设置文件名在德尔福的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打算在打印到PDF时设置文件名。设置(Printers.pas)Printer.Title可以为大多数PDF打印引擎(Adobe,CutePDF)默认使用PDF文件名,但它不适用于Microsoft打印到PDF或Microsoft XPS Document Writer



当Printer.BeginDoc被调用时,在打印对话框中设置文件名的方法是什么?如果设置Printer.Title是正确的方法,那么是否有Windows打印到PDF的解决方法?



使用Delphi XE。



谢谢!

解决方案

如果您没有使用vcl进行打印,您可以设置输出路径。这意味着您必须使用


I am trying to set the filename when printing to PDF. Setting (Printers.pas) Printer.Title works to default the PDF filename for most PDF printing engines (Adobe, CutePDF), however it does not work for "Microsoft print to PDF" nor "Microsoft XPS Document Writer"

What is the correct way to set the filename in the print dialog invoked when Printer.BeginDoc is called? If setting Printer.Title is the correct way, then is there a workaround for "Windows print to PDF"?

Using Delphi XE.

Thanks!

解决方案

You can set the output path if you do printing without vcl. That means you have to use the DOCINFO structure, named TDocInfo from the WinApi.Windows unit. I've sligtly adapted the official example from Embarcadero:

procedure TForm1.Button1Click(Sender: TObject);
var
  Pd : TPrintDlg;
  DocInfo: TDocInfo;
const
  DOC_NAME = 'Stack Overflow';
  FILE_NAME = 'C:\temp\print\SO.pdf';
  MAX_PATH = 260;
begin
  Pd := default(TPrintDlg);
  Pd.lStructSize := sizeof(Pd);
  Pd.hWndOwner := Form1.Handle;
  Pd.Flags := PD_RETURNDC;
  if PrintDlg(pd) then begin
    DocInfo := Default(TDocInfo);
    DocInfo.cbSize := SizeOf(DocInfo);
    DocInfo.lpszDocName := StrAllocW(32);
    DocInfo.lpszOutput := StrAllocW(MAX_PATH);
    lStrCpynW(DocInfo.lpszDocName, DOC_NAME, Length(DOC_NAME) * sizeof(char));
    lStrCpynW(DocInfo.lpszOutput, FILE_NAME, Length(FILE_NAME) * sizeof(char));
    StartDoc(Pd.hDc, DocInfo);
    StartPage(Pd.hDc);
    TextOut(Pd.hDc, 100, 100, 'Page 1', 6);
    EndPage(Pd.hDc);
    StartPage(Pd.hDc);
    TextOut(Pd.hDc, 100, 100, 'Page 2', 6);
    EndPage(Pd.hDc);
    EndDoc(Pd.hDc);
    StrDisposeW(DocInfo.lpszDocName);
    StrDisposeW(DocInfo.lpszOutput);
  end;
end;

Setting lpszOutput enables you to set the output file name if you select "Microsoft Print To Pdf" as printer.

这篇关于打印到“Microsoft打印到PDF”时如何设置文件名在德尔福的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆