Author: Jonas Bilinkevicius
Is it possible to change the printer orientation from portrait to landscape in the
middle of a print job?
Answer:
1 2 procedure TForm1.Button2Click(Sender: TObject);
3 var4 Device: array[0..255] of char;
5 Driver: array[0..255] of char;
6 Port: array[0..255] of char;
7 hDeviceMode: THandle;
8 pDevMode: PDeviceMode;
9 begin10 with Printer do11 begin12 BeginDoc;
13 try14 Canvas.font.size := 20;
15 Canvas.font.name := 'Arial';
16 Canvas.TextOut(50, 50, 'This is portait');
17 GetPrinter(Device, Driver, Port, hDeviceMode);
18 pDevMode := GlobalLock(hDevicemode);
19 with pDevMode^ do20 begin21 dmFields := dmFields or DM_ORIENTATION;
22 dmOrientation := DMORIENT_LANDSCAPE;
23 end;
24 { Cannot use NewPage here since the ResetDc will only work between EndPage25 and StartPage. As a consequence the Printer.PageCount is not updated. }26 Windows.EndPage(Printer.Handle);
27 if ResetDC(canvas.Handle, pDevMode^) = 0 then28 ShowMessage('ResetDC failed, ' + SysErrorMessage(GetLastError));
29 GlobalUnlock(hDeviceMode);
30 Windows.StartPage(Printer.Handle);
31 Printer.Canvas.Refresh;
32 Canvas.font.size := 20;
33 Canvas.font.name := 'Arial';
34 Canvas.TextOut(50, 50, 'This is landscape');
35 finally36 EndDoc;
37 end;
38 end;
39 end;