Board index » delphi » Setting Printer Margins?

Setting Printer Margins?

Hello,

can anybody tell me how to set printer margins without using the
PrinterSetupDialog?

I want to write a non-interactive program that is used with command
line arguments:

     prntprog inifile prntfile

where prntfile is a textfile that should be printed and inifile
contains printing parameters (printer, font, printer margins...)

I use the printer like a textfile (AssignPrn, Write, Writeln).

I tried to set the printer margins with ExcludeClipRect (Win API). The
result were indeed margins on the paper, but the writeln functions
obviously do not care about that, so the beginning of the lines were
cut when I specified a left margin.

I found out the printer margins are stored in the DefaultDevMode
registry key, but it is a binary key and I have no idea about its
format.

Any tips?

-----------------------------------------
Real address is gerhard.hofm...@planat.de
Delete the nospam
Please copy all answers to my email address

 

Re:Setting Printer Margins?


Quote
Gerhard Hofmann wrote:

> Hello,

> can anybody tell me how to set printer margins without using the
> PrinterSetupDialog?

> I want to write a non-interactive program that is used with command
> line arguments:

>      prntprog inifile prntfile

> where prntfile is a textfile that should be printed and inifile
> contains printing parameters (printer, font, printer margins...)

> I use the printer like a textfile (AssignPrn, Write, Writeln).

> I tried to set the printer margins with ExcludeClipRect (Win API). The
> result were indeed margins on the paper, but the writeln functions
> obviously do not care about that, so the beginning of the lines were
> cut when I specified a left margin.

> I found out the printer margins are stored in the DefaultDevMode
> registry key, but it is a binary key and I have no idea about its
> format.

> Any tips?

I have no practice with this, but have captured some posts on this
topic.

Here is the information, I hope you find it useful.

Rkr

<BEGIN 1>

This is because the printer puts its 0,0 point at the upper left
corner of the printable region, and there is an unprintable top area
and left area (as well as right and bottom) that you need to account
for.

One simple method is to get the unprintable areas (use the Printer
Escapes under Win3.1, and GetDeviceCaps() under Win95/NT). Then, Move
the printing origin so that 0,0 is the upper left corner of the page.
This can be done with the SetViewportOrg() call.

var
        Margins: TPoint;
Begin
        ...
        Printer.Begindioc;
        Margins := GetPrinterMargins;
        SetViewportOrg(Printer.Handle, -Margins.X, -Margins.Y);
        ...

End;

The SetViewPortOrg() call MUST be called after every Printer.NewPage!

GetPrintermargins:
{ Determine non-printable region on form }
Function TMain.GetPrinterMargins : TPoint;
var
  pMarg:TPoint;
  nEscapeCode:Integer;
Begin
  pMarg := Point (0,0);
  GetPrinterMargins := Point (0,0);     { Default margins to 0}
  nEscapeCode := GETPRINTINGOFFSET;
  if (Escape(Printer.Canvas.Handle, QUERYESCSUPPORT, sizeof(Integer),
@nEscapeCode, Nil) <> 0) then
  Begin
    if Escape(Printer.Canvas.Handle, GETPRINTINGOFFSET, 0, Nil,
@pMarg) > 0 then
      GetPrinterMargins := pMarg;
  End;
End;

<END 1>

<BEGIN 2>

aa := TPoint(Printer.PageWidth, Printer.PageHeight);
  returns the paper size.

Escape(Printer.Handle, GETPHYSPAGESIZE, 0, nil, @bb);
  returns a TPoint which gives the width and height of the printable
page
area.

Escape(Printer.Handle, GETPRINTINGOFFSET, 0, nil, @cc);
  returns a TPoint which is the amount of unprintable left and top
space.

Getting the exact location on the paper just means a little maths with
these TPoints. :)

<END 2>

--
                   \|||/
                   /'^'\
                  ( 0 0 )
--------------oOOO--(_)--OOOo--------------
. Reid Roman                              .
. Delphi Programmer / Analyst             .
. TVisualBasic:=class(None);              .
. May the Source be With You              .
-------------------------------------------
. Auto-By-Tel (http://www.autobytel.com)  .
. Irvine, CA U.S.A                        .
. E-Mail : rkroman (at) pacbell (dot) net .
. or reidr (at) autobytel (dot) com       .
-------------------------------------------

Other Threads