HOME - - - - - Lazarus Tutorials TOC - - - - - - Other material for programmers

Yet Another Tutorial on producing hardcopy
Just the "heart" of the answer

How to print 3 pages of text- the core of that Lazarus code

Page URL: lt3Nhardc-multpg-intro.htm

This page is "browser friendly". Make your browser window as wide as you want it. The text will flow nicely for you. It is easier to read in a narrow window. With most browsers, pressing plus, minus or zero while the control key (ctrl) is held down will change the texts size. (Enlarge, reduce, restore to default, respectively.) (This is more fully explained, and there's another tip, at my Power Browsing page.)

What this page has for you

I'm going to try to sketch the CORE of the Lazarus code needed to print out 3 pages of ink-on-paper.

Obviously, this would need a some "fleshing out", but I hope it will get you started. With what's on this page, you should get three pages of hardcopy for one button click. (Well, two, if you count the "OK" on the printer dialog.)

The "application" is going to have one button.

"Behind the scenes", unseen by the user, there will also be a printer dialog on the form.

When you press that, three pages will be printed, with a line of text on each.

What does it take?

What I know mostly came from the "official" page on producing hardcopy with Lazarus. (An more extensive discussion by me is available, too.)

You need to install the "official" printer support package, Printer4Lazarus. (Details at the pages cited a moment ago. As are details of many of the things I will refer to in this.

Any application using that must have "Printers" added to its "uses" clause.

In theory, the heart of all you then need is...

Printer.DocBegin;
  Printer.Canvas.TextOut(10,20,'First page');
Printer.DocEnd;

Printer.DocBegin;
  Printer.Canvas.TextOut(10,20,'Second page');
Printer.DocEnd;

Printer.DocBegin;
  Printer.Canvas.TextOut(10,20,'Third page');
Printer.DocEnd;

A fine theory.

First "necessary frill".

Do it that way, and you are presuming that the font will be what you want, by default. So much better to establish a font on purpose, don't you think?

Printer.Canvas.Font.Name:="Courier New";//should be a font on your system
Printer.Canvas.Font.Color:=clBlack;//"clBlack" is pre-defined in Lazarus
Printer.Canvas.Font.Size:=12;//units: "Points"

I THINK that if you do this once, just before the first TextOut, that it will carry over into the next BeginDoc/EndDoc block. It might be "better", on several grounds, to do it after each BeginDoc. (You'd make a parameter-less subroutine ("DoItAgain"?), and provide other ways to keep that informed as to your wants.)

Tedious "frill"

It isn't supposed to be necessary, but I have found that at run time, the "simple" answer above often triggers a "SIGSEGV" error... whatever that may be! that may not happen on your present system... but what about next week, when Microsoft has changed your operating system again? And do you want to write something only for your present system? Will you never upgrade?

So.. the following takes us away from the "simple" demo this was supposed to be, but probably necessarily.

To print a single page:

  if PrintDialog1.execute then begin
  with Printer do
  try
    BeginDoc;
    Canvas.Font.Name := 'Courier New';
    Canvas.Font.Size := 12;
    Canvas.Font.Color := clBlack;
    Canvas.TextOut(10,20,'First page');

  finally
    EndDoc;
 

The good news is...

This gives the user a chance to abort a print job for those times when "Print it" was invoked by mistake. And...

It seems that one PrintDialog1.execute is enough. It is early days... I'm only the one-eyed king, trying to be helpful... it SEEMS that the following "works". (The only difference between this and the previous is the material in blue.)

procedure TForm1.Button1Click(Sender: TObject);
begin
    if PrintDialog1.execute then begin
  with Printer do
  try
    BeginDoc;
      Canvas.Font.Name := 'Courier New';
      Canvas.Font.Size := 12;
      Canvas.Font.Color := clBlack;
      Canvas.TextOut(10,20,'First page');
    EndDoc;
    BeginDoc;
      Canvas.TextOut(10,20,'Second page');
    EndDoc;
    BeginDoc;
      Canvas.TextOut(10,20,'Third page');
    //NO EndDoc; here... there's one to come in the "finally" clause.
finally
    EndDoc;
  end;//of try/finally
 end;//of if... execute.. then...
end;//Button1Click

So! That's it! So why have I been ten days working on something not a lot more complex? Sigh.

For further discourses about printing, see my more detailed page about generating hardcopy. (This is the one I mentioned before.) There are more "onward" links there, too.





Search across all my sites with the Google search...

Custom Search


To search THIS site.... (Go to my other sites, below, and use their search buttons if you want to search them.)

index sitemap advanced
search engine by freefind

Site Map    What's New    Search

The search engine merely looks for the words you type, so....
*    Spell them properly.
*    Don't bother with "How do I get rich?" That will merely return pages with "how", "do", "I"....

Please also note that I have two other sites, and that this search will not include them. They have their own search buttons.

My SheepdogSoftware.co.uk site.

My site at Arunet.


Ad from page's editor: Yes.. I do enjoy compiling these things for you. I hope they are helpful. However... this doesn't pay my bills!!! Sheepdog Software is supposed to help do that, so if you found this stuff useful, (and you run a Windows or MS-DOS PC) please visit my freeware and shareware page, download something, and circulate it for me? Links on your page to this page would also be appreciated!
Click here to visit editor's freeware, shareware page.

Link to Lazarus Tutorials main page

To email this page's editor, Tom Boyd.... Editor's email address. Suggestions welcomed! Please cite "lt3Nhardc-multpg-intro.htm".




Valid HTML 4.01 Transitional Page has been tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org. Mostly passes.

AND passes... Valid CSS!

If this page causes a script to run, why? Because of things like Google panels, and the code for the search button. Why do I mention scripts? Be sure you know all you need to about spyware.

....... P a g e . . . E n d s .....