无法在vb.net中打印文档

问题描述 投票:0回答:1

我正在从事一个必须打印标记表的项目。我正在使用以下代码。该代码在我的系统中运行良好,但是当我尝试在客户端系统上打印时,它无法正确打印,边缘被切割。我使用的是 16 英寸屏幕(笔记本电脑),客户端使用的是 20 英寸显示器。

Dim printDoc As New PrintDocument()
printDoc.DefaultPageSettings.PaperSize = New PaperSize("A4", 827, 1169)
printDoc.DefaultPageSettings.Landscape = True
AddHandler printDoc.PrintPage, AddressOf PrintPageHandler
' Set print settings
'printDoc.PrinterSettings.PrintQuality = PrintQuality.High

printDoc.Print()
Me.FormBorderStyle = FormBorderStyle.Sizable

Printbtn.Visible = True
Prevbtn.Visible = True
Nextbtn.Visible = True
Firstbtn.Visible = True
Lastbtn.Visible = True
Private Sub PrintPageHandler(sender As Object, e As PrintPageEventArgs)
    Dim formBitmap As New Bitmap(Me.Width, Me.Height, Imaging.PixelFormat.Format64bppArgb)
    Me.DrawToBitmap(formBitmap, New Rectangle(0, 0, Me.Width, Me.Height))

    ' Draw the bitmap onto the print page
    e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
    e.Graphics.DrawImage(formBitmap, 0, 0)

    ' Clean up
    formBitmap.Dispose()

    e.HasMorePages = False
End Sub
vb.net visual-studio printing
1个回答
0
投票

绘制位图时,将其缩放以适合打印区域。

    Dim printArea As RectangleF = e.MarginBounds

    Dim scaleX As Single = printArea.Width / formBitmap.Width
    Dim scaleY As Single = printArea.Height / formBitmap.Height
    Dim scale As Single = Math.Min(scaleX, scaleY) ' Scale proportionally

    Dim newWidth As Integer = CInt(formBitmap.Width * scale)
    Dim newHeight As Integer = CInt(formBitmap.Height * scale)

    Dim xPos As Integer = printArea.Left + (printArea.Width - newWidth) / 2
    Dim yPos As Integer = printArea.Top + (printArea.Height - newHeight) / 2
© www.soinside.com 2019 - 2024. All rights reserved.