views:

77

answers:

4

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

A: 

What exactly do you mean by "printing size"? Do you mean that you want to select the type of paper to print on, if your printer is loaded with papers of different format? Then I do not know.

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.

Andreas Rejbrand
yes, I mean (as i told) format of paper. I wonna change it (change paper width and height) by CODE
Armin
A: 

there's a lot to this...

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

Printer.Canvas.Font.PixelsPerInch:=GetDeviceCaps(Printer.Canvas.Handle,LOGPIXELSY);
X-Ray
This is not what i want. thanks
Armin
@Armin: Then perhaps you should more clearly state what you *do* want.
Ken White
+1  A: 

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.

Ken White
+1  A: 

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.

Meta-note

By the way, from all the answers and your comments - eg, one quote:

This is not what i want. thanks – Armin

@Armin: Then perhaps you should more clearly state what you do want. – Ken White

it's clear you need to learn how to ask clear questions. When you first asked a question on SO, you would have been directed to this page. If you skipped past it, go back and read it. If you did read it, you may find this 'Asking better questions' page useful to read too.

This is for your benefit - if you want a solution, and we assume you do because you asked about it, then it's in your benefit to do what you can to make your problem clear, non-confusing, and easy for someone to understand so that they can answer. No-one is obliged to answer - we just do it because we want to for whatever reason. Don't make it hard or put unnecessary obstacles in the way.

For example, you wrote "printing size" and "I mean (as i told) format of paper. I wonna change it (change paper width and height)". These could mean several things. The size of the margins? The resolution, or changing the units you print to? The type of paper, ie the physical size of the paper you will print on, if the printer supports several options? You need to be clear what you want. It might seem clear to you, but that's because you've been thinking about it - the only knowledge we have about your problem is what you wrote, so you need to include in what you write everything we need to know, from scratch, to understand the problem.

David M
Thank you for your answers. They were so useful. About my question: Sorry if i had a bad writing or question. I mean define paper size by writing print size. as YOU told define paper like A4 or etc (all of them have defined size in width and height (A4 is 210 mm in 270 mm)). I want to customize them via code for my program. This can be done by "PageSetupDialog" dialog (Dialogs component tab -> PageSetupDialog component) but i want to set it up via direct code. Hope i was clear enough. Thank you again for you answer and sorry again if im not clear enough.
Armin
@Armin - The second Delphi example in [Ken's answer](http://stackoverflow.com/questions/3864060/printer-print-size/3867666#3867666) shows how to set the paper size to A4. You'd adapt the example: set `dmPaperSize` to 0 and `dmPaperLength` and `dmPaperWidth` to your liking. Read [DEVMODE documentation](http://msdn.microsoft.com/en-us/library/dd183565%28VS.85%29.aspx) to see how these members work. (I'm not sure if all printers would comply to any arbitrary setting though..)
Sertac Akyuz
No worries, Armin - glad to help.
David M