Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Printing Memo

Posted on 2000-03-03
Medium Priority
Last Modified: 2010-04-04
How to write a simple code to print a memo? Does D4 provides any print methods for TMemo component? Any help is greatly appreciated.
Question by:zulhilmi
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2

Expert Comment

ID: 2580679
have a look at the TQRMemo / TQRDBText component of QR.


Expert Comment

ID: 2580694
Why not just save the memo to a file
(member function--memo1.lines.Savetofile(filename)) and print the file.

Of course, if you use a rich edit component, you can print the contents
using one of its member functions
LVL 17

Accepted Solution

inthe earned 40 total points
ID: 2581327
here are 2 methods (first is quite long)

first method:

| Procedure PrintMemo
| Parameters:
|  aMemo: memo to print
|  centerVertical: true if output should be centered vertically
|  centerHorizontal: true if output should be centered
| 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;
    { 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
      Note that we have to make sure the print job is closed
      thus the try finally block. }
      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 }
          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
            y:= topMargin;
        End; { If }
      End; { For }
  End; { PrintMemo }

second method:
add to uses "printers"

procedure TForm1.Button1Click(Sender: TObject);
I: Integer;
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]);

Regards Barry

LVL 17

Expert Comment

ID: 2634839

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

661 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question