Academic Tutorials



English | French | Portugese | German | Italian
Home Advertise Payments Recommended Websites Interview Questions FAQs
News Source Codes E-Books Downloads Jobs Web Hosting
Chats

Delphi
Introduction to Delphi
Data Types
Programming Logic
Case statements
Delphi-loops
Overview of Subroutine
Delphi Exception handling
Delphi Date Time
Delphi Filest
Delphi Binary files
Delphi Pointers
Delphi Printing text and Graphics

HTML Tutorials
HTML Tutorial
XHTML Tutorial
CSS Tutorial
TCP/IP Tutorial
CSS 1.0
CSS 2.0
HLML
XML Tutorials
XML Tutorial
XSL Tutorial
XSLT Tutorial
DTD Tutorial
Schema Tutorial
XForms Tutorial
XSL-FO Tutorial
XML DOM Tutorial
XLink Tutorial
XQuery Tutorial
XPath Tutorial
XPointer Tutorial
RDF Tutorial
SOAP Tutorial
WSDL Tutorial
RSS Tutorial
WAP Tutorial
Web Services Tutorial
Browser Scripting
JavaScript Tutorial
VBScript Tutorial
DHTML Tutorial
HTML DOM Tutorial
WMLScript Tutorial
E4X Tutorial
Server Scripting
ASP Tutorial
PERL Tutorial
SQL Tutorial
ADO Tutorial
CVS
Python
Apple Script
PL/SQL Tutorial
SQL Server
PHP
.NET (dotnet)
Microsoft.Net
ASP.Net
.Net Mobile
C# : C Sharp
ADO.NET
VB.NET
VC++
Multimedia
SVG Tutorial
Flash Tutorial
Media Tutorial
SMIL Tutorial
Photoshop Tutorial
Gimp Tutorial
Matlab
Gnuplot Programming
GIF Animation Tutorial
Scientific Visualization Tutorial
Graphics
Web Building
Web Browsers
Web Hosting
W3C Tutorial
Web Building
Web Quality
Web Semantic
Web Careers
Weblogic Tutorial
SEO
Web Site Hosting
Domain Name
Java Tutorials
Java Tutorial
JSP Tutorial
Servlets Tutorial
Struts Tutorial
EJB Tutorial
JMS Tutorial
JMX Tutorial
Eclipse
J2ME
JBOSS
Programming Langauges
C Tutorial
C++ Tutorial
Visual Basic Tutorial
Data Structures Using C
Cobol
Assembly Language
Mainframe
Forth Programming
Lisp Programming
Pascal
Delphi
Fortran
OOPs
Data Warehousing
CGI Programming
Emacs Tutorial
Gnome
ILU
Soft Skills
Communication Skills
Time Management
Project Management
Team Work
Leadership Skills
Corporate Communication
Negotiation Skills
Database Tutorials
Oracle
MySQL
Operating System
BSD
Symbian
Unix
Internet
IP-Masquerading
IPC
MIDI
Software Testing
Testing
Firewalls
SAP Module
ERP
ABAP
Business Warehousing
SAP Basis
Material Management
Sales & Distribution
Human Resource
Netweaver
Customer Relationship Management
Production and Planning
Networking Programming
Corba Tutorial
Networking Tutorial
Microsoft Office
Microsoft Word
Microsoft Outlook
Microsoft PowerPoint
Microsoft Publisher
Microsoft Excel
Microsoft Front Page
Microsoft InfoPath
Microsoft Access
Accounting
Financial Accounting
Managerial Accounting
Network Sites


Printing text and graphics


Previoushome






Printing text and graphics


Teleprinter type printing Delphi reveals its heritage from the 1970's and earlier when console and line printers were standard.

A D V E R T I S E M E N T

The AssignPrn command allows you to use the Write and WriteLn commands to write a stream of text to your printer. It is simply that. There is no control of font, of page throws, page numbering and so on. And of course, no graphics.

Full control printingDelphi does provide modern full text and graphics printing. The Delphi print model is very different from that of Java, where Java asks the application for pages it chooses. In Delphi, we interrogate the print dialog, and determine ourselves what pages are to be printed, and in what sequence.
There are in fact two Printer objects, depending on whether you have a CLX or VCL application. The differences are minor.

Using the printer dialogIt is highly recommended that you display the printer dialog so that the user can control printing. In the example program that we will build up during this tutorial, we will show a print dialog that allows the user to select all pages, or a range of pages. We keep things as simple as possible by ignoring the Collate option. Unfortunately, when the user selects multiple copies, we cannot switch off the collate option, so you should code for it in your application.

