• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 233
  • Last Modified:

Printing problems from memo field

I have problems changing the font on the printer.
Here is my code:
procedure TForm1.BitBtn6Click(Sender: TObject);
var
  i: integer;
  PText: TextFile;
begin
  inherited;
  printer.Orientation := poLandscape;
  if PrintDialog1.Execute then begin
     AssignPrn(PText);
     Rewrite(PText);
{     printer.Canvas.Font := Memo1.font;}
     printer.Canvas.Font.name := 'courier';
     printer.Canvas.Font.Size := 9;
     try
       for i:= 0 to Memo1.Lines.Count -1 do
           writeln(PText, Memo1.Lines[i]);
       finally
         CloseFile(PText);
       end;
    end;
    Printer.CleanupInstance;
end;

This program works in my department with the LaserJet 4si.
But in another department they have a laserjet 6 i believe.
One of the newer ones.  
I need to use courier font.  When I tried printing on
the other department, the font was not courier and it was
a proportional type font and the report was not printing out
correct; the colums was not aligned properly.

HELP....

thanks
0
waipahu
Asked:
waipahu
1 Solution
 
rwilson032697Commented:
Listening
0
 
intheCommented:
Hi
maybe these will help

{+------------------------------------------------------------
| 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 }


another method:
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;


 
(another way is with a hidden richedit instead and calling richedit1.print; )
0
 
yk030299Commented:
listen
0
Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

 
waipahuAuthor Commented:
The HP laserjet 6 has a font named 'Courier New (Western)
Is this the way I select that font????

     printer.Canvas.Font.name := 'Courier New (Western)';
     printer.Canvas.Font.Size := 8;


0
 
waipahuAuthor Commented:
Thanks
I figured selected the wrong font name.
'Courier New' was the correct way.

thanks so much

Mahalo (thank you in hawaiian)

darin
0
 
intheCommented:
i would say yes but you can test it to make sure by using a tfontdialog(on dialogs tab):

procedure TForm1.Button1Click(Sender: TObject);
begin
fontdialog1.Execute;
showmessage(fontdialog1.Font.Name);
end;

this filename string is what you pass to the printer object.
0
 
intheCommented:
oh thanks
you can also check the fontnames in the object inspector if your unsure of any just drop a tlabel on a form and check the font stuff then.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now