setting up margin in vb.net while printing

1.5k views Asked by At

i am developing a school management software in which i have given a small handy functionality of a mini notepad so that it can make notice or doc within the software. I have taken a RichTextBox for this, Now my problem is that when i enter text in richtextbox without giving space in between ( eg. aaaaaaaa..........) continous for 2 lines and when i click on PrintPreview, it leave some space from it start displaying on left but the text leaves the page from right side. What i want is that i should a margin on both the side i.e. left & right side.

Below is my code on Print Document Click

Private Sub PrintDocument1_PrintPage_1(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage

    Dim font1 As New Font("Arial", 12, FontStyle.Regular)

    'Dim formatF As StringFormat = StringFormat.GenericDefault
    'formatF.Alignment = StringAlignment.Center
    'formatF.LineAlignment = StringAlignment.Center


    e.Graphics.DrawString(RichTextBox1.Text, font:=font1, brush:=Brushes.Black, x:=10, y:=50)


End Sub

So it basically do is represented in below images. Please have a look.

Text in richtextbox

Image of print preview

1

There are 1 answers

1
Chris Dunaway On BEST ANSWER

To print a string within a certain area of the page, specify a Rectangle which should contain the string.

Imports System.Drawing.Printing

Public Class Form1

    Dim pd As PrintDocument

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        pd = New PrintDocument()
        AddHandler pd.PrintPage, AddressOf pd_PrintPage


    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim preview = New PrintPreviewDialog()
        preview.Document = pd

        preview.ShowDialog()
    End Sub

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

        'Create a long sample string
        Dim s As New String("a"c, 2048)

        'Create a rectangle which describes the area where the string
        'should print
        Dim r As New Rectangle(50, 50, 500, 500)

        'Draw the string
        e.Graphics.DrawString(s, Me.Font, Brushes.Black, r)

        'Since there is no more data to print, set to False
        e.HasMorePages = False
    End Sub

End Class