1. Home
  2. Computing & Technology
  3. Delphi Programming
RTL reference|Glossary|Tips/Tricks|FREE App/VCL|Best'O'Net|Books|Link To
 
Quick Reports Tutorial
Page 9: Printing without using Quick Report components; Setting Trays and other printer features
 More of this Feature
• Page 1: Reports Overview
• Page 2: What's in a report
• Page 3: History of QR
• Page 4: Band reports
• Page 5: Calculated fields
• Page 6: Sorting & Grouping
• Page 7: Label Reports
• Page 8: QR components
 Join the Discussion
"Post your questions, concerns, views and comments to this article..."
Discuss!
 Related Resources
• Reporting Tools
• Printing with TPrinter
• Controling MS Office

   Printing without using Quick Report components
Sometimes you have a printing requirement that QR can't handle. In those circumstances you may be forced to use the basic Printer interface that of the Tprinter component. For more information on the Printer component look up TPrinter in the Delphi help. Basically you are writing text to a canvas that is the paper rather than a screen. Therefore you need to set the output to LPT1 or whatever port you want tell your printer to begin printing and then enter line after line of text onto your canvas. When you are finished with a page you can call a new page and continue or tell the printer you are done and shut it down. Here is a framework you can use to start with:

procedure TForm1.Button1Click(Sender: TObject);
 var I : Integer ;
begin
 with Printer do begin
  BeginDoc;
  Canvas.font.size := 12;
  for I:= 0 to 8 do
    Canvas.textout
	  (I*572, I*572,
	   IntToStr(I));
  EndDoc;
 end;
end;

In this example we have a button which is used to start the printing process. You need to add Printers to the uses clause of the form. The Printer is the object you are directing your output to. BeginDoc is used to open the file for printing. You can change the style of the printing by accessing the printer.Canvas. In this example we are changing the Font size to 12. Next we are printing at certain places on a page the numbers 1 to 8 (I) using the canvas.textout. the number 572 is the number of Pixels needed to move approximately 1 inch. It is not a fixed number but can change with the type of printer. Theoretically it is the number of dots per inch (DPI). The first variable in the Canvas.text out is the X coordinates, the second is the Y coordinates and the last is the text itself.

Because you are creating the page and then sending it to the printer using the EndDoc command, you can position your text all over the page in the manner that is most convenient for you. Here is an example:

 Canvas.font.size := 8;
 Canvas.textout
  (170, I + 220, Table1Name.Value);
 Canvas.textout
  (2500, I + 220, Table1Name.Value);

In this case, the same text is placed on two places (170 and 2500 Pixels) on the same line.

Setting Trays and other printer features
Unfortunately although printers come with more and more features, getting to use them programmatically is rather difficult. The key to doing this is to realize that Windows has it's own printer dialog and QR has its own. These two sources of printer info do not talk to each other. Therefore you need to do some overriding of settings to be able to change a default for a specific report. Before you go crazy searching the windows API, try using the QuickRep settings that are there. Specifically there are paper size and Orientation settings which usually work well.

If you need to use a different printer than the default you may have to change the default printer programmatically, run your report and then change it back. Here is some sample code to do this:

procedure SetDefaultPrinter(PrinterName: String);
var
   I: Integer;
   Device : PChar;
   Driver : Pchar;
   Port   : Pchar;
   HdeviceMode: Thandle;
begin
 Printer.PrinterIndex := -1;  
 //Save for use later Defaultprintername
 //is a global variable. 
 DefaultPrinterName :=
   Printer.Printers[Printer.PrinterIndex];
 with Printer.printers do begin
 //Count is a printers property
  for I := 0 to Count-1 do begin
   if PrinterDevice(Objects[I]).Device :=
     PrinterName then begin	
	  Printername := Printer.printers[I];
      Device := PChar(TprinterDevice
	       (objects[I]).Device);
      Driver := Pchar(TprinterDevice
	       (objects[I]).Driver);
      Port   := Pchar(TprinterDevice
	       (objects[I]).Port);
      StrCat(Device, ',');
      StrCat(Device, Driver );
      StrCat(Device, Port );
      WriteProfile('windows', 'device', Device );
      StrCopy( Device, 'windows' );
      SendMesssage(HWND_BROADCAST, WM_WINICHANGE,
	               0, Longint(@Device));
      HDeviceMode := 0;
	end;
   end;
 end;
end;

   Other Resources
Anyone who is trying to get the most out of Quick Reports and/or printing in general should take advantage of these resources:

1. The QR samples found in the Demos subdirectory of Delphi.

2. The manual and other tutorials which are in the Downloads section of QuSoft.com or QuSoft.No web sites.

Don't forget to post your questions, concerns, views and comments to this article on the Delphi Programming Forum.

First page > Report Overview and Philosophy > Page 1, 2, 3, 4, 5, 6, 7, 8, 9

All graphics (if any) in this feature created by Zarko Gajic.

 More Delphi
· Learn another routine every day - RTL Quick Reference.
· Download free source code applications and components.
· Talk about Delphi Programming, real time.
· Link to the Delphi Programming site from your Web pages.
· Tutorials, articles, tech. tips by date: 2001|2000|1999|1998 or by TOPIC.
· NEXT ARTICLE: Your application will be shut down!.
Access violations, invalid page faults, general protection faults; the name changes, but the nature of the problem is always the same. Find the solution to your Delphi AVs.
 Stay informed with all new and interesting things about Delphi (for free).
Subscribe to the Newsletter
Name
Email

 Got some code to share? Got a question? Need some help?
Explore Delphi Programming
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming

©2009 About.com, a part of The New York Times Company.

All rights reserved.