# Print Text with printer

Hi.. how can I print the text in a TMemo ?

I've tried with:

Pages:= Memo1.Lines.Text; // Pages is a string var.
       Printer.BeginDoc;
       with Printer.Canvas do
         begin
           Font.Name := 'Arial';
           Font.Pitch := fpDefault;
           Font.Style := [fsItalic, fsBold];
           Font.Size := 15;
           Font.Color := clBlack;
//           TextExtent(Pages);
 // I'VE TRIED BOTH METHODS //
//           TextOut(10, 10, Pages);
         end;
          Printer.EndDoc;
---------------------------------------

and it just doesn't print anything..
LVL 3
bryan7Asked:
Who is Participating?
 
kretzschmarConnect With a Mentor Commented:
well bryan,

glad to help you
good luck again

meikl
0
 
intheCommented:
try using pages:=memo1.text;
instead of memo1.lines.text
then use the  textout(10,10,pages)...
0
 
bryan7Author Commented:
didn't work..
it only prints a line on the top..

try it:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,Printers,
  StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var Pages: string;
begin
Pages:= Memo1.Text;
Printer.BeginDoc;
with Printer.Canvas do
begin
Font.Name := 'Arial';
Font.Pitch := fpDefault;
Font.Style := [fsItalic, fsBold];
Font.Size := 15;
Font.Color := clBlack;
TextOut(10,10,Pages);
end;
Printer.EndDoc;
end;

end.

0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
edeyCommented:
For printing Memos I have always used one of two attacks:

1) If I'm really lazy - just create a TRichEdit, assign the text property and call TRichEdit.print;

2) use AssignPrn - from the d3 help file

procedure TForm1.Button1Click(Sender: TObject);

var
  MyFile: TextFile;
begin
  AssignPrn(MyFile);
  Rewrite(MyFile);
  Writeln(MyFile, 'Print this text');
  System.CloseFile(MyFile);
end;

just remember to write your TMemo.lines to MyFile.

Good Luck!!!
0
 
kretzschmarCommented:
hi bryan,

use
 TextOut(100, 100, Pages);
instead
 TextOut(10, 10, Pages);

because the coordinates 10,10 could be on the non printable area of the printdevice. also you should print line by line of your memo-content

meikl
0
 
IndefreiCommented:
>> you should print line by line of your memo-content <<
that`s true,
but what about the correct linespacing depending on the used font ?
Indi

0
 
kretzschmarCommented:
hi inidi,

use for the vericalposition
(lineNr*canvas.Textheight(lines[lineNr])) + ConstOffset

meikl
0
 
IndefreiCommented:
Thanks Meikl,
Got no time now
check it out tonight with this

procedure TVorschau1.PrintButtonClick( Sender: TObject );
{==============================================================================}
// const crlf:String = chr(13)+chr(10); // CarridgeReturn LineFeed
var
  Rect: TRect;
  x,y,z,FontSize1: Integer;
  Fonthoch : Integer;
  Line : Integer;

begin
//     AbortDlg.Show;
// Compute the rectangle for the printer
  v1:=Printer.PageWidth/Panel2.width;
  Z1:=Printer.PageHeight/Panel2.Height;
 
  Printer.BeginDoc;

BTW : Is this BUG fixed in Delphi 4 ???????
{//  Handle Delphi 3 Bug
  FontSize1 := Printer.Canvas.Font.Size;
  Printer.Canvas.Font.PixelsPerInch := GetDeviceCaps (Printer.Handle, LOGPIXELSX );
  Printer.Canvas.Font.PixelsPerInch := GetDeviceCaps (Printer.Handle, LOGPIXELSY );

  Printer.Canvas.StretchDraw(Rect, Image2.Picture.Graphic);



//  If textcheck.State=cbChecked then}
//  Now something about image2.height and other

  y:=y+Round((Label3.Height)*z1);
  x:=Rect.Left;
  Printer.Canvas.Font := Memo1.Font;

//        Fonthoch is Fontheight (JUST Fantasy , doesn`t work correctly)
//     there must be some rules ????  
Fonthoch := round(2*(Memo1.Font.Size * Memo1.Font.PixelsPerInch /72 ));
 
    for Line := 0 to Memo1.Lines.Count - 1 do
    begin
    Printer.Canvas.TextOut(x,y, Memo1.Lines[Line]);
    newline;
//    If Printer.Orientation = poLandscape then
 
>>    (lineNr*canvas.Textheight(lines[lineNr])) + ConstOffset   <<   
??? so here somehow ???
    y:=y+Round(Fonthoch +(Label3.Height)*v1);

    end; //For
//  end; // If Textcheck
  Printer.EndDoc;
  Printer.Canvas.Font.Size := FontSize1;

