|
-
Jun 14th, 2005, 04:42 PM
#1
Thread Starter
Frenzied Member
PrintDocument and blank pages [Resolved]
I'm having a problem with print preview that's thoroughly driving me up the wall. I'm using something like the code below to print simple text:
VB Code:
Private WithEvents pdoc As New PrintDocument()
Private TextToPrint As String
Private FontSize As Integer
Private FontName As String
Private Sub pdoc_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles pdoc.PrintPage
Static intCurrentChar As Int32
Dim font As New Font(FontName, FontSize)
Dim intPrintAreaHeight, intPrintAreaWidth, marginLeft, marginTop As Int32
With pdoc.DefaultPageSettings
intPrintAreaHeight = .PaperSize.Height - .Margins.Top - .Margins.Bottom
intPrintAreaWidth = .PaperSize.Width - .Margins.Left - .Margins.Right
marginLeft = .Margins.Left
marginTop = .Margins.Top
End With
If pdoc.DefaultPageSettings.Landscape Then
Dim intTemp As Int32
intTemp = intPrintAreaHeight
intPrintAreaHeight = intPrintAreaWidth
intPrintAreaWidth = intTemp
End If
Dim intLineCount As Int32 = CInt(intPrintAreaHeight / font.Height)
Dim rectPrintingArea As New RectangleF(marginLeft, marginTop, intPrintAreaWidth, intPrintAreaHeight)
Dim fmt As New StringFormat(StringFormatFlags.LineLimit)
Dim intLinesFilled, intCharsFitted As Int32
e.Graphics.MeasureString(Mid(TextToPrint, intCurrentChar + 1), font, _
New SizeF(intPrintAreaWidth, intPrintAreaHeight), fmt, _
intCharsFitted, intLinesFilled)
e.Graphics.DrawString(Mid(TextToPrint, intCurrentChar + 1), font, _
Brushes.Black, rectPrintingArea, fmt)
intCurrentChar += intCharsFitted
If intCurrentChar < TextToPrint.Length Then
e.HasMorePages = True
Else
e.HasMorePages = False
intCurrentChar = 0
End If
End Sub
Private Sub doPrintPreview()
Dim ppd As New PrintPreviewDialog()
pdoc.DefaultPageSettings.Landscape = False
FontSize = 12
FontName = "Courier New"
Try
ppd.Document = pdoc
ppd.Icon = Me.Icon
ppd.Text = "Print Preview"
ppd.StartPosition = FormStartPosition.CenterScreen
ppd.ShowInTaskbar = True
ppd.WindowState = FormWindowState.Maximized
ppd.ShowDialog()
Catch exp As Exception
MessageBox.Show(exp.ToString, "Exception!")
End Try
End Sub
Most of it is copy-pasted from an MS sample or something. Now, this works fine...as long as TextToPrint contains less than 1160 lines of text (at least when this runs on my box). After that, I start getting blank pages at the beginning of the print preview. When the report is being printed, the result is correct (everything is printed as expected including the first pages that don't show up in the preview window). As the number of lines increases, more blank pages show up at the beginning of the print preview.
VB Code:
Dim i As Integer
TextToPrint = "Start of print---"
For i = 0 To 1159 '1159 works fine - after that, empty pages show up
TextToPrint += "another one bites the dust" + vbCrLf
Next
TextToPrint += "---End of print"
doPrintPreview()
I'm completely buffled by this. Anyone has any ideas?
Last edited by ntg; Jun 15th, 2005 at 04:37 AM.
-
Jun 14th, 2005, 10:07 PM
#2
Addicted Member
Re: PrintDocument and blank pages
Will you attatch the file and ill look at it tommorow?
-
Jun 15th, 2005, 04:36 AM
#3
Thread Starter
Frenzied Member
Re: PrintDocument and blank pages
Found it using the 4-eyes principle.
VB Code:
e.Graphics.DrawString(Mid(TextToPrint, intCurrentChar + 1), font, _
must change to:
VB Code:
e.Graphics.DrawString(Mid(TextToPrint, intCurrentChar + 1, intCharsFitted), font, _
I just love MS samples.
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
|