const
TOTAL_PAGES = 4;// How many pages to print
var
printDialog: TPrintDialog;

begin
// Create a printer selection dialog
printDialog := TPrintDialog.Create(Form1);

// Set up print dialog options
printDialog.MinPage := 1; // First allowed page number
printDialog.MaxPage := TOTAL_PAGES; // Highest allowed page number
printDialog.ToPage:= TOTAL_PAGES; // 1 to ToPage page range allowed
printDialog.Options := [poPageNums];// Allow page range selection

// if the user has selected a printer (or default), then print!
if printDialog.Execute then
begin
... Your print statements
end;
end;

Here we have created a print dialog object, set up some options, and then displayed it. The Execute method returns true if the user has hit OK rather than cancel. We then print the document. The user can select, via these options, whether to print all 4 pages or a range of these pages, as seen in a portion of the dialog shown below:
printingdialogue

Starting to printThe Printer object is permanently available to your code (you must use the Printers unit to get access to its methods and fields though). With this object, printing proceeds in an orderly fashion.
// Use the Printer function to get access to the global TPrinter object.
// Set to landscape orientation
Printer.Orientation := poLandscape;

// Set the printjob title - as it it appears in the print job manager
Printer.Title := 'Test print for Delphi';

// Set the number of copies to print each page
// This is crude - it doies not take Collation into account
Printer.Copies := printDialog.Copies;

// Start printing
Printer.BeginDoc;

This starts a print job, with a landscape page layout, and a title, and the user specified number of copies. Note that we are ignoring collation - we always print all page 1 copies before page 2 etc.

Responding to the dialog settingsThe following code snippet defines some new variables :
var
page, startPage, endPage : Integer;

And we now set these variables from the print dialog:
// Has the user selected a page range?
if printDialog.PrintRange = prPageNums then
begin
startPage := printDialog.FromPage;
endPage := printDialog.ToPage;
end
else // All pages
begin
startPage := 1;
endPage := TOTAL_PAGES;
end;

// Set up the start page number
page := startPage;

The prPageNums value is a TPrintRange value, and is one of the values that PrintRange may have. If set, it means that the user has selected a range of pages. The FromPage and ToPage values will then be set to the user specified values.

The main printing logicThe following code snippet shows the main part of our printing code:
// Keep printing whilst all OK
while (not Printer.Aborted) and Printer.Printing do
begin
// Show a message saying we are starting a page
ShowMessagePos('Starting to print page '+IntToStr(page),300,300);

// Set up a medium sized font
Printer.Canvas.Font.Size := 10;

// Allow Windows to keep processing messages
Application.ProcessMessages;

// Write out the page number
Printer.Canvas.Font.Color := clBlue;
Printer.Canvas.TextOut(40,20, 'Page number = '+IntToStr(page));

// Underline this page number
Printer.Canvas.MoveTo(40,80);
Printer.Canvas.LineTo(Printer.PageWidth-20,80);

// Write out the page size
Printer.Canvas.Font.Color := clRed;
Printer.Canvas.TextOut(40, 100, 'Pagewidth = '+
IntToStr(Printer.PageWidth));
Printer.Canvas.TextOut(40, 180, 'Page height = '+
IntToStr(Printer.PageHeight));

// Increment the page number
Inc(page);

// Now start a new page - if not the last
if (page <= endPage) and (not Printer.Aborted)
then Printer.NewPage;
end;

// Finish printing
Printer.EndDoc;

The functions highlighted in red illustrate printing graphics and text operations. We have drawn a line across the page after moving the line start point to the appropriate position. And we have written out the page number and size values. Text is also positioned by graphical coordinate.
Both graphical and textual operations are performed on the Printer Canvas. This is very important. It allows you to have a very similar or identical block of code that displays to a form canvas as it does to a printer canvas. This lets you print what you display.
Notice that we handle the page numbering, and page throws (Printer.NewPage) ourselves.
Finally, when all pages have been printed, the last page throw is performed when we close the print job using Printer.EndDoc.

We are not done thereIn practice, you may print documents greater in complexity and size than this simple one. It may be important to you to provide a print cancel mechanism, so that the user can easily abandon unwanted print runs. However, we cannot just display a cancel dialog, since this will hold up our processing. We must display this dialog in a separate threa of execution.
Threading is somewhat beyond the remit of this article, but is included in the final, complete code given below. You can copy and paste this code into Delphi, as long as you follow the instructions at the top of the code:
// Full Unit code.
// -----------------------------------------------------------
// You must store this code in a unit called Unit1 with a form
// called Form1 that has an OnCreate event called FormCreate.

unit Unit1;

interface

