We help IT Professionals succeed at work.
Get Started

PrintDocument issue with Fonts and Page Break

gwosgood
gwosgood asked
on
1,952 Views
Last Modified: 2012-05-09
Greetings Experts,

I've been trying with mixed success to put together a decent print routine to print a series of daily sales and inventory summary reports based off SQL query criteria.  After a lot of research and trial and error, I have been able to piece together a somewhat workable subroutine, however there are still a few holes in the hull.  
The first issue im having is with font settings.  

the line -

               printFont = New Font("Currier New", 8)

doesn't really seem to affect how the page prints out at this point -

               ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, New StringFormat())

a console.writeline printout of the printFont name reveals this --

               Console.WriteLine(printFont.Name & " ," & printFont.OriginalFontName)
               "Microsoft Sans Serif ,Currier New"

It seems like the fontcode being sent to the printer is sans serif instead of currier new.  When I print the same file through notepad, I get the desired Currier New font settings.  The two documents, original printed through this print routine, and the second printed through notepad, are completely different in terms of look.  Any ideas of what is causing the difference in appearance?

The other issue im having is with the form feed / page break codes.  For several of the reports I generate, particularly the summary reports, I often need to insert the page break command to separate groups of data.  I generate these reports using the StreamWriter object and send the vbFormFeed code when necessary.  The problem is that I can not get the printer to recognize the page break code, instead it just prints out a block character.  Is there a better way for me to insert the page break trigger?

Here is my currernt new page code -

       If bolEndOfSeries = True then
                 swPRINTReport.WriteLine(vbFormFeed)
       End If


Any assistance you can give to help clear these small issues up would be much appreciated, I would really like to be able to close this matter and move on to other things.  

Private Function PrintDocument(ByVal printer As String, ByVal file As String) As Integer
        Dim returnValue As Integer = 0

        Try
            streamToPrint = New StreamReader(file)
            Try
                printFont = New Font("Currier New", 8)

                Dim pd As New PrintDocument
                AddHandler pd.PrintPage, AddressOf pd_PrintPage

                pd.PrinterSettings.PrinterName = printer
                pd.DefaultPageSettings.Landscape = True

                If pd.PrinterSettings.IsValid Then
                    pd.Print()
                    returnValue = 0
                Else
                    MessageBox.Show("Printer is invalid")
                    returnValue = 1
                End If
            Finally
                streamToPrint.Close()
            End Try
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            'returnValue = ex.
        End Try

        Return returnValue

End Function

Private Sub pd_PrintPage(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
        Dim linesPerPage As Single = 0
        Dim yPos As Single = 0
        Dim count As Integer = 0
        Dim leftMargin As Single = 25
        Dim topMargin As Single = 25

        Dim line As String = Nothing

        ' Calculate the number of lines per page.
        linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics)
        'linesPerPage = 66

        ' Print each line of the file.
        While count < linesPerPage
            line = streamToPrint.ReadLine()
            If line Is Nothing Then
                Exit While
            End If
            yPos = topMargin + count * printFont.GetHeight(ev.Graphics)
            ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, New StringFormat())
            count += 1
        End While

        ' If more lines exist, print another page.
        If Not (line Is Nothing) Then
            ev.HasMorePages = True
        Else
            ev.HasMorePages = False
        End If
End Sub

Open in new window

Comment
Watch Question
Commented:
This problem has been solved!
Unlock 1 Answer and 6 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE