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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Does anyone know anything about printing?

Status
Not open for further replies.

aspro

Programmer
Jan 22, 2003
69
AU
Trust me I have been looking for days for something to help with my printing problem.
All I want to do is find out how I can word wrap but no one seems to know.
My app receives strings from a database and I want to print them all out however some of them are longer than the page width and when this happens it just prints whatever fits on the page and cuts the rest.

I really need some help on this problem. Every example I have seen only deals with strings that are shorter than one line.

Many thanks,
aspro
 
You can find the length of the sting that fits the page. E.g.
Say you assign the sting to be printed to the variable String1 and the max length to 100, use:

If String1.length > 100 Then
'take all the characters after 100 and print them on a new line
....
End If

 
HI
it will do what u want,let me know if it works for u
Nouman
Imports System.IO
Imports System.Drawing.Printing 'For printing

Private Sub btnPrintLog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrintLog.Click
Dim strFile As String
PrintLogFile(strFile) 'File path to print
End Sub
' The PrintPage event is raised for each page to be printed.
Private Sub pd_PrintPage(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
Dim linesPerPage As Single = 0
Dim yPos As Single = 0
Dim count As Integer = 0
Dim leftMargin As Single = ev.MarginBounds.Left
Dim topMargin As Single = ev.MarginBounds.Top
Dim line As String = Nothing

' Calculate the number of lines per page.
linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics)

' Iterate over the file, printing each line.
While count < linesPerPage
line = streamToPrint.ReadLine()
If line Is Nothing Then
Exit While
End If
yPos = topMargin + count * printFont.GetHeight(ev.Graphics)
ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, _
yPos, New StringFormat())
count += 1
End While

' If more lines exist, print another page.
If Not (line Is Nothing) Then
ev.HasMorePages = True
Else
ev.HasMorePages = False
End If
End Sub

' Print the file.
Public Sub PrintLogFile(ByVal filePath As String)
Try
streamToPrint = New StreamReader(filePath)
Try
printFont = New Font(&quot;Arial&quot;, 10)
Dim pd As New PrintDocument()
AddHandler pd.PrintPage, AddressOf pd_PrintPage
' Print the document.
pd.Print()
Finally
streamToPrint.Close()
End Try
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub 'Printing
 
To nomi2000
Thankyou for the help, but unfortunately you code does not solve my problem of word wrapping. It still prints off the page.

and to AP81
I did think of that idea however the problem was how do I find out if character 100 is in the middle of a word or not? I can't split the string mid-word. But thank you for trying. I guess I cannot print from .net.

aspro
 
This is the class module which i have will do what u want i implement it in VB 6.0 you have to check if it works with .Net it will all you have to do is implements libraary if needede
Nouman
Option Explicit

'Default margin values
Private Const MARGIN_LEFT = 720
Private Const MARGIN_RIGHT = 720
Private Const MARGIN_TOP = 1440
Private Const MARGIN_BOTTOM = 1440

'Modifiable parameters
Private m_HeaderText As String
Private m_MarginLeft As Single
Private m_MarginTop As Single
Private m_MarginRight As Single
Private m_MarginBottom As Single

Public Property Let HeaderText(sText As String)
m_HeaderText = sText
End Property

Public Property Get HeaderText() As String
HeaderText = m_HeaderText
End Property

'***
'*** Note: Margins are only close approximations
'*** and do not take into account the edge of the
'*** page that the printer cannot print to. For
'*** more precise control over margin sizes, use
'*** GetDeviceCaps to determine the physical page
'*** offsets and adjust margins accordingly.
'***
Public Property Let MarginLeft(nMargin As Single)
m_MarginLeft = nMargin
End Property

Public Property Get MarginLeft() As Single
MarginLeft = m_MarginLeft
End Property

Public Property Let MarginTop(nMargin As Single)
m_MarginTop = nMargin
End Property

Public Property Get MarginTop() As Single
MarginTop = m_MarginTop
End Property

Public Property Let MarginRight(nMargin As Single)
m_MarginRight = nMargin
End Property

Public Property Get MarginRight() As Single
MarginRight = m_MarginRight
End Property

