Link to home
Start Free TrialLog in
Avatar of bryan7
bryan7Flag for Japan

asked on

# 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..
Avatar of inthe
inthe

try using pages:=memo1.text;
instead of memo1.lines.text
then use the  textout(10,10,pages)...
Avatar of bryan7

ASKER

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.

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!!!
Avatar of kretzschmar
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
>> 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

hi inidi,

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

meikl
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
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
Avatar of bryan7

ASKER

Hi.. edey's solution worked fine.. with this:

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

but the 'example' wasn't printed..  =?
Avatar of bryan7

ASKER

( 'example' is the tittle only.. )
Avatar of bryan7

ASKER

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); ?
>>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
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
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
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


Sure
Didn`t you hear the accent, when i pronounced your name, meikl ? :o:
Indi
Avatar of bryan7

ASKER

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..

Avatar of bryan7

ASKER

"" text ( from 3 to 10 lines ) above it.. "" <-- meant BELOW
Avatar of bryan7

ASKER

meikl.. using your method I can't print a long text which is a few pages long..
Avatar of bryan7

ASKER

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;

>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
Avatar of bryan7

ASKER

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 )
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
Avatar of bryan7

ASKER

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.

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
Avatar of bryan7

ASKER

I'll give 25 to each other..of course with an A grade
Avatar of bryan7

ASKER

..
Avatar of bryan7

ASKER

how do I center the text ?
by which method ?
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
Avatar of bryan7

ASKER

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 ?
Avatar of bryan7

ASKER

oups.. I had this window opened for about 1.2h so I didn't see Indefreis's comment..
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
Avatar of bryan7

ASKER

Adjusted points to 35
Avatar of bryan7

ASKER

.
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
Sure Meikl,
See Q.10190794
Very surprised it came so quickly
Thanx very much
Indi
Avatar of bryan7

ASKER

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
Avatar of bryan7

ASKER

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 =)
Avatar of bryan7

ASKER

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..

hi bryan,

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

meikl
this comment is just for my notifying checkmark,
i've lost all checkmarks during the mail-server of my provider was 24 hours down.
Avatar of bryan7

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of kretzschmar
kretzschmar
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bryan7

ASKER

edey..  u there ?