Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Text prints off edge of right margin

Status
Not open for further replies.

MZwijacz

Programmer
Jun 27, 2002
33
US
Disclaimer:
I've check the archives but no answer there.

What I'm trying to do:
I am printing text files to unknown number and type of printers. I've allowed the users to set the margin on all 4 sides. However the right margin does not seem to do anything.

What I would like to do is use the setting for the right margin and truncate the text in the drawstring function. Later I might wordwrap the string. Below is my code. Any Ideas?

Thanks,
MFZ
Code:

Imports System.Drawing.Printing
Imports System.IO
Public Class TextPrint
#Region "Private Variables"

#End Region
Private _TxtPrnRequest As TextPrintRequest
Private _PrintDoc As PrintDocument
Private _Printer As String
Private _Paper As String
Private _PrinterSettings As PrinterSettings
Private _LineNo As Integer = 0
Private _PrintStream As StreamReader
Public Enum TextPrintStatus As Integer
TxtPrintSuccess = 0
TxtPrintInvalidPrinter = 1
TxtPrintInvalidPaper = 2
TxtPrintInvalidPrinterSettings = 3
End Enum
Declare Function GetDeviceCaps% Lib "GDI32" (ByVal hDC%, ByVal nIndex%)

#Region "Properties"

Public Property TxtPrnRequest() As TextPrintRequest
Get
TxtPrnRequest = _TxtPrnRequest
End Get
Set(ByVal Value As TextPrintRequest)
_TxtPrnRequest = Value
End Set
End Property
'Public Property Printer() As String
' Get
' Printer = _Printer
' End Get
' Set(ByVal Value As String)
' _Printer = Value
' End Set
'End Property
'Public Property Paper() As String
' Get
' Paper = _Paper
' End Get
' Set(ByVal Value As String)
' _Paper = Value
' End Set
'End Property

#Region "Private Properties"
Private Property PrintDoc() As PrintDocument
Get
PrintDoc = _PrintDoc
End Get
Set(ByVal Value As PrintDocument)
_PrintDoc = Value
End Set
End Property
Private Property LineNo() As Integer
Get
LineNo = _LineNo
End Get
Set(ByVal Value As Integer)
_LineNo = Value
End Set
End Property
#End Region
#End Region

#Region "Methods"
Public Function Print()
Dim Papers As PrinterSettings.PaperSizeCollection
Dim paper As PaperSize
Dim PrintRes As PrinterResolution
_PrintDoc = New PrintDocument

' _PrinterSettings = New PrinterSettings
Try
'create event handler
AddHandler _PrintDoc.PrintPage, AddressOf Me.PrintHandler
'setup printer
With _PrintDoc
.PrinterSettings.PrinterName = _TxtPrnRequest.PrinterName
.DocumentName = _TxtPrnRequest.DocumentName
_PrintStream = New StreamReader(_TxtPrnRequest.DocumentName)
.PrinterSettings.Copies = _TxtPrnRequest.Copies

'Get the papersizes and match to paper property of request
Papers = .PrinterSettings.PaperSizes
For Each paper In Papers
If paper.PaperName = _TxtPrnRequest.PrinterPaperSize Then
.DefaultPageSettings.PaperSize = paper
Exit For
End If
Next

'Set the Margins the the txtprnrequest
.DefaultPageSettings.Margins.Left = _TxtPrnRequest.MarginLeft * 100
.DefaultPageSettings.Margins.Right = _TxtPrnRequest.MarginRight * 100
.DefaultPageSettings.Margins.Top = _TxtPrnRequest.MarginTop * 100
.DefaultPageSettings.Margins.Bottom = _TxtPrnRequest.MarginBottom * 100

