?
Solved

# Print Text with printer

Posted on 1999-08-02
45
Medium Priority
?
276 Views
Last Modified: 2010-04-04
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..
0
Comment
Question by:bryan7
[X]
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
  • 22
  • 11
  • 10
  • +2
45 Comments
 
LVL 17

Expert Comment

by:inthe
ID: 1392480
try using pages:=memo1.text;
instead of memo1.lines.text
then use the  textout(10,10,pages)...
0
 
LVL 3

Author Comment

by:bryan7
ID: 1392481
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
 
LVL 6

Expert Comment

by:edey
ID: 1392482
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 27

Expert Comment

by:kretzschmar
ID: 1392483
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
 
LVL 1

Expert Comment

by:Indefrei
ID: 1392484
>> 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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1392485
hi inidi,

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

meikl
0
 
LVL 1

Expert Comment

by:Indefrei
ID: 1392486
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1392487
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
 
LVL 3

Author Comment

by:bryan7
ID: 1392488
Hi.. edey's solution worked fine.. with this:

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

but the 'example' wasn't printed..  =?
0
 
LVL 3

Author Comment

by:bryan7
ID: 1392489
( 'example' is the tittle only.. )
0
 
LVL 3

Author Comment

by:bryan7
ID: 1392490
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
 
LVL 1

Expert Comment

by:Indefrei
ID: 1392491
>>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
 
LVL 1

Expert Comment

by:Indefrei
ID: 1392492
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
 
LVL 1

Expert Comment

by:Indefrei
ID: 1392493
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1392494
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
 
LVL 1

Expert Comment

by:Indefrei
ID: 1392495
Sure
Didn`t you hear the accent, when i pronounced your name, meikl ? :o:
Indi
0
 
LVL 3

Author Comment

by:bryan7
ID: 1392496
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
 
LVL 3

Author Comment

by:bryan7
ID: 1392497
"" text ( from 3 to 10 lines ) above it.. "" <-- meant BELOW
0
 
LVL 3

Author Comment

by:bryan7
ID: 1392498
meikl.. using your method I can't print a long text which is a few pages long..
0
 
LVL 3

Author Comment

by:bryan7
ID: 1392499
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1392500
>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
 
LVL 3

Author Comment

by:bryan7
ID: 1392501
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1392502
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
 
LVL 3

Author Comment

by:bryan7
ID: 1392503
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
 
LVL 1

Expert Comment

by:Indefrei
ID: 1392504
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
 
LVL 3

Author Comment

by:bryan7
ID: 1392505
I'll give 25 to each other..of course with an A grade
0
 
LVL 3

Author Comment

by:bryan7
ID: 1392506
..
0
 
LVL 3

Author Comment

by:bryan7
ID: 1392507
how do I center the text ?
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1392508
by which method ?
0
 
LVL 1

Expert Comment

by:Indefrei
ID: 1392509
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
 
LVL 3

Author Comment

by:bryan7
ID: 1392510
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
 
LVL 3

Author Comment

by:bryan7
ID: 1392511
oups.. I had this window opened for about 1.2h so I didn't see Indefreis's comment..
0
 
LVL 1

Expert Comment

by:Indefrei
ID: 1392512
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
 
LVL 3

Author Comment

by:bryan7
ID: 1392513
Adjusted points to 35
0
 
LVL 3

Author Comment

by:bryan7
ID: 1392514
.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1392515
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
 
LVL 1

Expert Comment

by:Indefrei
ID: 1392516
Sure Meikl,
See Q.10190794
Very surprised it came so quickly
Thanx very much
Indi
0
 
LVL 3

Author Comment

by:bryan7
ID: 1392517
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
 
LVL 3

Author Comment

by:bryan7
ID: 1392518
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
 
LVL 3

Author Comment

by:bryan7
ID: 1392519
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1392520
hi bryan,

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

meikl
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1392521
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
 
LVL 3

Author Comment

by:bryan7
ID: 1392522
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
 
LVL 27

Accepted Solution

by:
kretzschmar earned 120 total points
ID: 1392523
well bryan,

glad to help you
good luck again

meikl
0
 
LVL 3

Author Comment

by:bryan7
ID: 1392524
edey..  u there ?
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

718 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