2006. február 3., péntek

How to change the page orientation in the middle of a print job


Problem/Question/Abstract:

Is it possible to change the printer orientation from portrait to landscape in the middle of a print job?

Answer:

procedure TForm1.Button2Click(Sender: TObject);
var
  Device: array[0..255] of char;
  Driver: array[0..255] of char;
  Port: array[0..255] of char;
  hDeviceMode: THandle;
  pDevMode: PDeviceMode;
begin
  with Printer do
  begin
    BeginDoc;
    try
      Canvas.font.size := 20;
      Canvas.font.name := 'Arial';
      Canvas.TextOut(50, 50, 'This is portait');
      GetPrinter(Device, Driver, Port, hDeviceMode);
      pDevMode := GlobalLock(hDevicemode);
      with pDevMode^ do
      begin
        dmFields := dmFields or DM_ORIENTATION;
        dmOrientation := DMORIENT_LANDSCAPE;
      end;
      { Cannot use NewPage here since the ResetDc will only work between EndPage
      and StartPage. As a consequence the Printer.PageCount is not updated. }
      Windows.EndPage(Printer.Handle);
      if ResetDC(canvas.Handle, pDevMode^) = 0 then
        ShowMessage('ResetDC failed, ' + SysErrorMessage(GetLastError));
      GlobalUnlock(hDeviceMode);
      Windows.StartPage(Printer.Handle);
      Printer.Canvas.Refresh;
      Canvas.font.size := 20;
      Canvas.font.name := 'Arial';
      Canvas.TextOut(50, 50, 'This is landscape');
    finally
      EndDoc;
    end;
  end;
end;

Nincsenek megjegyzések:

Megjegyzés küldése