Printing through CODE

  • Thread starter Thread starter TerishD
  • Start date Start date
T

TerishD

First, let me tell you what I do NOT want.

1) To print a document.

2) To click on ANYTHING

Now, how do I print to a printer? It seems that I need
PrintPageEventArgs to print to a printer. Let me make
this simple.

Public Sub Main()
 
Terish,
I take you want to send output to a printer, correct? Are you wanting to
write directly to the printer bypassing the Windows print driver?

Or would you like Windows to handle the formatting for you?

If you want Windows to handle the formatting you need to use the
PrintDocument class that I identified in my earlier post.

Remember Windows considers any output sent to the printer to be printed a
Document. Hence the name PrintDocument for the class. It has nothing really
to do with file 'Documents' such as Word.

The links I gave you include samples along with the explanation of how it
works.

Hope this helps
Jay
 
No Modify? This thing took off and posted on its own.
Anyway, to continue.

Public sub MainRout()
dim I as integer
dim S(10) as string
S(0)="Red"
S(1)="Green"
S(2)="Blue"
I=int(rnd*3)

PrintToPrinter(S(I)) 'HOW DO I DO THIS??????????????

End sub

'Note, the below routine was stolen from previous
<i>Help</i>, but cannot be called from inside code.

Public Sub pd_PrintPage(ByVal sender As Object, ByVal ev
As PrintPageEventArgs)
Dim LnPPg As Single = 0
Dim pfnt As Font
Dim MrgnL As Single = ev.MarginBounds.Left
Dim MrgnT As Single = ev.MarginBounds.Top
Dim SL As String = Nothing

' Calculate the number of lines per page.
PFnt = New Font(FtNm, FtSz)
LnPPg = ev.MarginBounds.Height / PFnt.GetHeight
(ev.Graphics)
' Iterate over the file, printing each line.

ev.Graphics.DrawString(SP, PFnt, Brushes.Black,
MrgnL, PY, New StringFormat())

' If more lines exist, print another page.
If Not (SL Is Nothing) Then
ev.HasMorePages = True
Else
ev.HasMorePages = False
End If
End Sub
 
First thanks, you are being helpful.

Now, let me rephrase my question. I actually figured how
to get things to print. So now, the question is

How does Microsoft expect me to set up a page to be
printed? The thing is printing one line to a page, and
there seems to be no way to tell it that all those lines
go on the same page.
 
TerishD,
Assuming you are using the print event, you need to draw each line in one
event.

Use a for each loop in your print event, to draw each line of text,
increasing the y by the height of the line. You can use
Graphics.MeasureString to get the size of a line.

This example is based on printing a DataSet, however it should give you
ideas how to arrange information for anything you want to print.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnadvnet/html/vbnet01282003.asp

Hope this helps
Jay
 
Back
Top