如何打印带有文本框的文档? (vB)

时间:2019-02-17 05:09:32

标签: vb.net printing textbox

所以我一直在尝试打印一个文档,其中文本框显示在图片框的顶部,但是似乎不起作用。

Imports System.Drawing.Printing

Public Class Form1
Dim WithEvents mPrintDocument As New PrintDocument
Dim mPrintBitMap As Bitmap

Private Sub m_PrintDocument_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles mPrintDocument.PrintPage
    ' Draw the image centered.
    Dim lWidth As Integer = e.MarginBounds.X + (e.MarginBounds.Width / 0.95 - mPrintBitMap.Width) \ 1
    Dim lHeight As Integer = e.MarginBounds.Y + (e.MarginBounds.Height / 0.9 - mPrintBitMap.Height) \ 2
    e.Graphics.DrawImage(mPrintBitMap, lWidth, lHeight)

    ' There's only one page.
    e.HasMorePages = False
End Sub


    Private Sub btnPrint_Click(sender As Object, e As EventArgs) Handles btnPrint.Click
    picFij.SendToBack()
    lblDN.BringToFront()

    mPrintBitMap = New Bitmap(Me.Width, Me.Width)
    Dim lRect As System.Drawing.Rectangle
    lRect.Width = Me.Width
    lRect.Height = Me.Width
    Me.DrawToBitmap(mPrintBitMap, lRect)



    mPrintDocument = New PrintDocument
    printPreviewDialog1.Document = mPrintDocument
    PrintPreviewDialog1.ShowDialog()

End Sub   

我尝试了BringToFront()和SendToBack(),但是没有用。

这是我要打印的内容: https://cdn.discordapp.com/attachments/358502382910570497/546555282940100648/unknown.png

这是打印预览 https://cdn.discordapp.com/attachments/358502382910570497/546555621806178324/unknown.png

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

将PictureBox设为TextBox的父级,然后在调用DrawToBitmap()时显示它。例如,要将TextBox1保留在同一位置,请转换它的屏幕坐标,然后相对于PictureBox返回到客户坐标:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim pt As Point = TextBox1.PointToScreen(New Point(0, 0))
    TextBox1.Parent = PictureBox1
    TextBox1.Location = PictureBox1.PointToClient(pt)
End Sub