VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Printer Object A Primer

by Gajendra S. Dhir (3 Submissions)
Category: Miscellaneous
Compatability: Visual Basic 3.0
Difficulty: Intermediate
Date Added: Wed 3rd February 2021
Rating: (19 Votes)

The focus of my article is to demystify the printer object and present it as a magnificient object, which can be used to churn out dashing printouts without the support of any third party reporting tool.

Rate Printer Object A Primer


by,

 Gajendra S. Dhir

 Team Leader

 Data Spec

 Bilaspur-CG, INDIA

 

All programmers creating software solutions for their client, invariably have to process data and generate output on paper, using the printer, in the form  of reports. There are many third party tools available in the market which are 
 instrucmental in generating beautifully crafted reports. 


I, too, have used such report writers, until recently, for my even my most 
 simple printing requirements. That is until I discovered the power of the printer 
 object. 


Most literature on Visual Basic, including books and articles, generally explore 
 this printer object superficially and this, I believe is, why most 
 of us tend to overlook this simple yet powerful printing tool.


The focus of my article is to demystify the printer object and 
 present it as a magnificient object, which can be used to churn out dashing 
 printouts without the support of any third party reporting tool. For detailed 
 syntaxes of the objects, statements, commands, properties and methods used here 
 you are requested to refer to the excellent documentation provided by Microsoft.


The sub-topics covered in the article include...



     
  • Select Printer 

  •  
  • Set the Page dimensions

  •  
  • Change to a new page

  •  
  • End of a Print Job

  •  
  • Cancel the Print Job

  •  
  • Position the head

  •  
  • Print the text

  •  
  • Justification - Left, Right, Center

  •  
  • Font - Name, Size and Style

  •  
  • Print in Color

  •  
  • Points for Consideration



Select the printer 


Windows operating system allows you to install more than one printer. One of 
 these is marked as the default printer and is offered as choice for printing 
 by the applications. 


VB provides us with the Printers collection and the Printer 
 object to take care of our printing requirements.


The printers collection contains a list of the printers installed 
 on your system. Printers.Count specifies the number of printers 
 installed and any printer can be selected as Printers(i), where 
 i is a number between 0 and Printer.Count-1.


To get a list of all the printers installed we could use a code snipet, like 
 this...


For i = 1 to Printers.Count - 1

     Printer.Print Printers(i).Name

 Next i

 Printer.EndDoc


or 


For Each P in Printers

     Printer.Print P.Name

 Next P

 Printer.EndDoc


The Printer object represents the printer which has been marked 
 as the default printer in the Windows environment.


The entire discussion here uses the printer object and can 
 easily be modified to use the Printers(i) object.


Setup Page Dimensions


The next thing that you must do is setup the dimensions of the paper on which 
 you will be printing. Windows has 41 predefined paper sizes based on the standard 
 paper sizes available around the world. Other than these if the size of the 
 paper does not match any of these pre-defined sizes you may set it the custom 
 size and specify your own height and width for the paper. The properties used 
 here are Printer.PaperSizePrinter.Height and Printer.Width.


 The more commonly used paper sizes are... 


  Printer.PaperSize = vbPRPSLetter

 or

  Printer.PaperSize = vbPRPSA4


Please refer to the Microsoft documentation for a complete list of paper size 
 constants.


To use a custom size paper your code will look something like...


  Printer.Height = 10 * 1440         ' 
 10 inch height x 1440 twips per inch

   Printer.Width = 5 * 1440           '  5 
 inch height x 1440 twips per inch


Any attempt to alter the height or the width of the printer object, 
 automatically changes the Printer.PaperSize to vbPRPSUser


While you are at it, you may also want to setup the orientation of the paper. 


  Printer.Orientation = vbPRORPortrait

 
 or

   Printer.Orientation = vbPRORLandscape


Any time during the print session you want to check the dimensions of the paper 
 size you can refer to the height and width properties 
 for the printer object.


While printing a page a typical use for the height is to compare the paper 
 length with current position of the printer head and determine whether the next 
 line can be printed on the same page or you should request for a new page.


Note: Depending upon the printer driver installed for the printer 
 it may or may not report an error is any of the printer properties is set beyond 
 the acceptable range.



Change to a new page


Printing to the printer is done in page mode, i.e. the printer 
 object sends data for printing to the operating system only after it is informed 
 that the current page formatting is complete and is ready for printing. 


In VB, this is accomplished by invoking the NewPage method like 
 this... 


Printer.NewPage


This method instructs the printer object to end the current page 
 and advance to the next page. 


End of Print Job


 When you have completed printing all the text and graphics that required to 
 be printed in this print job the printer object must be so informed. 
 You can do so using the EndDoc method.


Printer.EndDoc


This terminates a print operation and releases the document to the printer. 
 If something has been printed on the current page it automatically issues a 
 Printer.NewPage to complete printing of the page. If a Printer.NewPage 
 has been issued just before the Printer.EndDoc method, no blank 
 page is printed.


Cancel the Print Job


There will be occasions when you may want to abort the print session. This 
 may be in response to a cancel request from the user or any such situation requiring 
 you to do so.


