You are on page 1of 4

04/06/13

Delphi Basics : <a href="http://www.printerinks.com" target=_blank>Printing</a> text and graphic

Printing text and graphic


Teleprinter type printing Delphi reveals its heritage from the 1970's and earlier when console and line printers were standard. The AssignPrn command allows you to use the Write and WriteLncommands 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 printing Delphi 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 C LX or VC L application. The differences are minor.

Using the printer dialog It 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.
c o n s t T O T A L _ P A G E S=4 ; / /H o wm a n yp a g e st op r i n t v a r p r i n t D i a l o g :T P r i n t D i a l o g ; b e g i n / /C r e a t eap r i n t e rs e l e c t i o nd i a l o g p r i n t D i a l o g: =T P r i n t D i a l o g . C r e a t e ( F o r m 1 ) ; / /S e tu pp r i n td i a l o go p t i o n s p r i n t D i a l o g . M i n P a g e: =1 ; p r i n t D i a l o g . M a x P a g e: =T O T A L _ P A G E S ; p r i n t D i a l o g . T o P a g e : =T O T A L _ P A G E S ; p r i n t D i a l o g . O p t i o n s: =[ p o P a g e N u m s ] ; / /F i r s ta l l o w e dp a g en u m b e r / /H i g h e s ta l l o w e dp a g en u m b e r / /1t oT o P a g ep a g er a n g ea l l o w e d / /A l l o wp a g er a n g es e l e c t i o n

/ /i ft h eu s e rh a ss e l e c t e dap r i n t e r( o rd e f a u l t ) ,t h e np r i n t ! i fp r i n t D i a l o g . E x e c u t et h e n b e g i n . . .Y o u rp r i n ts t a t e m e n t s e n d ; e n d ;

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:

Starting to print The 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.
/ /U s et h eP r i n t e rf u n c t i o nt og e ta c c e s st ot h eg l o b a lT P r i n t e ro b j e c t . / /S e tt ol a n d s c a p eo r i e n t a t i o n P r i n t e r . O r i e n t a t i o n: =p o L a n d s c a p e ; / /S e tt h ep r i n t j o bt i t l e-a si ti ta p p e a r si nt h ep r i n tj o bm a n a g e r P r i n t e r . T i t l e: =' T e s tp r i n tf o rD e l p h i ' ; / /S e tt h en u m b e ro fc o p i e st op r i n te a c hp a g e / /T h i si sc r u d e-i td o i e sn o tt a k eC o l l a t i o ni n t oa c c o u n t

www.delphibasics.co.uk/Article.asp?Name=Printing

1/4

04/06/13

Delphi Basics : <a href="http://www.printerinks.com" target=_blank>Printing</a> text and graphic


P r i n t e r . C o p i e s: =p r i n t D i a l o g . C o p i e s ; / /S t a r tp r i n t i n g P r i n t e r . B e g i n D o c ;

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 settings The following code snippet defines some new variables :
v a r p a g e ,s t a r t P a g e ,e n d P a g e:I n t e g e r ;

And we now set these variables from the print dialog:


/ /H a st h eu s e rs e l e c t e dap a g er a n g e ? i fp r i n t D i a l o g . P r i n t R a n g e=p r P a g e N u m st h e n b e g i n s t a r t P a g e: =p r i n t D i a l o g . F r o m P a g e ; e n d P a g e : =p r i n t D i a l o g . T o P a g e ; e n d e l s e/ /A l lp a g e s b e g i n s t a r t P a g e: =1 ; e n d P a g e : =T O T A L _ P A G E S ; e n d ; / /S e tu pt h es t a r tp a g en u m b e r p a g e: =s t a r t P a g e ;

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 logic The following code snippet shows the main part of our printing code:
/ /K e e pp r i n t i n gw h i l s ta l lO K w h i l e( n o tP r i n t e r . A b o r t e d )a n dP r i n t e r . P r i n t i n gd o b e g i n / /S h o wam e s s a g es a y i n gw ea r es t a r t i n gap a g e S h o w M e s s a g e P o s ( ' S t a r t i n gt op r i n tp a g e' + I n t T o S t r ( p a g e ) , 3 0 0 , 3 0 0 ) ; / /S e tu pam e d i u ms i z e df o n t P r i n t e r . C a n v a s . F o n t . S i z e : =1 0 ; / /A l l o wW i n d o w st ok e e pp r o c e s s i n gm e s s a g e s A p p l i c a t i o n . P r o c e s s M e s s a g e s ; / /W r i t eo u tt h ep a g en u m b e r P r i n t e r . C a n v a s . F o n t . C o l o r: =c l B l u e ; P r i n t e r . C a n v a s . T e x t O u t ( 4 0 , 2 0 ,' P a g en u m b e r=' + I n t T o S t r ( p a g e ) ) ; / /U n d e r l i n et h i sp a g en u m b e r P r i n t e r . C a n v a s . M o v e T o ( 4 0 , 8 0 ) ; P r i n t e r . C a n v a s . L i n e T o ( P r i n t e r . P a g e W i d t h 2 0 , 8 0 ) ; / /W r i t eo u tt h ep a g es i z e P r i n t e r . C a n v a s . F o n t . C o l o r: =c l R e d ; P r i n t e r . C a n v a s . T e x t O u t ( 4 0 ,1 0 0 ,' P a g e w i d t h=' + I n t T o S t r ( P r i n t e r . P a g e W i d t h ) ) ; P r i n t e r . C a n v a s . T e x t O u t ( 4 0 ,1 8 0 ,' P a g eh e i g h t=' + I n t T o S t r ( P r i n t e r . P a g e H e i g h t ) ) ; / /I n c r e m e n tt h ep a g en u m b e r I n c ( p a g e ) ; / /N o ws t a r tan e wp a g e-i fn o tt h el a s t i f( p a g e< =e n d P a g e )a n d( n o tP r i n t e r . A b o r t e d ) t h e nP r i n t e r . N e w P a g e ; e n d ; / /F i n i s hp r i n t i n g P r i n t e r . E n d D o c ;

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 C anvas. This is very important. It allows you to have a very similar or identical block of code

