Advertisement
7_2009-2012 Miscellaneous #230018

VB.NET Printing a Text File (Using System.Drawing.Printing Namespace)

The code demonstrates how to work with System.Drawing.Printing Namespace

AI

Podsumowanie AI: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Kod źródłowy
original-source
Imports System.IO
Imports System.Drawing
Imports System.Drawing.Printing
Module Module1
 'Create objects
 Private WithEvents MyDoc As PrintDocument
 Private MyFile As StreamReader
 Sub Main()
  'Open a File
  MyFile = File.OpenText("c:\a.txt")
  MyDoc = New PrintDocument()
  MyDoc.Print()
 End Sub
 Private Sub OnPagePrint(ByVal Sender As Object, ByVal arg As System.Drawing.Printing.PrintPageEventArgs) Handles MyDoc.PrintPage
  'Event Fires at every Page has been printed
  Dim sngCurY As Single
  Dim sngLineHeight As Single
  Dim oFont As Font
  oFont = New System.Drawing.Font("Arial", 12)
  sngLineHeight = oFont.GetHeight(arg.Graphics)
  sngCurY = arg.MarginBounds.Top
  If MyFile.Peek() <> -1 Then
   Do
    sngCurY += sngLineHeight
    arg.Graphics.DrawString(MyFile.ReadLine(), oFont, Brushes.Black, arg.MarginBounds.Left, sngCurY)
   Loop Until sngCurY >= arg.MarginBounds.Bottom Or MyFile.Peek() = 1
  End If
  If MyFile.Peek <> -1 Then
   arg.HasMorePages = True
  Else
   arg.HasMorePages = False
  End If
 End Sub
End Module
Oryginalne komentarze (3)
Odzyskane z Wayback Machine