uses
Printers, // Unit containing the Printer command
SysUtils, Graphics, Windows,
Forms, Dialogs;

type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
end;

var
Form1: TForm1;

implementation
{$R *.dfm} // Include form definitions

// A subroutine used to display a print-cancel dialog
procedure CancelDialog;
begin
// Display the cancel print dialog
Dialogs.MessageDlg('Press cancel to abort printing',mtCustom,[mbCancel],0);

// Now that the user has pressed cancel, we abort the printing
if Printer.Printing then
begin
Printer.Abort;
ShowMessage('Printing aborted');
end;

// End this thread
endThread(0);
end;

// The main form On Create routine - our main program
procedure TForm1.FormCreate(Sender: TObject);
const
TOTAL_PAGES = 4;// How many pages to print
var
printDialog: TPrintDialog;
cancelThreadId : Integer;
threadId : LongWord;
page, startPage, endPage : Integer;

begin
// Create a printer selection dialog
printDialog := TPrintDialog.Create(Form1);

// Set up print dialog options
printDialog.MinPage := 1; // First allowed page number
printDialog.MaxPage := TOTAL_PAGES; // Highest allowed page number
printDialog.ToPage:= TOTAL_PAGES; // 1 to ToPage page range allowed
printDialog.Options := [poPageNums];// Allow page range selection

// if the user has selected a printer (or default), then print!
if printDialog.Execute then
begin
// Start a cancel print dilaog as a separate thread!
cancelThreadId := beginThread(nil,
0,
Addr(CancelDialog),
nil,
0,
threadId);

// Use the Printer function to get access to the global TPrinter object.
// Set to landscape orientation
Printer.Orientation := poLandscape;

// Set the printjob title - as it it appears in the print job manager
Printer.Title := 'Test print for Delphi';

// Set the number of copies to print each page
// This is crude - it doies not take Collation into account
Printer.Copies := printDialog.Copies;

// Start printing
Printer.BeginDoc;

// Has the user selected a page range?
if printDialog.PrintRange = prPageNums then
begin
startPage := printDialog.FromPage;
endPage := printDialog.ToPage;
end
else // All pages
begin
startPage := 1;
endPage := TOTAL_PAGES;
end;

// Set up the start page number
page := startPage;

// Keep printing whilst all OK
while (not Printer.Aborted) and Printer.Printing do
begin
// Show a message saying we are starting a page
ShowMessagePos('Starting to print page '+IntToStr(page),300,300);

// Set up a medium sized font
Printer.Canvas.Font.Size := 10;

// Allow Windows to keep processing messages
Application.ProcessMessages;

// Write out the page number
Printer.Canvas.Font.Color := clBlue;
Printer.Canvas.TextOut(40,20, 'Page number = '+IntToStr(page));

// Underline this page number
Printer.Canvas.MoveTo(40,80);
Printer.Canvas.LineTo(Printer.PageWidth-20,80);

// Write out the page size
Printer.Canvas.Font.Color := clRed;
Printer.Canvas.TextOut(40, 100, 'Pagewidth = '+
IntToStr(Printer.PageWidth));
Printer.Canvas.TextOut(40, 180, 'Page height = '+
IntToStr(Printer.PageHeight));

// Increment the page number
Inc(page);

// Now start a new page - if not the last
if (page <= endPage) and (not Printer.Aborted)
then Printer.NewPage;
end;

// Finish printing
Printer.EndDoc;
end;
end;

end.


Be the first one to comment on this page.




  Delphi eBooks
More Links » »
 
 Delphi FAQs
More Links » »
 
 Delphi Interview Questions
More Links » »
 
 Delphi Articles

No Delphi Articles could be found as of now.

 
 Delphi News

No News on Delphi could be found as of now.

 
 Delphi Jobs

No Delphi Articles could be found as of now.


Share And Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages.
  • blinkbits
  • BlinkList
  • blogmarks
  • co.mments
  • connotea
  • del.icio.us
  • De.lirio.us
  • digg
  • Fark
  • feedmelinks
  • Furl
  • LinkaGoGo
  • Ma.gnolia
  • NewsVine
  • Netvouz
  • RawSugar
  • Reddit
  • scuttle
  • Shadows
  • Simpy
  • Smarking
  • Spurl
  • TailRank
  • Wists
  • YahooMyWeb

Previoushome

Keywords: delphi pointers,delphi programming,delphi files,delphi datatypes,delphi text,delphi graphics,delphi records,delphi text printing