www.delphibasics.co.uk/Article.asp?Name=Printing

2/4

04/06/13

Delphi Basics : <a href="http://www.printerinks.com" target=_blank>Printing</a> text and graphic

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 for many purposes such as postcard printing. 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 there In 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:
/ /F u l lU n i tc o d e . / // /Y o um u s ts t o r et h i sc o d ei nau n i tc a l l e dU n i t 1w i t haf o r m / /c a l l e dF o r m 1t h a th a sa nO n C r e a t ee v e n tc a l l e dF o r m C r e a t e . u n i tU n i t 1 ; i n t e r f a c e u s e s P r i n t e r s , / /U n i tc o n t a i n i n gt h eP r i n t e rc o m m a n d S y s U t i l s ,G r a p h i c s ,W i n d o w s , F o r m s ,D i a l o g s ; t y p e T F o r m 1=c l a s s ( T F o r m ) p r o c e d u r eF o r m C r e a t e ( S e n d e r :T O b j e c t ) ; e n d ; v a r F o r m 1 :T F o r m 1 ; i m p l e m e n t a t i o n { $ R* . d f m }/ /I n c l u d ef o r md e f i n i t i o n s / /As u b r o u t i n eu s e dt od i s p l a yap r i n t c a n c e ld i a l o g p r o c e d u r eC a n c e l D i a l o g ; b e g i n / /D i s p l a yt h ec a n c e lp r i n td i a l o g D i a l o g s . M e s s a g e D l g ( ' P r e s sc a n c e lt oa b o r tp r i n t i n g ' , m t C u s t o m , [ m b C a n c e l ] , 0 ) ; / /N o wt h a tt h eu s e rh a sp r e s s e dc a n c e l ,w ea b o r tt h ep r i n t i n g i fP r i n t e r . P r i n t i n gt h e n b e g i n P r i n t e r . A b o r t ; S h o w M e s s a g e ( ' P r i n t i n ga b o r t e d ' ) ; e n d ; / /E n dt h i st h r e a d e n d T h r e a d ( 0 ) ; e n d ; / /T h em a i nf o r mO nC r e a t er o u t i n e-o u rm a i np r o g r a m p r o c e d u r eT F o r m 1 . F o r m C r e a t e ( S e n d e r :T O b j e c t ) ; c o n s t T O T A L _ P A G E S=4 ; / /H o wm a n yp a g e st op r i n t v a r p r i n t D i a l o g :T P r i n t D i a l o g ; c a n c e l T h r e a d I d:I n t e g e r ; t h r e a d I d :L o n g W o r d ; p a g e ,s t a r t P a g e ,e n d P a g e:I n t e g e r ; b e g i n / /C r e a t eap r i n t e rs e l e c t i o nd i a l o g p r i n t D i a l o g: =T P r i n t D i a l o g . C r e a t e ( F o r m 1 ) ; / /S e tu pp r i n td i a l o go p t i o n s p r i n t D i a l o g . M i n P a g e: =1 ; p r i n t D i a l o g . M a x P a g e: =T O T A L _ P A G E S ; p r i n t D i a l o g . T o P a g e : =T O T A L _ P A G E S ; p r i n t D i a l o g . O p t i o n s: =[ p o P a g e N u m s ] ; / /F i r s ta l l o w e dp a g en u m b e r / /H i g h e s ta l l o w e dp a g en u m b e r / /1t oT o P a g ep a g er a n g ea l l o w e d / /A l l o wp a g er a n g es e l e c t i o n