For such times we have been provided with the KillDoc method.


Printer.KillDoc


The difference of the KillDoc and the EndDoc methods 
 is more apparent when the operating system's Print Manager is handling the print 
 jobs. If the operating system's Print Manager is handling the print job KillDoc 
 deletes the current print job and the printer receives nothing.


If Print Manager isn't handling the print job, some or all of the data may 
 be sent to the printer before KillDoc can take effect. In this 
 case, the printer driver resets the printer when possible and terminates the 
 print job.



Position the Head


We can get or set the position using the two properties, Printer.CurrentX 
 and Printer.CurrentY. As obvious by their names the return the 
 position on the X and Y axes respectively.


Label1.Caption = "(" & Printer.CurrentX & ", " & Printer.CurrentY & ")" 


Alternately, you may use these very functions to position the printer head 
 as per your requirement.


Printer.CurrentX = 1440

 Printer.CurrentY = 1440


Remember 1 inch = 1440 twips. so this previous code snipet should position 
 the printer head 1 inch from each the top and left margins. Similarly this next 
 code snipet here will position the printer head at the center of the page (half 
 of width and height).


Printer.CurrentX = Printer.Width / 2

 Printer.CurrentY = Printer.Height / 2


Every print instruction issued to place text or graphic on the page moves the 
 CurrentX and CurrentY and should be considered and, 
 if necessary, taken care of before issuing the next print instruction.


Print out the text


To print use...

 

 Printer.Print "Text to Print" 

 

 Printing starts at the location marked by the CurrentX and CurrentY.

 

 After the text as been printed the values of the CurrentX and CurrentY 
 are changed to the new location. The new location is different when a , (comma) 
 or a ; (semi-colon) is added at the end of the Print statement. 
 Run the following code and compare the results...


Code 1 

Printer.CurrentX = 0

 Printer.CurrentY = 0

 For i = 1 to 5

    Printer.Print Printer.CurrentX & ", " & 
 Printer.CurrentY

 Next i


Code 2 

Printer.CurrentX = 0

 Printer.CurrentY = 0

 For i = 1 to 5

    Printer.Print Printer.CurrentX & ", " & 
 Printer.CurrentY;

 Next i


notice the ; (semi-colon) at the end of the print statement. 


and Code 3 

Printer.CurrentX = 0

 Printer.CurrentY = 0

 For i = 1 to 5

    Printer.Print Printer.CurrentX & ", " & 
 Printer.CurrentY,

 Next i


in this case note the , (comma) at the end of the print statement.


Justification - Left, Right or Center


Justification is accomplished with the help of two methods of the printer 
 object, viz Printer.TextHeight(Text) and Printer.TextWidth(Text)
 with which we can determine the about of vertical and horizontal space that 
 will be occupied when you print the Text.


So in this example...


mTxt = "Gajendra S. Dhir"

 TxtWidth = Printer.TextWidth(mTxt)


TxtWidth is the amount of horizontal space required by the text 
 in mTxt to print.


Let us see print this as Left, Right and Center Justified.


'to leave 1" Margins on the Left, Right and Top of the Printer

 Printer.CurrentX = 1440

 MaxWidth = Printer.Width - 1440*2

 Printer.CurrentY = 1440

 


Left Justified is the simplest form of justification and the head position 
 is already set.


Printer.Print mTxt


The printer head automatically moves to the starting point on the next line 
 as there is no comma or semi-colon at the end of the Print


Lets try right justification. We have CurrentY set for 
 the next print statement. We need to set the CurrentX. Now we will 
 require the MaxWidth and TxtWidth values, which we 
 have ready with us (above).


' add 1440 is to maintain the 1" Left Margin.

 Printer.CurrentX = 1440 + (MaxWidth - TxtWidth)

 Printer.Print mTxt


Similarly, you can achieve center justification 


 Printer.CurrentX = 1440 + (MaxWidth - TxtWidth)/2    'again 
 1440 is to maintain Left Margin.

 Printer.Print mTxt


This is all there is to printing text.


Ah yes ... just one more thing before we proceed. The above logic assume that 
 TxtWidth < MaxWidth. If the width of the text is greater than 
 the maximum print width then you must separately process the text to either 
 truncate it so that it fits the MaxWidth or split the lines suitably 
 to simulate word-wrap.


For those interested, here's the entire code, 


 mTxt = "Gajendra S. Dhir"

 TxtWidth = Printer.TextWidth(mTxt)

 

 
'to leave 1" Margins on the Top, Left and Right of the page

 Printer.CurrentY = 1440

 Printer.CurrentX = 1440

 MaxWidth = Printer.Width - 1440*2

 

 'Left Justified - no extra work

 Printer.Print mTxt

 

 'Right Justified

 Printer.CurrentX = 1440 + (MaxWidth - TxtWidth)  ' add 1440 is to 
 maintain the 1" Left Margin

 Printer.Print mTxt 

 

 'Center Justified

 Printer.CurrentX = 1440 + (MaxWidth - TxtWidth)/2    'again 
 1440 is to maintain Left Margin.

 Printer.Print mTxt

 

 'Terminate Printing

 Printer.EndDoc 


