10

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!

1
  • 1
    You can't. As an alternative you can use Arnaud Bouchez' excellent SynPDF engine to create PDF files on the fly. Commented Jan 19, 2017 at 18:03

2 Answers 2

10

You can try.

var
  DeviceMode: THandle;
  Device, Driver, Port: array[0..80] of Char;
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;
  Printer.Canvas.TextOut(100, 100, 'Test');
  Printer.EndDoc;
end;
Sign up to request clarification or add additional context in comments.

3 Comments

Really good ! Much better than the other answer, should be the accepted one.
I have tried this with: - Bullzip PDF - does NOT work (prints into Postscript format) - BLC easyPDF - does NOT work (still prompts for a filename) - Microsoft Print to PDF - DOES work exactly as original question requires. So this answer is not really an universal solution, but the problem seems to be within each individual PDF printer driver, and not with this answer.
Thank you! It worked in Delphi 7 also. "Printer.SetPrinter(Device, Driver, 'C:\Temp\Test.pdf', 0);"
5

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.

created pdf file

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.