//  AbortDlg.Hide;
end;
1 point for you, if it works, meikl
cheers
Indi
0
 
kretzschmarCommented:
hi indi,

why you are doing this ?
 FontSize1 := Printer.Canvas.Font.Size;
      Printer.Canvas.Font.PixelsPerInch := GetDeviceCaps (Printer.Handle, LOGPIXELSX );
      Printer.Canvas.Font.PixelsPerInch := GetDeviceCaps (Printer.Handle, LOGPIXELSY );

why not
  Printer.Canvas.Font.Assign(memo1.Font);

for the memo-print
       
        Y := 200; //Some StartingPoint
        for Line := 0 to Memo1.Lines.Count - 1 do
        begin
          //inc y each line+ some linespace offset, 4 pixel, could also be a formula
          Y := Y + Printer.Canvas.TextHeight(memo1.Lines[line]) + 4;
          Printer.Canvas.TextOut(x,y, Memo1.Lines[Line]);
        end;

good luck

meikl
0
 
bryan7Author Commented:
Hi.. edey's solution worked fine.. with this:

RichEdit1.Text:= Memo1.Text;
RichEdit1.Print('    Example');

but the 'example' wasn't printed..  =?
0
 
bryan7Author Commented:
( 'example' is the tittle only.. )
0
 
bryan7Author Commented:
hmm.,, but, if I want to have a picture on top and then the text below it ?
can I do Printer.canvas.TextExtent(RichEdit1.Text); ?
0
 
