Printing the contents of a memo

I want to print the contents of a memo. So far I have set up a second invisible memo box with the same width as the printing area and then used the 'lines' to print. However, there must be a more elegant way.

Do you have any ideas?
michaelhigginsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

intheCommented:
hi,
two methods to try
1:

{+------------------------------------------------------------
| Procedure PrintMemo
|
| Parameters:
|  aMemo: memo to print
|  centerVertical: true if output should be centered vertically
|  centerHorizontal: true if output should be centered
horizontally
| Call method:
|  static
| Description:
|  Prints the contents of a memo on the currently selected
|  printer, using the memos Font. The output can be centered
|  vertically and/or horizontally. Default margins will be used.
|  If the text does not fit onto one page the centerVertical
|  parameter is ignored.
| Error Conditions:
|  none
| Note:
|  requires Printers in the Uses clause.
|Created: 27.10.97 13:16:46 by P. Below
+------------------------------------------------------------}
Procedure PrintMemo( aMemo: TMemo;
                    centerVertical, centerHorizontal: Boolean );   Var
    topMargin, bottomMargin, leftMargin, rightMargin: Integer;
    x,y: Integer;
    maxLineLength: Integer;
    linecounter: Integer;
    lineheight : Integer;
  Begin
    { Set the default margins to 1 inch for top, bottom, 0.75 inch       for left and right margins. GetDeviceCaps is used to get the       printers resolution (dots per inch). The margin variables
      contain the position of the margin on paper, in dots, not the       distance from the border of the printing region! }
    topMargin := GetDeviceCaps( Printer.Handle, LOGPIXELSY );
    bottomMargin := Printer.PageHeight - topMargin;
 
    leftMargin := GetDeviceCaps( Printer.handle, LOGPIXELSX ) * 3 div 4;
    rightMargin := Printer.PageWidth - leftMargin;
 
    { Start the print job, assign the memo font to the printer
canvas.
      Note that we have to make sure the print job is closed
properly,
      thus the try finally block. }
    Printer.BeginDoc;
    try
      Printer.Canvas.Font.PixelsPerInch :=
        GetDeviceCaps( Printer.Canvas.Handle, LOGPIXELSY );
        { This is really only necessary for Delphi 1, due to a
          buglet. }
      Printer.Canvas.Font:= aMemo.Font;
 
      { Determine the height of a line in pixels }
      lineHeight := Printer.Canvas.TextHeight('Ay');
 
      If centerHorizontal Then Begin
        { Iterate once over all lines of the memo and determine the           length, in pixels, of the longest line. We need that to           adjust the leftMargin to center the text. }
        maxLineLength := 0;
        For linecounter := 0  To aMemo.Lines.Count-1 Do Begin
          x:= Printer.Canvas.TextWidth( aMemo.Lines[linecounter]);           If x > maxLineLength Then
            maxLineLength := x;
        End; { For }
 
        { Adjust the leftMargin to center the text into the
          available space between the current margins. }
        x := leftMargin +
            (rightMargin-leftMargin-maxLineLength) div 2;
        If x < leftMargin Then Begin
          { Problem, longest line does not fit into the available             space! We leave the margin untouched, the line will
            be truncated. }
        End { If }
        Else
          leftMargin := x;
      End; { If }
 
      If centerVertical Then Begin
        { Calculate the vertical space needed to print all lines,           adjust the topMargin to center the text as needed. }
        y := lineHeight * aMemo.Lines.Count;
        If y < (bottomMargin - topMargin) Then Begin
          topMargin :=
            topMargin + (bottomMargin - topMargin - y) div 2;
        End; { If }
        { Else
            space needed is larger than the height of a page,
            so ignore the center request. }
      End; { If }
 
      { Margins have been calculated, so we are finally set up
        to start printing lines. In the following code x and y
        hold the starting position of the next line. }
      x:= leftMargin;
      y:= topMargin;
      For linecounter := 0  To aMemo.Lines.Count-1 Do Begin
        Printer.Canvas.TextOut( x, y, aMemo.Lines[linecounter]);         y := y + lineHeight;
        If y >= bottomMargin Then Begin
          { Eject the page and start a new one, but only if
            the line we have just printed is not the last line
            anyway. EndDoc will eject the current page. }
          If linecounter < (aMemo.Lines.Count-1) Then Begin
            Printer.NewPage;
            y:= topMargin;
          End;
        End; { If }
      End; { For }
    finally
      Printer.EndDoc;
    end;
  End; { PrintMemo }



2:
uses printers;

procedure TForm1.Button1Click(Sender: TObject);
var
  I: Integer;
begin
  Printer.BeginDoc;
  Printer.Canvas.Font.Style:= [fsBold, fsItalic];
  Printer.Canvas.Font.Size:= 10;
  for I := 0 to Memo1.Lines.Count -1 do
    Printer.Canvas.TextOut(10, I * 100, Memo1.Lines[I]);
  Printer.EndDoc;
end;


Regards Barry
ps.
(another way is similar to what your using with a hidden richedit instead and calling richedit1.print; )
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
intheCommented:
hello ..
0
intheCommented:
does it help ????
0
michaelhigginsAuthor Commented:
Thanks for the help--  it works fine. And I have learnt a bit more about how to print in Delphi
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.