[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 172
  • Last Modified:

How change papersize programmatically?

Hi!

   I'm developing a database-application, in which I wanna make printouts on any papersize supported by the printer. My question is this:

   The user of the application has told the program that he/she wants a report written on portait-orientated A4-sized paper (210 * 297 mm). How do I change the printer's papersize programmatically, if it's not set to the requested size? I don't want to tell the user to open up the "Printer Setup" dialog,
   I wanna do it totally automatic. I can solve the orientation bit, but how do I change the papersize?
   I'm using Delphi 2.0.


                  Thanks for your help!!

//Daniel Persson, Malmoe, Sweden
0
Dippen
Asked:
Dippen
  • 2
1 Solution
 
DippenAuthor Commented:
Edited text of question
0
 
pivarCommented:
Hej Daniel,

This sample code should do the trick:

---------------------------------------------------
var
  pDevMode: PDeviceMode;
  devHandle: THandle;
  device,driver,port: Array[0..255] of AnsiChar;
begin
  Printer.GetPrinter(device,driver,port,devHandle);
  pDevMode := GlobalLock(devHandle);
  pDevMode.dmFields := pDevMode.dmFields or DM_PAPERSIZE;
  pDevMode.dmPaperSize := DMPAPER_A4;
  GlobalUnlock(devHandle);

  Printer.BeginDoc;
etc...
---------------------------------------------------
With this you should be able to change other things as well, the orientation for example. Look in the WIN32 help, item DEVMODE.

If you haven't used Printer before the first line: Printer.GetPrinter, you will get an AccessViolation error. I haven't solved this yet,  but since the printer object is initialized when you use it the first time, I suspect it has to do with the printer object not been initialized before GetPrinter is run. To get around this you only have to use Printer before this. For example, get the printer name. 'name := Printer.Printers[Printer.PrinterIndex]'.

This code uses the Printer-objects internal DEVMODE structure to change the items you want.

/pivar
0
 
DippenAuthor Commented:
Thanks for your simple and perfectly working answer, pivar!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now