PrintDocument
Hi there.
I am working on a VB.Net application (WinForms) and all is working fine. I'm just putting the finishing touches to it, and am now working on a Print function.
The application is a code editor, and of course, one of the facilities is to be able to print the code that one has written. I have consulted the MSDN documentation, and, with it's help, I have managed to write a well behaved routine that does indeed print to the printer, using the PrintDocument_PrintPage event.
However, there is one problem - some lines that are longer than the page width are 'clipped' and thus there is test missing from the printout.
I need someway to know if a line of text is too long, and if so, at which point do I split the string?
I have seen a reference to MeasureString on this page: http://www.startvbdotnet.com/controls/printdialog1.aspx
However, the code as presented on the above site is broken (there is part of a line missing) and I can't reconstruct his code.
Can anyone suggest a method providing a printout without text being clipped? The text should ust wrap to the next line.
Many thanks
Mark Wills UK
To get printing to wordwrap you need to pass an fRectange and string.format
to the e.graphics.drawstring.
Private Sub PrintDocument1_PrintPage1(ByVal sender As Object, _
ByVal e As System.Drawing.Printing.PrintPageEventArgs) _
Handles PrintDocument1.PrintPage
Dim charactersOnPage As Integer = 0
Dim linesPerPage As Integer = 0
' Sets the value of charactersOnPage to the number of characters
' of stringToPrint that will fit within the bounds of the page.
e.Graphics.MeasureString(stringToPrint, myfont, e.MarginBounds.Size, _
StringFormat.GenericTypographic, charactersOnPage, linesPerPage)
' Draws the string within the bounds of the page
e.Graphics.DrawString(stringToPrint, myfont, Brushes.Black, _
e.MarginBounds, StringFormat.GenericTypographic)
' Remove the portion of the string that has been printed.
stringToPrint = stringToPrint.Substring(charactersOnPage)
' Check to see if more pages are to be printed.
e.HasMorePages = stringToPrint.Length > 0
End Sub
'See also:
ms-help://MS.VSExpressCC.v80/MS.NETFramework.v20.en/cpref8/html/M_System_Drawing_Graphics_DrawString_1_8c429b2b.htm
http://www.msusenet.com/f-dotnet-languages-csharp-13/t-word-wrap-while-printing-1801348.html