IndefreiCommented:
>>why you are doing this ? <<
>>BTW : Is this BUG fixed in Delphi 4 ???????
   {//  Handle Delphi 3 Bug
     FontSize1 := Printer.Canvas.Font.Size;
     Printer.Canvas.Font.PixelsPerInch := GetDeviceCaps (Printer.Handle, LOGPIXELSX );
     Printer.Canvas.Font.PixelsPerInch := GetDeviceCaps (Printer.Handle, LOGPIXELSY ); <<
as i said before
I use Delphi 3.0 Prof
and if you try to print an image/bitmap into printercanvas on a HP printer e.g.
you get nothing without setting this LOGPIXELsY for the pictureprint and then assign back to Textfonts
this bug comes only with pictures, not text (perhaps only in germany)
therefor my question to D4.
Just came in, check it out now
cheers
Indi
0
 
IndefreiCommented:
Heh Raymond
works great
thanks a lot it really helped me out
cheers
Indi

Brian7
you should give the points to Rwilson, cause it solves also your Bitmap problem
richedit can`t or you try richeditEX or richedit98 component
regards
Indi
0
 
IndefreiCommented:
Oh Fu....
of course I meant Meikl
Post the points to kretschmar, Brian7
Sorry I`m handling another question right now with Raymond Rwilson
Sorry Meikl
Oh this damm heat
Indi
0
 
kretzschmarCommented:
hi indi,

well, so i have learned something too, didn't thought there is a bug
>this bug comes only with pictures, not text (perhaps only in germany)
you are from germany?

meikl


0
 
IndefreiCommented:
Sure
Didn`t you hear the accent, when i pronounced your name, meikl ? :o:
Indi
0
 
bryan7Author Commented:
well, this is what I need:

2 things:

1: Print text.. only text.. which is contained in a TMemo and may be a few pages long..

2: Print a bitmap which is 405x246 , and a short text ( from 3 to 10 lines ) above it..

0
 
bryan7Author Commented:
"" text ( from 3 to 10 lines ) above it.. "" <-- meant BELOW
0
 
bryan7Author Commented:
meikl.. using your method I can't print a long text which is a few pages long..
0
 
bryan7Author Commented:
I'm using the RichEdit method.. but, how would I print the text centered this way ?


procedure PrintPages(Pages: string; Tittle: string);
begin
if Printer.Printing then
     Application.MessageBox('Printer is busy', 'Print Error', 0)
   else
 begin

with RichEdit1 do
     begin
       Font.Name := 'Arial';
       Font.Pitch := fpDefault;
       Font.Style := [fsItalic, fsBold];
       Font.Size := 15;
       Font.Color := clBlack;

       Text:= Tittle+chr(10)+chr(13)+chr(10)+chr(13)+Pages;
       Print(Tittle);
     end;
 end;
end;

0
 
kretzschmarCommented:
>meikl.. using your method I can't print a long text which is a few pages long..

well bryan,

its just a kickoff
you must y compare with Pagewidth-BottomMargin

if y > Pagewidth-BottomMargin then
begin
   Newpage;
   Y := StartValueOnThisNewPage;
end;

bryan use your brain ;-)

meikl
0
 
bryan7Author Commented:
well, em.. give me a reason why not to use RichEdit's method ..    is the other way better ? why ?

what about this :  "'m using the RichEdit method.. but, how would I print the text centered this way ? ""

( from last comment )
0
 
kretzschmarCommented:
hmm bryan,

by the richedit-method you are not so free to format during the print. the only thing you can set, as far as i know, is the property pagerect before the print.

meikl
0
 
bryan7Author Commented:
pagerect.. what is it for ?  

I think I should do something like

for a:= 1 to RichEdit1.Lines.Count do
 RichEdit2.Lines.Add( '  ' * LineSpaces/2 + RichEdit.Lines[a]+ ' ' * LineSpaces * 2)

then print RichEdit2.

how would I do that ?
I'll use both urs and edey's methods..

I'll use edey's when I only need to print text, and I'll use urs when I need some text below an image.

0
 
IndefreiCommented:
Heh Brian, like to see your resulting print programs
what about increasing your points, 15 to M,15 to E
this thread is becoming bigger n bigger
Indi
0
 
bryan7Author Commented:
I'll give 25 to each other..of course with an A grade
0
 
bryan7Author Commented:
..
0
 
bryan7Author Commented:
how do I center the text ?
0
 
kretzschmarCommented:
by which method ?
0
 
IndefreiCommented:
Hi , Meikl , you lazybones :o:

try to help you out with one method

>>you must y compare with Pagewidth-BottomMargin <<

>>  if y > Pagewidth-BottomMargin then
      begin
         Newpage;
         Y := StartValueOnThisNewPage;
      end; <<

of course Meikl meant  PageHeight-BottomMargin

Brian
there is another coordinate too,

set X for centering

x := (Printer.pageWidth - Memo1.width) / 2 ;
cheers
Indi
0
 
bryan7Author Commented:
RichEdit's ..

I can have 2 RichEdits and pass the 1st to the 2nd one, but adding some spaces to each line so the text is centered.. how ?
0
 
bryan7Author Commented:
oups.. I had this window opened for about 1.2h so I didn't see Indefreis's comment..
0
 
IndefreiCommented:
o o maybe its not as easy
didn`t think about the relationship between
screen resolution and printerpixels
sorry in case it doesn`t work
Indi
0
 
bryan7Author Commented:
Adjusted points to 35
0
 
bryan7Author Commented:
.
0
 
kretzschmarCommented:
hi bryan,

try this out

procedure TForm1.Button1Click(Sender: TObject);
begin
  Richedit1.SelectAll;      //Mark Whole Text
  Richedit1.Paragraph.Alignment := taCenter; //center whole text
  Richedit1.Print('My PrintOut');   //Print text
end;

>of course Meikl meant  PageHeight-BottomMargin
of course

hi indi,

did you get my sample about the dwavemix?
had sent it yesterday morning

meikl
0
 
IndefreiCommented:
Sure Meikl,
See Q.10190794
Very surprised it came so quickly
Thanx very much
Indi
0
 
bryan7Author Commented:
hmm.. I also have that component..  ;) well, I have lots of added components.. about 250.. all categorized and packaged ( panels, sound, graphic comps, system..

well, em, I'll check it now meikl,. thanks =)

bryan
0
 
bryan7Author Commented:
yeah ! thanx meikl, that works perfectly !
I'm using this now:

procedure PrintPages(Pages: string; Tittle: string);
begin
if Printer.Printing then
     Application.MessageBox('Printer is busy', 'Print Error', 0)
   else
 begin
  with Form1.RichEdit1 do
     begin
       Font.Name := 'Arial';
       Font.Pitch := fpDefault;
       Font.Style := [fsItalic, fsBold];
       Font.Size := 15;
       Font.Color := clBlack;

       Text:= 'Alaior: '+Tittle+chr(10)+chr(13)+chr(10)+chr(13)+Pages;

       SelectAll;      //Mark Whole Text
       Paragraph.Alignment := taCenter; //center whole text
       Print(Tittle);
     end;
 end;
end;

thanx =)
0
 
bryan7Author Commented:
hmm.. why is the RichEdit still visible if I had set its property to Visible:= False ?
I added RichEdit.Visible:= False; in the form's OnCreate event even..

0
 
kretzschmarCommented:
hi bryan,

move this line
RichEdit.Visible:= False;
from the onCreate-event of your from
to the onShow-Event of your form

meikl
0
 
kretzschmarCommented:
this comment is just for my notifying checkmark,
i've lost all checkmarks during the mail-server of my provider was 24 hours down.
0
 
bryan7Author Commented:
done that.. doesn't work..

anyway.. I'm setting the width and hight to 0 in the create event.. so it's ok..

you can post the answer meikl..

and I'll post a separate q. for edey to get some points also.


bryan
0
 
bryan7Author Commented:
edey..  u there ?
0
All Courses

From novice to tech pro — start learning today.