JayBeeOH

TxtEditor - Printing - BeginPrint & PrintPage Events

Apr 18th, 2015
438
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 8.58 KB | None | 1 0
  1. '------------------------------------------------------------------------------------------
  2. '           Notice of My Copyright and Intellectual Property Rights
  3. '
  4. ' Any intellectual property contained within the program by Joseph L. Bolen remains the
  5. ' intellectual property of the Joseph L. Bolen. This means that no person may distribute,
  6. ' publish or provide such intellectual property to any other person or entity for any
  7. ' reason, commercial or otherwise, without the express written permission of Joseph L. Bolen.
  8. '
  9. '                 Copyright © 2015. All rights reserved.
  10. '        All trademarks remain the property of their respective owners.
  11. '-------------------------------------------------------------------------------------------
  12. ' Program Name:   Text Editor
  13. '
  14. ' Author:         Joseph L. Bolen
  15. ' Date Created:   Feb 2015
  16. '
  17. ' Description:    Simple plain text editor program with printing ability.
  18. '
  19. '                 Documentation is at:
  20. '                   App's screen image is at: http://imgur.com/TzWs9kO
  21. '                   App's screen design time specs at: http://pastebin.com/bhinRQ5d
  22. '                   App's Visual Basic .NET Event Procedures & TODO's is at
  23. '                       http://pastebin.com/UZ4tcEpE
  24. '                   App's Visual Basic .NET code is at http://pastebin.com/u/jaybeeoh
  25. '                   Video tutorial at YouTube: http://www.youtube.com/user/bolenpresents
  26. '-------------------------------------------------------------------------------------------
  27.  
  28. Imports System.Drawing.Printing
  29.  
  30. Public Class MainForm
  31.  
  32. #Region "  Declare class level variables"
  33.  
  34.     Private workingPathFile As String = String.Empty
  35.     Private fromPageNumber As Integer = 0
  36.     Private toPageNumber As Integer = 9999
  37.  
  38. #End Region
  39.  
  40. #Region "  Printing"
  41.  
  42.     ' File Print option
  43.     Private Sub PrintToolStripMenuItem_Click(sender As Object, e As EventArgs) _
  44.         Handles PrintToolStripMenuItem.Click,
  45.                 PrintToolStripButton.Click
  46.  
  47.         ' For full code of this procedure, see < http://pastebin.com/W8q7yUwF >.
  48.  
  49.                 PrintDocument1.Print()
  50.     End Sub
  51.  
  52.     Private Sub PrintDocument1_BeginPrint(sender As Object, e As Printing.PrintEventArgs) _
  53.         Handles PrintDocument1.BeginPrint
  54.  
  55.         If String.IsNullOrWhiteSpace(MyTextBox.Text) Then
  56.             e.Cancel = True
  57.         Else
  58.             ' Determine pages to print.
  59.             With PrintDocument1.PrinterSettings
  60.                 If .PrintRange = Printing.PrintRange.SomePages Then
  61.                     fromPageNumber = .FromPage
  62.                     toPageNumber = .ToPage
  63.                 Else
  64.                     fromPageNumber = 0
  65.                     toPageNumber = 9999
  66.                 End If
  67.             End With
  68.         End If
  69.     End Sub
  70.  
  71.     ' Print each page of document, one page at a time.
  72.     Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) _
  73.         Handles PrintDocument1.PrintPage
  74.  
  75.         ' Set up actual output to print.
  76.         Static currentChar As Integer
  77.         Static currentLine As Integer
  78.         Static pageNumber As Integer = 1
  79.  
  80.         Dim leftMargin As Integer
  81.         Dim topMargin As Integer
  82.         Dim printableAreaWidth As Integer
  83.         Dim printableAreaHeight As Integer
  84.         Dim hdrLeft As Integer
  85.         Dim hdrRight As Integer
  86.         Dim fmt As StringFormat
  87.  
  88.         Dim hdrFont As New Font("Ariel", 14)
  89.         Dim dtlFont As Font = MyTextBox.Font
  90.         Dim dtlBrush = New SolidBrush(MyTextBox.ForeColor)
  91.  
  92.         ' Determine printable area
  93.         With Me.PrintDocument1.DefaultPageSettings
  94.             If .Landscape Then
  95.                 topMargin = .Margins.Top
  96.                 leftMargin = .Margins.Left
  97.                 printableAreaWidth = (.PaperSize.Height - (.Margins.Left + .Margins.Right))
  98.                 printableAreaHeight = (.PaperSize.Width - (.Margins.Top + .Margins.Bottom))
  99.                 hdrLeft = 50
  100.                 hdrRight = .PaperSize.Height - 145
  101.             Else
  102.                 ' Portrait
  103.                 topMargin = .Margins.Top
  104.                 leftMargin = .Margins.Left
  105.                 printableAreaWidth = (.PaperSize.Width - (.Margins.Left + .Margins.Right))
  106.                 printableAreaHeight = (.PaperSize.Height - (.Margins.Top + .Margins.Bottom))
  107.                 hdrLeft = 50
  108.                 hdrRight = .PaperSize.Width - 145
  109.             End If
  110.         End With
  111.  
  112.         ' Draw a red boarder. Activate for testing only.
  113.         'e.Graphics.DrawRectangle(New Pen(Brushes.Red, 3), e.MarginBounds)
  114.  
  115.         ' Setup Print Header - Filename and Page Number.
  116.         Dim headerTitle As String = String.Empty
  117.         If workingPathFile <> String.Empty Then
  118.             headerTitle = Path.GetFileNameWithoutExtension(workingPathFile)
  119.         End If
  120.  
  121.         ' Replace tabs with spaces.
  122.         Dim tempString As String = Replace(MyTextBox.Text, ControlChars.Tab, Space(8))
  123.  
  124.         ' Setup Print Body
  125.         If MyTextBox.WordWrap Then
  126.             fmt = New StringFormat(StringFormatFlags.LineLimit)
  127.             Dim chars As Integer
  128.             Dim lines As Integer
  129.  
  130.             Do
  131.                 ' Page Header
  132.                 If pageNumber >= fromPageNumber Then
  133.                     e.Graphics.DrawString(headerTitle, hdrFont, Brushes.Black, hdrLeft, 50)
  134.                     e.Graphics.DrawString("Page " & pageNumber.ToString(), hdrFont,
  135.  
  136. Brushes.Black, hdrRight, 50)
  137.                 End If
  138.  
  139.                 ' Page Body
  140.                 e.Graphics.MeasureString(Mid(tempString, currentChar + 1),
  141.                                          dtlFont,
  142.                                          New Size(printableAreaWidth, printableAreaHeight),
  143.                                          fmt, chars, lines)
  144.                 If currentChar + chars < tempString.Length Then
  145.                     If tempString.Substring(currentChar + chars, 1) <> " " AndAlso
  146.                         tempString.Substring(currentChar + chars, 1) <> ControlChars.Lf Then
  147.                         While chars > 0 AndAlso
  148.                             tempString.Substring(currentChar + chars, 1) <> " " AndAlso
  149.                             tempString.Substring(currentChar + chars, 1) <> ControlChars.Lf
  150.                             chars -= 1
  151.                         End While
  152.                         chars += 1
  153.                     End If
  154.                 End If
  155.  
  156.                 If pageNumber >= fromPageNumber Then
  157.                     e.Graphics.DrawString(tempString.Substring(currentChar, chars), dtlFont,
  158.  
  159. dtlBrush, e.MarginBounds, fmt)
  160.                 End If
  161.  
  162.                 currentChar += chars
  163.                 pageNumber += 1
  164.             Loop While (pageNumber < fromPageNumber + 1)
  165.  
  166.             If (currentChar < tempString.Length) AndAlso (pageNumber <= toPageNumber) Then
  167.                 e.HasMorePages = True
  168.             Else
  169.                 e.HasMorePages = False
  170.                 currentChar = 0
  171.                 pageNumber = 1
  172.             End If
  173.         Else
  174.             'No WordWrap
  175.             fmt = New StringFormat(StringFormatFlags.NoWrap)
  176.             fmt.Trimming = StringTrimming.EllipsisWord
  177.             Dim linesperpage As Integer = CInt(Math.Round(printableAreaHeight /
  178.  
  179. (dtlFont.Height + 0.025)))
  180.  
  181.             Do
  182.                 ' Page Header
  183.                 If pageNumber >= fromPageNumber Then
  184.                     e.Graphics.DrawString(headerTitle, hdrFont, Brushes.Black, hdrLeft, 50)
  185.                     e.Graphics.DrawString("Page " & pageNumber.ToString(), hdrFont,
  186.  
  187. Brushes.Black, hdrRight, 50)
  188.                 End If
  189.  
  190.                 For idx As Integer = currentLine To Math.Min(currentLine + linesperpage,
  191.  
  192. MyTextBox.Lines.Length - 1)
  193.                     If pageNumber >= fromPageNumber Then
  194.                         e.Graphics.DrawString(MyTextBox.Lines(idx),
  195.                                               dtlFont, dtlBrush,
  196.                                               New RectangleF(leftMargin, topMargin +
  197.  
  198. dtlFont.Height * (idx - currentLine), printableAreaWidth, dtlFont.Height),
  199.                                               fmt)
  200.                     End If
  201.                 Next
  202.  
  203.                 currentLine += linesperpage
  204.                 pageNumber += 1
  205.             Loop While (pageNumber < fromPageNumber + 1)
  206.  
  207.             If (currentLine < MyTextBox.Lines.Length) AndAlso (pageNumber <= toPageNumber)
  208.  
  209. Then
  210.                 e.HasMorePages = True
  211.             Else
  212.                 e.HasMorePages = False
  213.                 currentLine = 0
  214.                 pageNumber = 1
  215.             End If
  216.         End If
  217.     End Sub
  218.  
  219. #End Region
Advertisement
Add Comment
Please, Sign In to add comment