JayBeeOH

TxtEditor - File Printing Overview

Apr 10th, 2015
420
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 5.16 KB | None | 0 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.     ' File Print option
  31.     Private Sub PrintToolStripMenuItem_Click(sender As Object, e As EventArgs) _
  32.         Handles PrintToolStripMenuItem.Click, PrintToolStripButton.Click
  33.  
  34.         Try
  35.             PrintDocument1.Print()
  36.         Catch ex As InvalidPrinterException
  37.             MessageBox.Show(ex.Message, "Invalid Printer",
  38.                             MessageBoxButtons.OK,
  39.                             MessageBoxIcon.Error)
  40.         Catch ex As Exception
  41.             MessageBox.Show("There was a problem printing the file." &
  42.                             ControlChars.NewLine & ex.Message,
  43.                            "Printing File Error",
  44.                            MessageBoxButtons.OK,
  45.                            MessageBoxIcon.Error)
  46.         End Try
  47.     End Sub
  48.  
  49.     ' Print each page of document, one page at a time.
  50.     Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) _
  51.         Handles PrintDocument1.PrintPage
  52.  
  53.         ' Set up actual output to print.
  54.         Static pageNumber As Integer = 1
  55.  
  56.         Dim leftMargin As Integer
  57.         Dim topMargin As Integer
  58.         Dim printableAreaWidth As Integer
  59.         Dim printableAreaHeight As Integer
  60.         Dim hdrLeft As Integer
  61.         Dim hdrRight As Integer
  62.  
  63.         Dim hdrFont As New Font("Ariel", 14)
  64.         Dim dtlFont As New Font("Tahoma", 24)
  65.         Dim dtlBrush As New SolidBrush(Color.Navy)
  66.  
  67.         ' Determine printable area
  68.         With Me.PrintDocument1.DefaultPageSettings
  69.             If .Landscape Then
  70.                 topMargin = .Margins.Top
  71.                 leftMargin = .Margins.Left
  72.                 printableAreaWidth = (.PaperSize.Height - (.Margins.Left + .Margins.Right))
  73.                 printableAreaHeight = (.PaperSize.Width - (.Margins.Top + .Margins.Bottom))
  74.                 hdrLeft = 50
  75.                 hdrRight = .PaperSize.Height - 145
  76.             Else
  77.                 ' Portrait
  78.                 topMargin = .Margins.Top
  79.                 leftMargin = .Margins.Left
  80.                 printableAreaWidth = (.PaperSize.Width - (.Margins.Left + .Margins.Right))
  81.                 printableAreaHeight = (.PaperSize.Height - (.Margins.Top + .Margins.Bottom))
  82.                 hdrLeft = 50
  83.                 hdrRight = .PaperSize.Width - 145
  84.             End If
  85.         End With
  86.  
  87.         ' Draw a red boarder. Activate for testing only.
  88.         e.Graphics.DrawRectangle(New Pen(Brushes.Red, 3), e.MarginBounds)
  89.  
  90.         ' Setup Print Header - Filename and Page Number.
  91.         Dim headerTitle As String = "Text Editor Test Printing"
  92.  
  93.         ' Setup Print Body
  94.         Dim fmt = New StringFormat(StringFormatFlags.NoWrap)
  95.         fmt.Trimming = StringTrimming.EllipsisWord
  96.         Dim linesperpage As Integer = CInt(Math.Round(printableAreaHeight / (dtlFont.Height + 0.025)))
  97.  
  98.         ' Page Header
  99.         e.Graphics.DrawString(headerTitle, hdrFont, Brushes.Black, hdrLeft, 50)
  100.         e.Graphics.DrawString("Page " & pageNumber.ToString(), hdrFont, Brushes.Black, hdrRight, 50)
  101.  
  102.         ' Page Body
  103.         For line = 0 To (linesperpage - 1)
  104.             e.Graphics.DrawString("This is line " & (line + 1).ToString & " of the document.",
  105.                                   dtlFont,
  106.                                   dtlBrush,
  107.                                   New RectangleF(leftMargin, topMargin + dtlFont.Height * (line), printableAreaWidth, dtlFont.Height),
  108.                                   fmt)
  109.         Next
  110.  
  111.         pageNumber += 1
  112.  
  113.         If (pageNumber < 1) Then
  114.             e.HasMorePages = True
  115.         Else
  116.             e.HasMorePages = False
  117.             pageNumber = 1
  118.         End If
  119.  
  120.     End Sub
Advertisement
Add Comment
Please, Sign In to add comment