delphiprintingvcl

In Delphi TPrintDialog, can the printer name and paper size be programmatically set?


Using Delphi VCL. When the user starts a print job, I display TPrintDialog.

Ideally, the print dialog would show the name of the printer, paper size and orientation that the user last used (which I have saved). But while I can pass the orientation to the printer and have it appear in the dialog, I can find no way to have the dialog show anything other than the default printer and paper size.

I could design a custom dialog that looks like the print dialog, and pass the parameters that way, but it seems like there should be a way to pass these to TPrintDialog.


Solution

  • TPrintDialog does not natively support the values you are asking about. However, internally it just calls the Win32 PrintDlg() API, which can be initialized using a DEVMODE struct, which has dmOrientation and dmPaperSize fields.

    TPrintDialog retrieves a DEVMODE from TPrinter before calling PrintDlg(), and then assigns the resulting DEVMODE back to TPrinter if the dialog is successful. So, you can use the TPrinter.SetPrinter() method to provide your own DEVMODE for the desired device before invoking TPrintDialog.

    The TPrinter.Orientation property reads/writes the dmOrientation field of the TPrinter's currently assigned DEVMODE. TPrinter does not have a property for reading/writing the dmPaperSize field, but you can do that manually.