'Set the orientation the txtprnrequest.orientation
If _TxtPrnRequest.Orientation = TextPrintRequest.PageOrientation.Landscape Then
.DefaultPageSettings.Landscape = True
Else
.DefaultPageSettings.Landscape = False
End If
'Set the resolution to high
For Each PrintRes In .DefaultPageSettings.PrinterSettings.PrinterResolutions
If PrintRes.Kind = PrinterResolutionKind.High Then
.DefaultPageSettings.PrinterResolution = PrintRes
Exit For
End If
Next
If .PrinterSettings.IsValid Then
.Print()
_PrintStream.Close()
Return TextPrintStatus.TxtPrintSuccess
End If
End With

Catch ex As Printing.InvalidPrinterException
_PrintStream.Close()
Return TextPrintStatus.TxtPrintInvalidPrinter
Catch ex As Exception
_PrintStream.Close()
Return TextPrintStatus.TxtPrintInvalidPaper
End Try
End Function
#Region "Private Methods"
Private Sub PrintHandler(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
Dim FontFamily As FontFamily
Dim FontStyle As FontStyle
Dim FontSize As Single
Dim PrintFont As Font
Dim LinesPerPage As Single = 0
Dim iCount As Integer = 0
Dim Line As String = String.Empty
Dim FileStream As StreamReader
Dim yPos As Single = 0
Dim TopMargin As Single
Dim LeftMargin As Single

'To fix the margin problem not showing the correct size
'Only do this for printer and not printpreview
'******************************************************
Dim hDC As IntPtr
Dim hardMarginLeft As Integer
Dim hardMarginTop As Integer
Dim hardMarginBottom As Integer

'Calculate the margin spacing not shown in ev.marginbounds
hDC = ev.Graphics.GetHdc()
hardMarginLeft = GetDeviceCaps(hDC.ToInt64, 112)
hardMarginTop = GetDeviceCaps(hDC.ToInt64, 113)
ev.Graphics.ReleaseHdc(hDC)
hardMarginLeft = (hardMarginLeft * 100.0) / ev.Graphics.DpiX
hardMarginTop = (hardMarginTop * 100.0) / ev.Graphics.DpiY
hardMarginBottom = hardMarginTop

'Add the hardmarginbound to top and left
TopMargin = ev.MarginBounds.Top - hardMarginTop
LeftMargin = ev.MarginBounds.Left - hardMarginLeft

'End To fix the margin problem not showing the correct size
'**********************************************************

'Create the font settings
FontFamily = New FontFamily(_TxtPrnRequest.FntFamily)
FontStyle = FontStyle.Parse(FontStyle.GetType, _TxtPrnRequest.FntStyle)
FontSize = _TxtPrnRequest.FntSize
PrintFont = New Font(FontFamily, FontSize, FontStyle, GraphicsUnit.Point)

'Get the lines perpage
LinesPerPage = (ev.MarginBounds.Height - hardMarginBottom) / PrintFont.GetHeight(ev.Graphics)

'Walk through file and printit
'Do While iCount < CSng(LinesPerPage)
Do While _PrintStream.Peek > -1 And iCount < CSng(LinesPerPage)
iCount = iCount + 1

'get line to print
Line = _PrintStream.ReadLine
Debug.WriteLine(Line)

'set the starting point on the page
yPos = TopMargin + (iCount * PrintFont.GetHeight(ev.Graphics))

'Send it to the printer
ev.Graphics.DrawString(Line, PrintFont, Brushes.Black, LeftMargin, yPos, New StringFormat)
Debug.WriteLine(ev.Graphics.MeasureString(Line, PrintFont, yPos, New StringFormat))

'Next page if there is more
If Line <> Nothing Then
ev.HasMorePages = True
Else
ev.HasMorePages = False
End If

Loop


End Sub
#End Region
#End Region
End Class
 
you need to drawstring in rectanglef one of the overloads of drawstring has this option.

Christiaan Baes
Belgium

"Time for a new sig." - Me
 
Ok it looks like its getting more difficult. I've now been asked to wordwrap any lines that try to extend beyond the right margin.

Thanks Chrissie1, I will look at the options with drawstring. Can you point me towards any code snipits ? It's getting kind of hard explaining why it's so difficult to print simple text files.

Thanks,
MFZ
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top