Font Name, Size and Style


A wide variety of fonts, also known as typefaces, are available under the Windows 
 operating system. Some are optimized for better screen appearance while others 
 are designed with the printed output in mind. The printer that you use also 
 has certain built-in fonts which you can access from your VB program.


The Printer.FontCount property tells you the number of fonts that 
 are available in your system and are supported by current the printer. You can 
 select the name of the font that you want to use for printing your text from 
 the Printer.Fonts collection


To get a list of the names of the fonts available you can use a loop like this...


For i = 0 to Printer.FontCount-1

     Printer.Print Printer.Fonts(i)

 Next i
 


or better still you could use the Printer.Font.Name property like 
 this...


For i = 0 to Printer.FontCount-1

     Printer.Font.Name = Printer.Fonts(i)

     Printer.Print Printer.Font.Name

 Next i
 


to get a complete list of the fonts available with each Font.Name 
 printed using that very typeface. 


To determine or alter the size of the text that is being printed you must access 
 the Printer.Font.Size property. Mayby something like this...


mSize = Printer.Font.Size

 Printer.Font.Size = mSize + 4

 Printer.Print "THE TITLE TEXT"

 Printer.Font.Size = mSize


Other than this, control for BoldItalicUnderline and 
 Strikethru characteristics of a font that are available at your disposal 
 as a Visual Basic programmer. These are boolean properties and take the values 
 True or False. You may use these properties as...


 Printer.Font.Bold = True to enable and False 
 to disable

  Printer.Font.Italic = True to enable and False 
 to disable

  Printer.Font.Strikethrough = True to enable and False 
 to disable

 and

  Printer.Font.Underline = True to enable and False 
 to disable


The following code will give you a printout of all the printer fonts installed 
 on your system along with the "bold" and "italic
 texts printed next to the font name.


With Printer

   For i = 0 to .FontCount-1

     .Font.Name = Printer.Fonts(i)

     .Print Printer.Font.Name;     'Note 
 the ; (semi-colon) at the end of print

     .Font.Bold = True

     .Print " Bold";               'Note 
 the ; (semi-colon) at the end of print

     .Font.Bold = False

     .Font.Italic = True

     .Print " Italic"              'Note 
 no ; (semi-colon) at the end of print

     .Font.Italic = False

     If Printer.CurrentY + Printer.TextHeight("NextLine") 
 > Printer.Height - 720 Then

       Printer.NewPage

     End If

   Next i

 End Width

 

 'Terminate Printing

 Printer.EndDoc 

 


When working with the fonts you can also use .FontName.FontSize
 .FontBold.FontItalic.FontStrikeThru
 .FontUnderline for .Font.Name.Font.Size
 .Font.Bold.Font.Italic.Font.Strikethrough
 .Font.Underline used above.


Print in Color


Printing in color adds to the presentation value of the final output. Let us 
 add some color to our printing. 


Use the Printer.ColorMode to enable or disable color printing for your color printer.


Printer.ColorMode = vbPRCMColor

 
 or

 Printer.ColorMode = vbPRCMMonochrome

 


Depending on the printer installed, when you the set the printer to vbPRCMMonochrome 
 prints in shades of black and white. 


Once you have activated color printing you can control the color of the output 
 through two properties two properties, backcolor and forecolor
 of the printer, to control the color of the background and the 
 foreground respectively. The color values can be assigned to these properties 
 using the RGB() function.


Printer.ForeColor = RGB(255, 0, 0)     ' For 
 Text in Red Color

 Printer.Print "This text is in Red ";

 Printer.ForeColor = RGB(0, 0, 255)     ' For Text in 
 Blue

 Printer.Print "and this is in Blue"

 Printer.BackColor = RGB(255, 255, 0)   ' For Background in Yellow

 Printer.Print "The text here is Blue and the background is Yellow"
 


Visual Basic has provided color constants for the standard colors, namely vbBlue
 vbRedvbGreenvbMagentavbCyan
 vbYellowvbBlack and vbWhite.


Points for Consideration


Here are some tips which I think you will find useful during your exploration 
 of the printer object...



     
  • You will need simple sub-routines to print text - left, right and center 
      justified within a maximum width that you may specify. This will allow you 
      to create the columns in a tabular report and adequately justify the text 
      within the column.

  •  
  • You could write a function to split long strings based on the print width 
      to enable word wrapping. See my previous code submitted titled 
      Split Strings for Word Wrapping.

  •  
  • The printer uses the same concept of device contexts that is used by Form 
      and PictureBox Control. The difference is only in methods like EndDoc
      KillDocCls etc. Using code like...

      If Destination = "Printer" Then

          Set objDC = Printer

      Else

          Set objDC = Picture1

      Endif

      objDC.Print "Hello! This is Gajendra"


      you can easily create a print preview.


I welcome and will appreciate constructive feedback and creative suggestions.

Download this snippet    Add to My Saved Code

Printer Object A Primer Comments

No comments have been posted about Printer Object A Primer. Why not be the first to post a comment about Printer Object A Primer.

Post your comment

Subject:
Message:
0/1000 characters