-
Jun 24th, 2012, 10:46 AM
#1
Thread Starter
Addicted Member
Printing multipage documents
I am printing a page of text and graphics to the printer using grid references for drawing lines and positioning text. Now I have hit a problem, once I am at the bottom of the page how do I set up the second, third page etc. Do I continue referencing from the top/left corner of the first page or do I somehow create a second page? Any advice welcome does anyone know of a simple tutorial for this.
-
Jun 24th, 2012, 11:53 AM
#2
Re: Printing multipage documents
no. to print a second page, you set e.hasmorepages to true at the end of the PrintDocument_PrintPage event. this causes the PrintDocument_PrintPage event to run again after exiting. so for your second page your coordinates start at 0 again.
obviously you need to setup some method for checking which page you're currently printing (within the PrintDocument_PrintPage event) + print the correct lines + text for that page
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jun 26th, 2012, 03:03 PM
#3
Thread Starter
Addicted Member
Re: Printing multipage documents
e.hasmorepages = true is setting it back to the top of the page but instead of printing page 1 then filling page 2 it is overwriting page 1.
-
Jun 26th, 2012, 03:29 PM
#4
Re: Printing multipage documents
ok. post your code + i'll tell you where to change it.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jun 26th, 2012, 03:44 PM
#5
Thread Starter
Addicted Member
Re: Printing multipage documents
To reduce the amount of code I need to post I have removed all the lines that fill each sheet. Hopefully what is left is sufficient to reflect how I am trying to do it. If not I will post the whole form.
Code:
Private Sub StockTake_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
RadioButton4.Checked = True
StartPageBox.Text = "1"
EndPageBox.Text = "Last"
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim MorePages As Boolean = False
Dim TitleFont As Font
TitleFont = New Font("Times New Roman", 20, FontStyle.Underline)
Dim HeadingFont As Font
HeadingFont = New Font("Arial", 12)
Dim ContentFont As Font
ContentFont = New Font("Times New Roman", 12)
Dim ItemLocated As String = ""
If RadioButton1.Checked = True Then
ItemLocated = "Attic"
ElseIf RadioButton2.Checked = True Then
ItemLocated = "Home"
ElseIf RadioButton3.Checked = True Then
ItemLocated = "Post Office"
End If
Dim connection As New SqlConnection(PubConnString)
Dim SQLdr As SqlDataReader
Dim SQLStr As String
If RadioButton4.Checked = True Then
SQLStr = ("SELECT StockItems.ItemCode, ItemCodes.ItemName, WhereLocated from " _
& "StockItems INNER JOIN Itemcodes on ItemCodes.ItemCode=StockItems.ItemCode order by StockItems.ItemCode")
Else
SQLStr = ("SELECT StockItems.ItemCode, ItemCodes.ItemName, WhereLocated from " _
& "StockItems INNER JOIN Itemcodes on ItemCodes.ItemCode=StockItems.ItemCode " _
& "where WhereLocated = @ItemLocated order by StockItems.ItemCode, WhereLocated")
End If
Dim SQLConn As New SqlConnection()
Dim SQLCmd As New SqlCommand()
SQLConn.ConnectionString = PubConnString
SQLConn.Open()
SQLCmd.Connection = SQLConn
SQLCmd.CommandText = SQLStr
SQLCmd.Parameters.AddWithValue("@ItemLocated", ItemLocated)
SQLdr = SQLCmd.ExecuteReader()
Dim FirstRecord As Boolean = True
Dim CurrentItemCode As String = ""
Dim CurrentLocation As String = ""
Dim ItemCount As Integer = 0
Dim RowPosition As Integer = 100
While SQLdr.Read()
If FirstRecord = True Then
Dim CurrentDate As Date = Date.Now
Dim StocktakeDate As String
StocktakeDate = CurrentDate.ToString("dd/MM/yyyy")
End If
End While
'Process last item
If CurrentLocation = "Attic" Then
e.Graphics.DrawString(ItemCount, ContentFont, Brushes.Black, 482, RowPosition)
Else
If CurrentLocation = "Home" Then
e.Graphics.DrawString(ItemCount, ContentFont, Brushes.Black, 532, RowPosition)
Else
e.Graphics.DrawString(ItemCount, ContentFont, Brushes.Black, 582, RowPosition)
End If
End If
If MorePages = True Then
e.HasMorePages = True
Else
e.HasMorePages = False
End If
End Sub
Private Sub StartPageBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles StartPageBox.KeyPress
If Not Char.IsDigit(e.KeyChar) Then e.Handled = True
If e.KeyChar = Chr(8) Then e.Handled = False 'allow Backspace
If e.KeyChar = Chr(13) Then StartPageBox.Focus()
End Sub
Private Sub EndPageBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles EndPageBox.KeyPress
If EndPageBox.Text = "Last" Then EndPageBox.Text = ""
If Not Char.IsDigit(e.KeyChar) Then e.Handled = True
If e.KeyChar = Chr(8) Then e.Handled = False 'allow Backspace
If e.KeyChar = Chr(13) Then EndPageBox.Focus()
End Sub
Private Sub FirstPageButton_Click(sender As System.Object, e As System.EventArgs) Handles FirstPageButton.Click
StartPageBox.Text = "1"
Label4.Visible = False
End Sub
Private Sub LastPageButton_Click(sender As System.Object, e As System.EventArgs) Handles LastPageButton.Click
EndPageBox.Text = "Last"
Label4.Visible = False
End Sub
Private Sub ContinueButton_Click(sender As System.Object, e As System.EventArgs) Handles ContinueButton.Click
If EndPageBox.Text <> "Last" Then
If CInt(StartPageBox.Text) > CInt(EndPageBox.Text) Then
Label4.Visible = True
GoTo restart
End If
End If
Dim PrintDialog1 As New PrintDialog()
PrintDialog1.Document = PrintDocument1
Dim result As DialogResult = PrintDialog1.ShowDialog
If (result = Windows.Forms.DialogResult.OK) Then
PrintPreviewDialog1.Document = PrintDocument1
PrintPreviewDialog1.ShowDialog()
PrintDocument1.Print()
End If
End Sub
-
Jun 26th, 2012, 03:58 PM
#6
Re: Printing multipage documents
your code is complicated by the db. here's a template for how you should approach your printing:
vb Code:
Public Class Form1
Private Sub PrintDocument1_BeginPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles PrintDocument1.BeginPrint
'read your db into a datatable here
'we'll call it dt
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Static startAt As Integer = 0
Dim startY As Integer = CInt(PrintDocument1.DefaultPageSettings.PrintableArea.Top)
'you can put additional code here
For x As Integer = startAt To dt.rows.count - 1
'print your fields
'increment startY
If startY > PrintDocument1.DefaultPageSettings.PrintableArea.Height Then
startAt = x + 1
If startAt < dt.rows.count Then
e.HasMorePages = True
Return
Else
startAt = 0
End If
End If
Next
'don't put any additional code here
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jun 29th, 2012, 02:19 PM
#7
Thread Starter
Addicted Member
Re: Printing multipage documents
Thanks Paul, not sure why my original code wasn't working but it is now working fine and a lot less complicated.
How can I control what pages are to be printed. The printdialog box 'From page' and 'To page' doesn't appear to be active. I have created 2 text boxes on the form so the user can select a start and end page but I am struggling with the From Page. The To page is easy I just set hasmorepages to false.
-
Jun 29th, 2012, 02:26 PM
#8
Re: Printing multipage documents
The printdialog box 'From page' and 'To page' is just for choosing which pages to print. you have to code the actual printing yourself.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|