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

时间:2017-01-04 13:25:44

标签: vb.net printing margins

我正在开发一种学校管理软件,我在其中提供了一个迷你记事本的小功能,以便它可以在软件中发出通知或文档。 我已经为此采用了RichTextBox,现在我的问题是,当我在richtextbox中输入文本而没有在它们之间留出空间(例如.aaaaaaaa ..........)连续2行,当我点击PrintPreview时,它会从左边开始显示一些空格,但是文本会从右侧离开页面。 我想要的是我应该在两边都留有余量,即左边和右边。右边。

以下是我在打印文档上的代码点击

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

1 个答案:

答案 0 :(得分:0)

要在页面的某个区域内打印字符串,请指定应包含字符串的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