Public Property Let MarginBottom(nMargin As Single)
m_MarginBottom = nMargin
End Property

Public Property Get MarginBottom() As Single
MarginBottom = m_MarginBottom
End Property

'Public method to print text
Public Sub PrintText(sText As String)
Dim i As Integer, j As Integer, sCurrWord As String

Screen.MousePointer = vbHourglass
'Initialize first page
DoNewPage False
'Print text, word-wrapping as we go
i = 1
Do Until i > Len(sText)
'Get next word
sCurrWord = &quot;&quot;
Do Until i > Len(sText) Or Mid$(sText, i, 1) <= &quot; &quot;
sCurrWord = sCurrWord & Mid$(sText, i, 1)
i = i + 1
Loop
'Check if word will fit on this line
If (Printer.CurrentX + Printer.TextWidth(sCurrWord)) > (Printer.ScaleWidth - m_MarginRight) Then
'Send carriage-return line-feed to printer
Printer.Print
'Check if we need to start a new page
If Printer.CurrentY > (Printer.ScaleHeight - m_MarginBottom) Then
DoNewPage
Else
Printer.CurrentX = m_MarginLeft
End If
End If
'Print this word
Printer.Print sCurrWord;
'Process whitespace and any control characters
Do Until i > Len(sText) Or Mid$(sText, i, 1) > &quot; &quot;
Select Case Mid$(sText, i, 1)
Case &quot; &quot; 'Space
Printer.Print &quot; &quot;;
Case Chr$(10) 'Line-feed
'Send carriage-return line-feed to printer
Printer.Print
'Check if we need to start a new page
If Printer.CurrentY > (Printer.ScaleHeight - m_MarginBottom) Then
DoNewPage
Else
Printer.CurrentX = m_MarginLeft
End If
Case Chr$(9) 'Tab
j = (Printer.CurrentX - MARGIN_LEFT) / Printer.TextWidth(&quot;0&quot;)
j = j + (10 - (j Mod 10))
Printer.CurrentX = MARGIN_LEFT + (j * Printer.TextWidth(&quot;0&quot;))
Case Else 'Ignore other characters
End Select
i = i + 1
Loop
Loop
Printer.EndDoc
Screen.MousePointer = vbDefault
End Sub

'Prints page header and footer
Private Sub DoNewPage(Optional bEjectPage As Boolean = True)
Dim Buff As String

'Start new page if requested
If bEjectPage Then
Printer.NewPage
End If
'Print page header
Printer.CurrentY = (m_MarginTop - Printer.TextHeight(m_HeaderText)) / 2
Printer.CurrentX = (Printer.ScaleWidth - Printer.TextWidth(m_HeaderText)) / 2
Printer.Print m_HeaderText;
'Print page footer
Buff = Format$(Date$, &quot;Short Date&quot;)
Printer.CurrentX = m_MarginLeft
Printer.CurrentY = Printer.ScaleHeight - (m_MarginBottom / 2)
Printer.Print Buff;
Buff = &quot;Page &quot; & CStr(Printer.Page)
Printer.CurrentX = Printer.ScaleWidth - (Printer.TextWidth(Buff) + m_MarginRight)
Printer.CurrentY = Printer.ScaleHeight - (m_MarginBottom / 2)
Printer.Print Buff;
'Reset position to top of page
Printer.CurrentX = m_MarginLeft
Printer.CurrentY = m_MarginTop
End Sub

Private Sub Class_Initialize()
'Set default properties
m_MarginLeft = MARGIN_LEFT
m_MarginRight = MARGIN_RIGHT
m_MarginTop = MARGIN_TOP
m_MarginBottom = MARGIN_BOTTOM
m_HeaderText = &quot;CWordWrap Class&quot;
End Sub
 
Thanks for the code. I am using part of it for my app. I have one more little problem though. My pageBounds are wrong. The width is 850 when really the page width is about 795. DO you have any idea why it would be wrong and how I can change it?

Many thanks,
aspro
 
HI
aspro
you can use to defint the rectangular area of your page
in
Private Sub pd_PrintPage(ByVal sender As Object, ByVal ev As PrintPageEventArgs)

ev.PageBounds.FromLTRB(0, 0, 795, 100)

