Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '------------------------------------------------------------------------------------------
- ' Notice of My Copyright and Intellectual Property Rights
- '
- ' Any intellectual property contained within the program by Joseph L. Bolen remains the
- ' intellectual property of the Joseph L. Bolen. This means that no person may distribute,
- ' publish or provide such intellectual property to any other person or entity for any
- ' reason, commercial or otherwise, without the express written permission of Joseph L. Bolen.
- '
- ' Copyright © 2015. All rights reserved.
- ' All trademarks remain the property of their respective owners.
- '-------------------------------------------------------------------------------------------
- ' Program Name: Text Editor
- '
- ' Author: Joseph L. Bolen
- ' Date Created: Feb 2015
- '
- ' Description: Simple plain text editor program with printing ability.
- '
- ' Documentation is at:
- ' App's screen image is at: http://imgur.com/TzWs9kO
- ' App's screen design time specs at: http://pastebin.com/bhinRQ5d
- ' App's Visual Basic .NET Event Procedures & TODO's is at
- ' http://pastebin.com/UZ4tcEpE
- ' App's Visual Basic .NET code is at http://pastebin.com/u/jaybeeoh
- ' Video tutorial at YouTube: http://www.youtube.com/user/bolenpresents
- '-------------------------------------------------------------------------------------------
- Imports System.Drawing.Printing
- Public Class MainForm
- #Region " Declare class level variables"
- Private workingPathFile As String = String.Empty
- Private fromPageNumber As Integer = 0
- Private toPageNumber As Integer = 9999
- #End Region
- #Region " Printing"
- ' File Print option
- Private Sub PrintToolStripMenuItem_Click(sender As Object, e As EventArgs) _
- Handles PrintToolStripMenuItem.Click,
- PrintToolStripButton.Click
- ' For full code of this procedure, see < http://pastebin.com/W8q7yUwF >.
- PrintDocument1.Print()
- End Sub
- Private Sub PrintDocument1_BeginPrint(sender As Object, e As Printing.PrintEventArgs) _
- Handles PrintDocument1.BeginPrint
- If String.IsNullOrWhiteSpace(MyTextBox.Text) Then
- e.Cancel = True
- Else
- ' Determine pages to print.
- With PrintDocument1.PrinterSettings
- If .PrintRange = Printing.PrintRange.SomePages Then
- fromPageNumber = .FromPage
- toPageNumber = .ToPage
- Else
- fromPageNumber = 0
- toPageNumber = 9999
- End If
- End With
- End If
- End Sub
- ' Print each page of document, one page at a time.
- Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) _
- Handles PrintDocument1.PrintPage
- ' Set up actual output to print.
- Static currentChar As Integer
- Static currentLine As Integer
- Static pageNumber As Integer = 1
- Dim leftMargin As Integer
- Dim topMargin As Integer
- Dim printableAreaWidth As Integer
- Dim printableAreaHeight As Integer
- Dim hdrLeft As Integer
- Dim hdrRight As Integer
- Dim fmt As StringFormat
- Dim hdrFont As New Font("Ariel", 14)
- Dim dtlFont As Font = MyTextBox.Font
- Dim dtlBrush = New SolidBrush(MyTextBox.ForeColor)
- ' Determine printable area
- With Me.PrintDocument1.DefaultPageSettings
- If .Landscape Then
- topMargin = .Margins.Top
- leftMargin = .Margins.Left
- printableAreaWidth = (.PaperSize.Height - (.Margins.Left + .Margins.Right))
- printableAreaHeight = (.PaperSize.Width - (.Margins.Top + .Margins.Bottom))
- hdrLeft = 50
- hdrRight = .PaperSize.Height - 145
- Else
- ' Portrait
- topMargin = .Margins.Top
- leftMargin = .Margins.Left
- printableAreaWidth = (.PaperSize.Width - (.Margins.Left + .Margins.Right))
- printableAreaHeight = (.PaperSize.Height - (.Margins.Top + .Margins.Bottom))
- hdrLeft = 50
- hdrRight = .PaperSize.Width - 145
- End If
- End With
- ' Draw a red boarder. Activate for testing only.
- 'e.Graphics.DrawRectangle(New Pen(Brushes.Red, 3), e.MarginBounds)
- ' Setup Print Header - Filename and Page Number.
- Dim headerTitle As String = String.Empty
- If workingPathFile <> String.Empty Then
- headerTitle = Path.GetFileNameWithoutExtension(workingPathFile)
- End If
- ' Replace tabs with spaces.
- Dim tempString As String = Replace(MyTextBox.Text, ControlChars.Tab, Space(8))
- ' Setup Print Body
- If MyTextBox.WordWrap Then
- fmt = New StringFormat(StringFormatFlags.LineLimit)
- Dim chars As Integer
- Dim lines As Integer
- Do
- ' Page Header
- If pageNumber >= fromPageNumber Then
- e.Graphics.DrawString(headerTitle, hdrFont, Brushes.Black, hdrLeft, 50)
- e.Graphics.DrawString("Page " & pageNumber.ToString(), hdrFont,
- Brushes.Black, hdrRight, 50)
- End If
- ' Page Body
- e.Graphics.MeasureString(Mid(tempString, currentChar + 1),
- dtlFont,
- New Size(printableAreaWidth, printableAreaHeight),
- fmt, chars, lines)
- If currentChar + chars < tempString.Length Then
- If tempString.Substring(currentChar + chars, 1) <> " " AndAlso
- tempString.Substring(currentChar + chars, 1) <> ControlChars.Lf Then
- While chars > 0 AndAlso
- tempString.Substring(currentChar + chars, 1) <> " " AndAlso
- tempString.Substring(currentChar + chars, 1) <> ControlChars.Lf
- chars -= 1
- End While
- chars += 1
- End If
- End If
- If pageNumber >= fromPageNumber Then
- e.Graphics.DrawString(tempString.Substring(currentChar, chars), dtlFont,
- dtlBrush, e.MarginBounds, fmt)
- End If
- currentChar += chars
- pageNumber += 1
- Loop While (pageNumber < fromPageNumber + 1)
- If (currentChar < tempString.Length) AndAlso (pageNumber <= toPageNumber) Then
- e.HasMorePages = True
- Else
- e.HasMorePages = False
- currentChar = 0
- pageNumber = 1
- End If
- Else
- 'No WordWrap
- fmt = New StringFormat(StringFormatFlags.NoWrap)
- fmt.Trimming = StringTrimming.EllipsisWord
- Dim linesperpage As Integer = CInt(Math.Round(printableAreaHeight /
- (dtlFont.Height + 0.025)))
- Do
- ' Page Header
- If pageNumber >= fromPageNumber Then
- e.Graphics.DrawString(headerTitle, hdrFont, Brushes.Black, hdrLeft, 50)
- e.Graphics.DrawString("Page " & pageNumber.ToString(), hdrFont,
- Brushes.Black, hdrRight, 50)
- End If
- For idx As Integer = currentLine To Math.Min(currentLine + linesperpage,
- MyTextBox.Lines.Length - 1)
- If pageNumber >= fromPageNumber Then
- e.Graphics.DrawString(MyTextBox.Lines(idx),
- dtlFont, dtlBrush,
- New RectangleF(leftMargin, topMargin +
- dtlFont.Height * (idx - currentLine), printableAreaWidth, dtlFont.Height),
- fmt)
- End If
- Next
- currentLine += linesperpage
- pageNumber += 1
- Loop While (pageNumber < fromPageNumber + 1)
- If (currentLine < MyTextBox.Lines.Length) AndAlso (pageNumber <= toPageNumber)
- Then
- e.HasMorePages = True
- Else
- e.HasMorePages = False
- currentLine = 0
- pageNumber = 1
- End If
- End If
- End Sub
- #End Region
Advertisement
Add Comment
Please, Sign In to add comment