Results 1 to 3 of 3

Thread: PrintDocument and blank pages [Resolved]

  1. #1

    Thread Starter
    Frenzied Member ntg's Avatar
    Join Date
    Sep 2004
    Posts
    1,449

    Resolved 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:
    1. Private WithEvents pdoc As New PrintDocument()
    2.     Private TextToPrint As String
    3.     Private FontSize As Integer
    4.     Private FontName As String
    5.  
    6.     Private Sub pdoc_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles pdoc.PrintPage
    7.  
    8.         Static intCurrentChar As Int32
    9.         Dim font As New Font(FontName, FontSize)
    10.  
    11.         Dim intPrintAreaHeight, intPrintAreaWidth, marginLeft, marginTop As Int32
    12.         With pdoc.DefaultPageSettings
    13.             intPrintAreaHeight = .PaperSize.Height - .Margins.Top - .Margins.Bottom
    14.             intPrintAreaWidth = .PaperSize.Width - .Margins.Left - .Margins.Right
    15.             marginLeft = .Margins.Left
    16.             marginTop = .Margins.Top
    17.         End With
    18.  
    19.         If pdoc.DefaultPageSettings.Landscape Then
    20.             Dim intTemp As Int32
    21.             intTemp = intPrintAreaHeight
    22.             intPrintAreaHeight = intPrintAreaWidth
    23.             intPrintAreaWidth = intTemp
    24.         End If
    25.  
    26.         Dim intLineCount As Int32 = CInt(intPrintAreaHeight / font.Height)
    27.         Dim rectPrintingArea As New RectangleF(marginLeft, marginTop, intPrintAreaWidth, intPrintAreaHeight)
    28.  
    29.         Dim fmt As New StringFormat(StringFormatFlags.LineLimit)
    30.         Dim intLinesFilled, intCharsFitted As Int32
    31.         e.Graphics.MeasureString(Mid(TextToPrint, intCurrentChar + 1), font, _
    32.                    New SizeF(intPrintAreaWidth, intPrintAreaHeight), fmt, _
    33.                     intCharsFitted, intLinesFilled)
    34.  
    35.         e.Graphics.DrawString(Mid(TextToPrint, intCurrentChar + 1), font, _
    36.             Brushes.Black, rectPrintingArea, fmt)
    37.  
    38.         intCurrentChar += intCharsFitted
    39.  
    40.         If intCurrentChar < TextToPrint.Length Then
    41.             e.HasMorePages = True
    42.         Else
    43.             e.HasMorePages = False
    44.             intCurrentChar = 0
    45.         End If
    46.  
    47.     End Sub
    48.  
    49.     Private Sub doPrintPreview()
    50.  
    51.         Dim ppd As New PrintPreviewDialog()
    52.         pdoc.DefaultPageSettings.Landscape = False
    53.         FontSize = 12
    54.         FontName = "Courier New"
    55.  
    56.         Try
    57.             ppd.Document = pdoc
    58.             ppd.Icon = Me.Icon
    59.             ppd.Text = "Print Preview"
    60.             ppd.StartPosition = FormStartPosition.CenterScreen
    61.             ppd.ShowInTaskbar = True
    62.             ppd.WindowState = FormWindowState.Maximized
    63.             ppd.ShowDialog()
    64.         Catch exp As Exception
    65.             MessageBox.Show(exp.ToString, "Exception!")
    66.         End Try
    67.  
    68.     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:
    1. Dim i As Integer
    2.         TextToPrint = "Start of print---"
    3.         For i = 0 To 1159   '1159 works fine - after that, empty pages show up
    4.             TextToPrint += "another one bites the dust" + vbCrLf
    5.         Next
    6.         TextToPrint += "---End of print"
    7.         doPrintPreview()

    I'm completely buffled by this. Anyone has any ideas?
    Last edited by ntg; Jun 15th, 2005 at 04:37 AM.
    "Feel the force...read the source..."
    Utilities: POPFileDebugViewProcess ExplorerWiresharkKeePassUltraVNCPic2Ascii
    .Net tools & open source: DotNetNukelog4NetCLRProfiler
    My open source projects: Thales SimulatorEFT CalculatorSystem Info ReporterVSS2SVNIBAN Functions
    Customer quote: "If the server has a RAID array, why should we bother with backups?"
    Programmer quote: "I never comment my code. Something that is hard to write should be impossible to comprehend."
    Ignorant quote: "I have no respect for universities, as they teach not practicle stuff, and charge money for"

  2. #2
    Addicted Member techwizz's Avatar
    Join Date
    Apr 2005
    Location
    U.S.A.
    Posts
    246

    Re: PrintDocument and blank pages

    Will you attatch the file and ill look at it tommorow?

  3. #3

    Thread Starter
    Frenzied Member ntg's Avatar
    Join Date
    Sep 2004
    Posts
    1,449

    Re: PrintDocument and blank pages

    Found it using the 4-eyes principle.
    VB Code:
    1. e.Graphics.DrawString(Mid(TextToPrint, intCurrentChar + 1), font, _
    must change to:
    VB Code:
    1. e.Graphics.DrawString(Mid(TextToPrint, intCurrentChar + 1, intCharsFitted), font, _
    I just love MS samples.
    "Feel the force...read the source..."
    Utilities: POPFileDebugViewProcess ExplorerWiresharkKeePassUltraVNCPic2Ascii
    .Net tools & open source: DotNetNukelog4NetCLRProfiler
    My open source projects: Thales SimulatorEFT CalculatorSystem Info ReporterVSS2SVNIBAN Functions
    Customer quote: "If the server has a RAID array, why should we bother with backups?"
    Programmer quote: "I never comment my code. Something that is hard to write should be impossible to comprehend."
    Ignorant quote: "I have no respect for universities, as they teach not practicle stuff, and charge money for"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width