Mirror

How to control the print margins when printing a TRichEdit (Views: 100)


Problem/Question/Abstract:

How do I set the left margin when printing the contents of a TRichEdit?

Answer:

You can set TRichEdit.PageRect for the margins. Get the printer information with GetDeviceCaps.

procedure TForm1.Button1Click(Sender: TObject);
var
  PixelsX, PixelsY: integer;
  LeftSpace, TopSpace: integer;
  R: TRect;
begin
  if PrintDialog1.Execute then
  begin
    {get pixels per inch}
    PixelsX := GetDeviceCaps(Printer.Handle, LOGPIXELSX);
    PixelsY := GetDeviceCaps(Printer.Handle, LOGPIXELSY);
    {get non-printable margins}
    LeftSpace := GetDeviceCaps(Printer.Handle, PHYSICALOFFSETX);
    TopSpace := GetDeviceCaps(Printer.Handle, PHYSICALOFFSETY);
    {1' margin all around}
    R.Left := Round(PixelsX * 1) - LeftSpace;
    R.Right := Round(PixelsX * 7.5) - LeftSpace;
    R.Top := Round(PixelsY * 1) - TopSpace;
    R.Bottom := Round(PixelsY * 10) - TopSpace;
    RichEdit1.PageRect := R;
    RichEdit1.Print('Test');
  end;
end;

<< Back to main page