End Sub

Regards
Nouman
 
I tried setting the marginbounds but it didn't seem to make a difference. It still just prints right from the edges of the page.
There just must be something I cannot see.
Here is my code. sorry it is long



Imports System.Drawing.Printing
Public Class testform
Inherits System.Windows.Forms.Form
Private WithEvents pd As New PrintDocument
Private mymargins As New_ System.Drawing.Printing.Margins(46, 46, 50, 50)
Private myQarray() As String
Private myAarray() As String
Private ev As System.Drawing.Graphics


Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click




Try
printsettings()
AddHandler pd.PrintPage, AddressOf _ Me.pd_PrintPage
pd.Print()

Catch ex As Exception
MessageBox.Show(&quot;An error occurred - &quot; +_ ex.Message)
End Try


End Sub

Private Sub pd_PrintPage(ByVal sender As Object, ByVal_ e As PrintPageEventArgs)

Dim tab As Integer = 100
Dim sos As SizeF
Dim i As Int32
Dim counter As Integer = 0
Dim xpos As Integer = 0
Dim ypos As String = 0
Dim testxpos As Integer = 0
Dim lastposQ As Integer = 300
Dim linecount As Integer = 0
Dim linepp As Integer = 0
Dim currword As String
Dim middle As Integer
Dim space As String = &quot; &quot;
Dim spacesize As Int32
Dim enter As String = Chr(10)
Dim hasmore As Boolean = True
Dim endofstring As Boolean = False
Dim pagecount As Integer = 0
Dim topMargin As Single = e.MarginBounds.Top
Dim drawfont As New Font(&quot;miriam fixed&quot;, 12)
Dim headingfont As New Font(&quot;miriam fixed&quot;, 20)
Dim heading As String = &quot;The Heading&quot;
Dim drawbrush As New System.Drawing.SolidBrush_(System.Drawing.Color.Black)
Dim Qline = &quot;Today I went to the zoo and I saw the_ lions&quot;
ev = Me.CreateGraphics

e.PageBounds.FromLTRB(46, 100, 700, 700)
pd.OriginAtMargins = True
sos = ev.MeasureString(space, drawfont)
spacesize = Convert.ToInt32(sos.Width)

'get heading and put that in the middle
middle = (e.MarginBounds.Size.Width / 2) - _(heading.Length / 2)
'middle = (793 / 2) - (heading.Length / 2)
topMargin = topMargin + headingfont.GetHeight_(e.Graphics)
e.Graphics.DrawString(heading, headingfont,_ drawbrush, middle, ypos)


'split strings
myQarray = Split(Qline)


'find out how many lines per page
If (pagecount = 0) Then
linepp = (e.MarginBounds.Height - headingfont.GetHeight(e.Graphics)) / drawfont.GetHeight_(e.Graphics)
Else
linepp = e.MarginBounds.Height /_ drawfont.GetHeight(e.Graphics)
End If

While (linecount < linepp And counter < (myQarray.Length))
ypos = topMargin + (linecount * drawfont.GetHeight(e.Graphics))
currword = myQarray(counter)
If Not hasmore Then
counter = 0
End If

If xpos < 300 Then
sos = ev.MeasureString(currword, drawfont)
i = Convert.ToInt32(sos.Width)
testxpos = i + xpos
If (testxpos < 300) Then
e.Graphics.DrawString(currword, _ drawfont, drawbrush, xpos, ypos)
counter = counter + 1
xpos = testxpos
e.Graphics.DrawString(&quot; &quot;, drawfont,_ drawbrush, xpos, ypos)
xpos = xpos + spacesize
ElseIf counter < myQarray.Length Then
e.Graphics.DrawString(enter, drawfont,_ drawbrush, xpos, ypos)
hasmore = True
linecount = linecount + 1
xpos = 0
End If
End If

End While

End Sub

Private Sub printsettings()
'sets magins, orientation and colour to false
pd.DefaultPageSettings.Landscape = False
pd.DefaultPageSettings.Margins = mymargins
pd.DocumentName = &quot;Danni's Form&quot;

If pd.PrinterSettings.SupportsColor Then
pd.DefaultPageSettings.Color = False
End If
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top