HTML Quizzes
HTML Quiz
XHTML Quiz
CSS Quiz
TCP/IP Quiz
CSS 1.0 Quiz
CSS 2.0 Quiz
HLML Quiz
XML Quizzes
XML Quiz
XSL Quiz
XSLT Quiz
DTD Quiz
Schema Quiz
XForms Quiz
XSL-FO Quiz
XML DOM Quiz
XLink Quiz
XQuery Quiz
XPath Quiz
XPointer Quiz
RDF Quiz
SOAP Quiz
WSDL Quiz
RSS Quiz
WAP Quiz
Web Services Quiz
Browser Scripting Quizzes
JavaScript Quiz
VBScript Quiz
DHTML Quiz
HTML DOM Quiz
WMLScript Quiz
E4X Quiz
Server Scripting Quizzes
ASP Quiz
PERL Quiz
SQL Quiz
ADO Quiz
CVS Quiz
Python Quiz
Apple Script Quiz
PL/SQL Quiz
SQL Server Quiz
PHP Quiz
.NET (dotnet) Quizzes
Microsoft.Net Quiz
ASP.Net Quiz
.Net Mobile Quiz
C# : C Sharp Quiz
ADO.NET Quiz
VB.NET Quiz
VC++ Quiz
Multimedia Quizzes
SVG Quiz
Flash Quiz
Media Quiz
SMIL Quiz
Photoshop Quiz
Gimp Quiz
Matlab Quiz
Gnuplot Programming Quiz
GIF Animation Quiz
Scientific Visualization Quiz
Graphics Quiz
Web Building Quizzes
Web Browsers Quiz
Web Hosting Quiz
W3C Quiz
Web Building Quiz
Web Quality Quiz
Web Semantic Quiz
Web Careers Quiz
Weblogic Quiz
SEO Quiz
Web Site Hosting Quiz
Domain Name Quiz
Java Quizzes
Java Quiz
JSP Quiz
Servlets Quiz
Struts Quiz
EJB Quiz
JMS Quiz
JMX Quiz
Eclipse Quiz
J2ME Quiz
JBOSS Quiz
Programming Langauges Quizzes
C Quiz
C++ Quiz
Visual Basic Quiz
Data Structures Using C Quiz
Cobol Quiz
Assembly Language Quiz
Mainframe Quiz
Forth Programming Quiz
Lisp Programming Quiz
Pascal Quiz
Delphi Quiz
Fortran Quiz
OOPs Quiz
Data Warehousing Quiz
CGI Programming Quiz
Emacs Quiz
Gnome Quiz
ILU Quiz
Soft Skills Quizzes
Communication Skills Quiz
Time Management Quiz
Project Management Quiz
Team Work Quiz
Leadership Skills Quiz
Corporate Communication Quiz
Negotiation Skills Quiz
Database Quizzes
Oracle Quiz
MySQL Quiz
Operating System Quizzes
BSD Quiz
Symbian Quiz
Unix Quiz
Internet Quiz
IP-Masquerading Quiz
IPC Quiz
MIDI Quiz
Software Testing Quizzes
Testing Quiz
Firewalls Quiz
SAP Module Quizzes
ERP Quiz
ABAP Quiz
Business Warehousing Quiz
SAP Basis Quiz
Material Management Quiz
Sales & Distribution Quiz
Human Resource Quiz
Netweaver Quiz
Customer Relationship Management Quiz
Production and Planning Quiz
Networking Programming Quizzes
Corba Quiz
Networking Quiz
Microsoft Office Quizzes
Microsoft Word Quiz
Microsoft Outlook Quiz
Microsoft PowerPoint Quiz
Microsoft Publisher Quiz
Microsoft Excel Quiz
Microsoft Front Page Quiz
Microsoft InfoPath Quiz
Microsoft Access Quiz
Accounting Quizzes
Financial Accounting Quiz
Managerial Accounting Quiz
Testimonials | Contact Us | Link to Us | Site Map
Copyright ? 2008. Academic Tutorials.com. All rights reserved Privacy Policies | About Us
Our Portals : Academic Tutorials | Best eBooksworld | Beyond Stats | City Details | Interview Questions | Discussions World | Excellent Mobiles | Free Bangalore | Give Me The Code | Gog Logo | Indian Free Ads | Jobs Assist | New Interview Questions | One Stop FAQs | One Stop GATE | One Stop GRE | One Stop IAS | One Stop MBA | One Stop SAP | One Stop Testing | Webhosting in India | Dedicated Server in India | Sirf Dosti | Source Codes World | Tasty Food | Tech Archive | Testing Interview Questions | Tests World | The Galz | Top Masala | Vyom | Vyom eBooks | Vyom International | Vyom Links | Vyoms | Vyom World | Important Websites
Copyright ? 2003-2024 Vyom Technosoft Pvt. Ltd., All Rights Reserved.