Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is my code for printing, print preview, and print doc.

Everything works to a point.

My problem is how do I keep the words on the page from staying within the margin. With this source code the words bleed over the margins on the right,
this is a windows form application.

Thank you

What I have tried:

Private Sub TSPrint_Click(sender As Object, e As EventArgs) Handles TSPrint.Click
    PrintDialog1.Document = PrintDocument1
    If PrintDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
        PrintDocument1.Print()
    End If
End Sub

Private Sub PrintPreviewToolStripMenuItem2_Click(sender As Object, e As EventArgs) Handles PrintPreviewToolStripMenuItem2.Click
    PrintPreviewDialog1.Document = PrintDocument1
    PrintPreviewDialog1.ShowDialog()
End Sub

Private Sub PageSetupToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles PageSetupToolStripMenuItem.Click
    PageSetupDialog1.Document = PrintDocument1
    PageSetupDialog1.Document.DefaultPageSettings.Color = False
    PageSetupDialog1.ShowDialog()
End Sub
Posted
Updated 9-Jul-23 2:53am
v2

1 solution

You need to use the 'DefaultPageSettings.Margins' method, full explanation and sample code at - PageSettings.Margins Property[^]

Your code will look similar to -
VB
Private Sub TSPrint_Click(sender As Object, e As EventArgs) Handles TSPrint.Click
    PrintDialog1.Document = PrintDocument1
    If PrintDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
        ' Set the margins for the PrintDocument
        PrintDocument1.DefaultPageSettings.Margins = New System.Drawing.Printing.Margins(50, 50, 50, 50) 
' Adjust the values (50,50,50,50) to fit into your margins you need
' The values is left, right, top, and bottom margins
        PrintDocument1.Print()
    End If
End Sub


Make sure to update your 'PrintDocument1.PrintPage' event handler as well to account for the newly set margins when drawing the content to be printed that will handle the text that exceeds the available space within the margins by wrapping it to a new line.

VB
Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    Dim graphics As Graphics = e.Graphics
    Dim font As New Font("Arial", 12)
    Dim brush As New SolidBrush(Color.Black)
    Dim leftMargin As Integer = e.MarginBounds.Left
    Dim topMargin As Integer = e.MarginBounds.Top

    'Text to be printed
    Dim text As String = "This is my text I want to print which might exceeds the right margin if I am not carefull, with this code I can wrap the long text to a new line without it being cutt off at the end................!"

    ' Calculate the remaining space within the margins
    Dim availableWidth As Integer = e.MarginBounds.Width
    Dim availableHeight As Integer = e.MarginBounds.Height

    ' Measure the text size
    Dim textSize As SizeF = graphics.MeasureString(text, font)

    ' Check if the text fits within the available space
    If textSize.Width <= availableWidth AndAlso textSize.Height <= availableHeight Then
        ' The text fits within the available space, so print it
        graphics.DrawString(text, font, brush, leftMargin, topMargin)
    Else
        ' The text does not fit within the available space, truncate or wrap the text
        Dim truncatedText As String = TruncateTextToFit(text, font, availableWidth)
        ' Or
        ' Dim wrappedText As String = WrapTextToFit(text, font, availableWidth, availableHeight)

        graphics.DrawString(truncatedText, font, brush, leftMargin, topMargin)
    End If
End Sub

Private Function TruncateTextToFit(text As String, font As Font, availableWidth As Integer) As String
    ' Calculate the size of the text
    Dim textSize As SizeF = graphics.MeasureString(text, font)

    ' Truncate the text if it exceeds the available width
    While textSize.Width > availableWidth AndAlso text.Length > 0
        text = text.Substring(0, text.Length - 1)
        textSize = graphics.MeasureString(text, font)
    End While

    Return text
End Function
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900