打印时在 vb.net 中设置边距

setting up margin in vb.net while printing

我正在开发一个学校管理软件,我在其中提供了一个迷你记事本的小方便功能,以便它可以在软件内制作通知或文档。 我为此使用了 RichTextBox,现在我的问题是,当我在 richtextbox 中输入文本时没有在两者之间给出 space(例如 aaaaaaaa.......)连续 2 行,当我单击 PrintPreview,它会留下一些 space 从它开始显示在左侧,但文本从右侧离开页面。 我想要的是我应该在两侧留出边距,即左侧和右侧。

下面是我的打印文档点击代码

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

所以它基本上是用下面的图片表示的。请看一下。

Text in richtextbox

Image of print preview

要在页面的特定区域内打印字符串,请指定应包含该字符串的 Rectangle

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