/ /i ft h eu s e rh a ss e l e c t e dap r i n t e r( o rd e f a u l t ) ,t h e np r i n t ! i fp r i n t D i a l o g . E x e c u t et h e n b e g i n / /S t a r tac a n c e lp r i n td i l a o ga sas e p a r a t et h r e a d ! c a n c e l T h r e a d I d: =b e g i n T h r e a d ( n i l ,

www.delphibasics.co.uk/Article.asp?Name=Printing

3/4

04/06/13

Delphi Basics : <a href="http://www.printerinks.com" target=_blank>Printing</a> text and graphic


c a n c e l T h r e a d I d: =b e g i n T h r e a d ( n i l , 0 , A d d r ( C a n c e l D i a l o g ) , n i l , 0 , t h r e a d I d ) ; / /U s et h eP r i n t e rf u n c t i o nt og e ta c c e s st ot h eg l o b a lT P r i n t e ro b j e c t . / /S e tt ol a n d s c a p eo r i e n t a t i o n P r i n t e r . O r i e n t a t i o n: =p o L a n d s c a p e ; / /S e tt h ep r i n t j o bt i t l e-a si ti ta p p e a r si nt h ep r i n tj o bm a n a g e r P r i n t e r . T i t l e: =' T e s tp r i n tf o rD e l p h i ' ; / /S e tt h en u m b e ro fc o p i e st op r i n te a c hp a g e / /T h i si sc r u d e-i td o i e sn o tt a k eC o l l a t i o ni n t oa c c o u n t P r i n t e r . C o p i e s: =p r i n t D i a l o g . C o p i e s ; / /S t a r tp r i n t i n g P r i n t e r . B e g i n D o c ; / /H a st h eu s e rs e l e c t e dap a g er a n g e ? i fp r i n t D i a l o g . P r i n t R a n g e=p r P a g e N u m st h e n b e g i n s t a r t P a g e: =p r i n t D i a l o g . F r o m P a g e ; e n d P a g e : =p r i n t D i a l o g . T o P a g e ; e n d e l s e/ /A l lp a g e s b e g i n s t a r t P a g e: =1 ; e n d P a g e : =T O T A L _ P A G E S ; e n d ; / /S e tu pt h es t a r tp a g en u m b e r p a g e: =s t a r t P a g e ; / /K e e pp r i n t i n gw h i l s ta l lO K w h i l e( n o tP r i n t e r . A b o r t e d )a n dP r i n t e r . P r i n t i n gd o b e g i n / /S h o wam e s s a g es a y i n gw ea r es t a r t i n gap a g e S h o w M e s s a g e P o s ( ' S t a r t i n gt op r i n tp a g e' + I n t T o S t r ( p a g e ) , 3 0 0 , 3 0 0 ) ; / /S e tu pam e d i u ms i z e df o n t P r i n t e r . C a n v a s . F o n t . S i z e : =1 0 ; / /A l l o wW i n d o w st ok e e pp r o c e s s i n gm e s s a g e s A p p l i c a t i o n . P r o c e s s M e s s a g e s ; / /W r i t eo u tt h ep a g en u m b e r P r i n t e r . C a n v a s . F o n t . C o l o r: =c l B l u e ; P r i n t e r . C a n v a s . T e x t O u t ( 4 0 , 2 0 ,' P a g en u m b e r=' + I n t T o S t r ( p a g e ) ) ; / /U n d e r l i n et h i sp a g en u m b e r P r i n t e r . C a n v a s . M o v e T o ( 4 0 , 8 0 ) ; P r i n t e r . C a n v a s . L i n e T o ( P r i n t e r . P a g e W i d t h 2 0 , 8 0 ) ; / /W r i t eo u tt h ep a g es i z e P r i n t e r . C a n v a s . F o n t . C o l o r: =c l R e d ; P r i n t e r . C a n v a s . T e x t O u t ( 4 0 ,1 0 0 ,' P a g e w i d t h=' + I n t T o S t r ( P r i n t e r . P a g e W i d t h ) ) ; P r i n t e r . C a n v a s . T e x t O u t ( 4 0 ,1 8 0 ,' P a g eh e i g h t=' + I n t T o S t r ( P r i n t e r . P a g e H e i g h t ) ) ; / /I n c r e m e n tt h ep a g en u m b e r I n c ( p a g e ) ; / /N o ws t a r tan e wp a g e-i fn o tt h el a s t i f( p a g e< =e n d P a g e )a n d( n o tP r i n t e r . A b o r t e d ) t h e nP r i n t e r . N e w P a g e ; e n d ;

/ /F i n i s hp r i n t i n g P r i n t e r . E n d D o c ; e n d ; e n d ; e n d .

www.delphibasics.co.uk/Article.asp?Name=Printing

4/4

You might also like