I had added a PrintDocument to my Project to print something on A4 paper.
This is the code:
Code:
Private Sub PrintDocument_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument.PrintPage
Dim intY As Integer = 158
'Print cash
e.Graphics.DrawString("/", New Font("Courier New", 10, FontStyle.Bold), Brushes.Black, 165, intY - 20)
'Print cheque
e.Graphics.DrawString("/", New Font("Courier New", 10, FontStyle.Bold), Brushes.Black, 165, intY)
'Print cheque number
e.Graphics.DrawString("123456", New Font("Courier New", 12, FontStyle.Bold), Brushes.Black, 315, intY - 4)
'Print amount
e.Graphics.DrawString("12345.00", New Font("Courier New", 12, FontStyle.Bold), Brushes.Black, 440, intY)
'Print number of employees
e.Graphics.DrawString("123", New Font("Courier New", 12, FontStyle.Bold), Brushes.Black, 580, intY)
End Sub
Using the above codes, it will print everything on 1 piece of A4 paper on the specific (x, y) coordinate.
My question is, if i have very very long information to be printed, and requires it to be printed on 2nd, 3rd, 4th, 5th pages and etc... How should i specific its (x, y) coordinate?
Please guide me, thank you!
Last edited by albertlse; Aug 25th, 2003 at 04:03 AM.
What you need to is get the textheight of the font... increment a counter by that amount for every line.. if that amount is greater than the height of the paper... you need to NewPage...
somethin akin to:
VB Code:
'myYoffset being the next Y coordinate to move to...
I don't understand how much clearer i can be...post a pic of what you want to do...
For different font sizes, you need to measure the fontsize of every font you use... add it to sum... when the sum is greater than the alloted space... NEWPAGE.
Attached is a picture of the form that I want to print to.
Refering to the picture of the form, at the center of the form there are 20 rows to be filled up.
I use this code to fill up 20 row into 1 form:
Code:
Private Sub PrintDocument_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument.PrintPage
Dim intY As Integer = 158
'Print cash
e.Graphics.DrawString("/", New Font("Courier New", 10, FontStyle.Bold), Brushes.Black, 165, intY - 20)
'Print cheque
e.Graphics.DrawString("/", New Font("Courier New", 10, FontStyle.Bold), Brushes.Black, 165, intY)
'Print cheque number
e.Graphics.DrawString("123456", New Font("Courier New", 12, FontStyle.Bold), Brushes.Black, 315, intY - 4)
'Print amount
e.Graphics.DrawString("12345.00", New Font("Courier New", 12, FontStyle.Bold), Brushes.Black, 440, intY)
'Print number of employees
e.Graphics.DrawString("123", New Font("Courier New", 12, FontStyle.Bold), Brushes.Black, 580, intY)
'Print 20 records in 1 form
intY = 450
Dim intRow As Integer
For intRow = 0 To 19
'Print IC
e.Graphics.DrawString("111111223333", New Font("Courier New", 12, FontStyle.Bold), Brushes.Black, 18, intY)
''Print SOCSO Number
e.Graphics.DrawString("S12345678", New Font("Courier New", 12, FontStyle.Bold), Brushes.Black, 160, intY)
''Print Cross
e.Graphics.DrawString("X", New Font("Courier New", 12, FontStyle.Bold), Brushes.Black, 270, intY)
''Print Name
e.Graphics.DrawString("NAME NAME NAME", New Font("Courier New", 12, FontStyle.Bold), Brushes.Black, 300, intY)
''Print Amount
e.Graphics.DrawString("1", New Font("Courier New", 12, FontStyle.Bold), Brushes.Black, 685, intY - 1)
e.Graphics.DrawString("2", New Font("Courier New", 12, FontStyle.Bold), Brushes.Black, 710, intY - 1)
e.Graphics.DrawString("5", New Font("Courier New", 12, FontStyle.Bold), Brushes.Black, 740, intY - 1)
e.Graphics.DrawString("7", New Font("Courier New", 12, FontStyle.Bold), Brushes.Black, 765, intY - 1)
intY += 25
Next
intY = 955
'Print Sub-Total Amount
e.Graphics.DrawString("4", New Font("Courier New", 12, FontStyle.Bold), Brushes.Black, 635, intY)
e.Graphics.DrawString("4", New Font("Courier New", 12, FontStyle.Bold), Brushes.Black, 660, intY)
e.Graphics.DrawString("4", New Font("Courier New", 12, FontStyle.Bold), Brushes.Black, 685, intY)
e.Graphics.DrawString("4", New Font("Courier New", 12, FontStyle.Bold), Brushes.Black, 710, intY)
e.Graphics.DrawString("4", New Font("Courier New", 12, FontStyle.Bold), Brushes.Black, 740, intY)
e.Graphics.DrawString("4", New Font("Courier New", 12, FontStyle.Bold), Brushes.Black, 765, intY)
intY = 1000
'Print Grand Total Amount
e.Graphics.DrawString("3", New Font("Courier New", 12, FontStyle.Bold), Brushes.Black, 610, intY)
e.Graphics.DrawString("3", New Font("Courier New", 12, FontStyle.Bold), Brushes.Black, 635, intY)
e.Graphics.DrawString("3", New Font("Courier New", 12, FontStyle.Bold), Brushes.Black, 660, intY)
e.Graphics.DrawString("3", New Font("Courier New", 12, FontStyle.Bold), Brushes.Black, 685, intY)
e.Graphics.DrawString("3", New Font("Courier New", 12, FontStyle.Bold), Brushes.Black, 710, intY)
e.Graphics.DrawString("3", New Font("Courier New", 12, FontStyle.Bold), Brushes.Black, 740, intY)
e.Graphics.DrawString("3", New Font("Courier New", 12, FontStyle.Bold), Brushes.Black, 765, intY)
End Sub
if i have more than 20 records to be filled into the form, i sure need to print more than 1 form right? clear?
I want to tell you a somewhat better way to do this...
Again, you are printing rows... you will need to check the fontsize of each row... and when the sum of the sizes is greater than the space alloted, you start a new page.
The only thing you need to worry about is if you have to print more than 20 records on a form, its the only thing that will force a new page. So you must only allow 20 records per page.. obvious enough.
Take a look at a sub i wrote for a program below:
VB Code:
Private Sub DrawItems(ByRef g As Graphics, ByRef b As Brush, ByRef f As Font, _
ByRef yourItems As ItemCollection, [b]ByVal x As Integer, _
ByVal y As Integer, ByVal [width] As Integer, ByVal [height] As Integer[/b])
Note the bold part of the sub header. I am actually defining a rectangle that encompasses the surface area that holds the records. (see image link here)
The yellow outline is the rectangle that defines the area. No record can print outside that area, because if the sum of the row heights becomes greater than that area, I force a new page to be printed, but before doing, i remove all the records from the collection that have been printed.
So when the next page prints, the remaining records are printed.
Now...
here's some other code to help you out:
when you do a graphics.DrawString(), you can also pass a rectangle that defines the area that can be drawn to.
for example, g.drawString(myrecord, myFont, myBrush, myRectangle)
In the case of your form, you will take the rectangle that encompasses the whole row area (yellow outline), and divide its height by 20, but keeping its width the same. You assign that to a new rectangle, and you use that rectangle to draw each record.(indicated by the red rectangles in the image link i posted).
Last edited by nemaroller; Aug 21st, 2003 at 09:37 PM.
I whip something together for you in two days...
But in the meantime...
1) Do you need to print anything on the form besides the 20 records?
2) What objects hold your records...? Are they collections of a class you wrote, or datasets?
3) Can one record span two vertical lines on the form? (for example, can one record take two lines, only allowing 18 more records on the form?... OR are they all limited to one line?)
other then the 20 rows of records,
i need to print the name & address (as you can see on the picture form) on every page of forms.
i need to print the sub-total of salary for every page of forms.
i need to print the grand total of salary on the last page of forms only, not every page...
i wish to put exactly 1 record in each row. no spanning is allow.
actually i still havent design wat object should holds my records. but the data is from database. so you think which design is suitable? a dataset? or datagrid? pls suggest...
all the x and y coordinates i use in the code (which can b seen in my first posting) is exactly where the text whould be printed. you may use that for reference if you need.
hope my explanation is clear to you. feel free to ask me if anything confuse you.
hope to get the good news (the coding i mean) from you very soon after 2 days!!!
actually i still havent design wat object should holds my records. but the data is from database. so you think which design is suitable? a dataset? or datagrid? pls suggest...
Well... a dataset probably. I don't know what other requirements you have for your program... if you need to translate the data before printing, or do other evaluations, you might be better off grabbing the data from the database and creating objects that represent that data, and putting those objects into a collection.
If you don't have those requirements, then its a waste of resources and time required to design the classes.
So you decide and be sure to get back to me..
Btw, should the FINAL TOTAL be printed in the rows section ? Or printed beneath it? or above it?
actually the 20 records should be retrieve from database data.
but for testing now, just use text only, is it possible?
just try it in simple way.
actually every row of records contains name, NRIC, membership a/c number and salary.
the "sub-total" is the total of salary in each page. it should appears at the bottom, after the 20 records.
whereas, the "grand-total" is the total of all sub-total of all pages. it should appears at the bottom of "sub-total", but only appears once, which is at the last page only.
hope my information is clear enough. feel free to ask me if any confuse. thank you.
Now of course, I wouldn't recommend using my code as listed..
its crude!
But you can see that I remove an item from the collection of data after its printed, and if there are more items than can be printed ona page, I interupt the loops printing the items, and do a next page, and then continue...