Armin Taghavizad
Armin Taghavizad

Reputation: 1685

Printer Print Size

How can I set the printing size (width and height) by code without showing a dialog?

Upvotes: 1

Views: 7503

Answers (4)

Ken White
Ken White

Reputation: 125620

See the MSDN documentation for GetPrinter and SetPrinter. You can find basic examples of their use in Delphi here and here. The second example has specific code for setting paper size, which I've provided below.

procedure SetPrinterSettings(FPrinter: TPrinter);
var
  FDevice: PChar;
  FDriver: PChar;
  FPort: PChar;
  DeviceMode: THandle;
  DevMode: PDeviceMode;
begin
  {to get a current printer settings}
  FPrinter.GetPrinter(FDevice, FDriver, FPort, DeviceMode);
  {lock a printer device}
  DevMode := GlobalLock(DeviceMode);

  {set a paper size as A4-Transverse}
  if ((DevMode^.dmFields and DM_PAPERSIZE) = DM_PAPERSIZE) then
  begin
    DevMode^.dmFields := DevMode^.dmFields or DM_PAPERSIZE;
    DevMode^.dmPaperSize := DMPAPER_A4_TRANSVERSE;
  end;

  {set a paper source as Tractor bin}
  if  ((DevMode^.dmFields and DM_DEFAULTSOURCE) = DM_DEFAULTSOURCE) then
  begin
    DevMode^.dmFields := DevMode^.dmFields or DM_DEFAULTSOURCE;
    DevMode^.dmDefaultSource := DMBIN_TRACTOR;
  end;

  {set a Landscape orientation}
  if  ((DevMode^.dmFields and DM_ORIENTATION) = DM_ORIENTATION) then
  begin
    DevMode^.dmFields := DevMode^.dmFields or DM_ORIENTATION;
    DevMode^.dmOrientation := DMORIENT_LANDSCAPE;
  end;

  {set a printer settings}
  FPrinter.SetPrinter(FDevice, FDriver, FPort, DeviceMode);

  {unlock a device}
  GlobalUnlock(DeviceMode);
end;

Upvotes: 4

Andreas Rejbrand
Andreas Rejbrand

Reputation: 108929

If you just want to alter the margins, this is easy, but depends on how you print.

If you are printing manually (using Printer.BeginDoc etc.), then you simply draw on the priter's canvas further from the edges! If you are printing using a TRichEdit, you can change the PageRect property.

Upvotes: 0

David
David

Reputation: 13580

From your comments to other answers, it seems like you want to select the type of paper (A4, Legal, etc) - is that right?

This page states that to select the paper type, you need to use the Printer.GetPrinter function to get a device handle for the printer, then use GlobalLock to get a pointer you can access, cast to a PDeviceMode. There is then a PaperSize member of the TDeviceMode that pointer points to which can be A4, legal, etc.

This was all just turned up via Google. Scroll to 'Printer Properties' on this page for where I got this info. This page mentions changing the tray too.

Upvotes: 3

X-Ray
X-Ray

Reputation: 2846

Here's one little piece that helps get the font size correct:

Printer.Canvas.Font.PixelsPerInch:=GetDeviceCaps(Printer.Canvas.Handle,LOGPIXELSY);

Upvotes: 2

Related Questions