云计算百科
云计算领域专业知识百科平台

【VB.NET】将标签信息精确打印到81mm×60mm标签纸上:

将标签信息精确打印到81mm×61mm标签纸上:

完整实现代码(TableLayoutPanel版本)

Imports System.Drawing
Imports System.Drawing.Printing
Imports System.Windows.Forms

Public Class LabelPrinterForm
Private WithEvents printDocument As New PrintDocument()
Private labelWidthMM As Integer = 81 ' 标签宽度(毫米)
Private labelHeightMM As Integer = 60 ' 标签高度(毫米)
Private tableLayout As TableLayoutPanel

Public Sub New()
InitializeComponent()

' 初始化打印设置
printDocument.DocumentName = "DL-888T标签打印"
SetupPrinterSettings()

' 创建包含标签内容的TableLayoutPanel
CreateLabelContentTable()
End Sub

' 创建标签内容表格
Private Sub CreateLabelContentTable()
tableLayout = New TableLayoutPanel()
tableLayout.Dock = DockStyle.Fill
tableLayout.ColumnCount = 1
tableLayout.RowCount = 6
tableLayout.CellBorderStyle = TableLayoutPanelCellBorderStyle.None

' 设置行高(像素单位,根据实际调整)
tableLayout.RowStyles.Add(New RowStyle(SizeType.Absolute, 30)) ' 品牌行
tableLayout.RowStyles.Add(New RowStyle(SizeType.Absolute, 25)) ' 型号行
tableLayout.RowStyles.Add(New RowStyle(SizeType.Absolute, 25)) ' 机号行
tableLayout.RowStyles.Add(New RowStyle(SizeType.Absolute, 25)) ' 电源行
tableLayout.RowStyles.Add(New RowStyle(SizeType.Absolute, 40)) ' 警告行
tableLayout.RowStyles.Add(New RowStyle(SizeType.Absolute, 20)) ' 制造商行

' 1. 添加品牌行
Dim lblBrand As New Label()
lblBrand.Text = "deli得力 条码标签打印机"
lblBrand.Font = New Font("Microsoft YaHei", 10, FontStyle.Bold)
lblBrand.TextAlign = ContentAlignment.MiddleLeft
tableLayout.Controls.Add(lblBrand, 0, 0)

' 2. 添加型号行
Dim lblModel As New Label()
lblModel.Text = "型号:DL-888T"
lblModel.Font = New Font("Microsoft YaHei", 9)
lblModel.TextAlign = ContentAlignment.MiddleLeft
tableLayout.Controls.Add(lblModel, 0, 1)

' 3. 添加机号行
Dim lblSerial As New Label()
lblSerial.Text = "机号:62452801737"
lblSerial.Font = New Font("Microsoft YaHei", 9)
lblSerial.TextAlign = ContentAlignment.MiddleLeft
tableLayout.Controls.Add(lblSerial, 0, 2)

' 4. 添加电源行
Dim lblPower As New Label()
lblPower.Text = "电源:24V===2.0A"
lblPower.Font = New Font("Microsoft YaHei", 9)
lblPower.TextAlign = ContentAlignment.MiddleLeft
tableLayout.Controls.Add(lblPower, 0, 3)

' 5. 添加警告行(带图标)
Dim warningPanel As New Panel()
warningPanel.Dock = DockStyle.Fill

' 警告图标
Dim picWarning As New PictureBox()
picWarning.Image = SystemIcons.Warning.ToBitmap()
picWarning.SizeMode = PictureBoxSizeMode.Zoom
picWarning.Size = New Size(20, 20)
picWarning.Location = New Point(0, 10)

' 警告文本
Dim lblWarning As New Label()
lblWarning.Text = "注意:使用其他数值之电压会使机件损坏;在进行维修/维护时必须切断电源。"
lblWarning.Font = New Font("Microsoft YaHei", 8)
lblWarning.Location = New Point(25, 5)
lblWarning.AutoSize = True

warningPanel.Controls.Add(picWarning)
warningPanel.Controls.Add(lblWarning)
tableLayout.Controls.Add(warningPanel, 0, 4)

' 6. 添加制造商行
Dim lblMaker As New Label()
lblMaker.Text = "得力集团有限公司 中国制造"
lblMaker.Font = New Font("Microsoft YaHei", 8, FontStyle.Italic)
lblMaker.TextAlign = ContentAlignment.MiddleRight
tableLayout.Controls.Add(lblMaker, 0, 5)

' 添加到窗体
Me.Controls.Add(tableLayout)
End Sub

' 配置打印机设置
Private Sub SetupPrinterSettings()
' 转换为1/100英寸(打印机单位)
Dim widthInch As Integer = CInt(labelWidthMM / 25.4 * 100)
Dim heightInch As Integer = CInt(labelHeightMM / 25.4 * 100)

' 创建自定义纸张大小
printDocument.DefaultPageSettings.PaperSize = New PaperSize("Custom", widthInch, heightInch)

' 设置最小边距(根据打印机能力调整)
printDocument.DefaultPageSettings.Margins = New Margins(10, 10, 10, 10)

' 配置高质量打印
ConfigurePrinterForQuality()
End Sub

' 打印按钮点击事件
Private Sub btnPrint_Click(sender As Object, e As EventArgs) Handles btnPrint.Click
' 优化打印字体
OptimizePrintFonts()

Dim printDialog As New PrintDialog()
printDialog.Document = printDocument

If printDialog.ShowDialog() = DialogResult.OK Then
printDocument.Print()
End If
End Sub

' 打印页面事件
Private Sub PrintDocument_PrintPage(sender As Object, e As PrintPageEventArgs) Handles printDocument.PrintPage
' 1. 设置高质量打印参数
e.Graphics.InterpolationMode = Drawing2D.InterpolationMode.High
e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.None
e.Graphics.TextRenderingHint = Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit

' 2. 计算缩放比例(使表格适应标签)
Dim scaleX As Single = (e.MarginBounds.Width – 5) / tableLayout.Width
Dim scaleY As Single = (e.MarginBounds.Height – 5) / tableLayout.Height
Dim scale As Single = Math.Min(scaleX, scaleY) * 0.95 ' 留5%边距

' 3. 创建位图来捕获表格内容
Dim tableBitmap As New Bitmap(tableLayout.Width, tableLayout.Height)
tableLayout.DrawToBitmap(tableBitmap, New Rectangle(0, 0, tableLayout.Width, tableLayout.Height))

' 4. 绘制表格到打印页面(保持比例)
e.Graphics.DrawImage(tableBitmap,
e.MarginBounds.Left + (e.MarginBounds.Width – tableLayout.Width * scale) / 2,
e.MarginBounds.Top + (e.MarginBounds.Height – tableLayout.Height * scale) / 2,
tableLayout.Width * scale,
tableLayout.Height * scale)

' 5. 释放资源
tableBitmap.Dispose()

e.HasMorePages = False
End Sub

' 优化打印字体(解决小字模糊问题)
Private Sub OptimizePrintFonts()
For Each ctrl As Control In tableLayout.Controls
If TypeOf ctrl Is Label Then
Dim lbl As Label = DirectCast(ctrl, Label)
' 使用更清晰的字体配置
lbl.Font = New Font("Microsoft YaHei", lbl.Font.Size, lbl.Font.Style, GraphicsUnit.Point, 0, True)

' 重要内容加粗
If lbl.Text.StartsWith("型号:") Or lbl.Text.StartsWith("机号:") Then
lbl.Font = New Font(lbl.Font, FontStyle.Bold)
End If
ElseIf TypeOf ctrl Is Panel Then
' 处理警告面板中的控件
For Each childCtrl As Control In ctrl.Controls
If TypeOf childCtrl Is Label Then
Dim lbl As Label = DirectCast(childCtrl, Label)
lbl.Font = New Font("Microsoft YaHei", 8, FontStyle.Regular, GraphicsUnit.Point, 0, True)
End If
Next
End If
Next
End Sub

' 配置打印机质量
Private Sub ConfigurePrinterForQuality()
' 尝试设置最高分辨率
Dim maxRes = printDocument.PrinterSettings.PrinterResolutions _
.Cast(Of PrinterResolution)() _
.OrderByDescending(Function(r) r.X) _
.FirstOrDefault()

If maxRes IsNot Nothing AndAlso maxRes.X >= 300 Then
printDocument.DefaultPageSettings.PrinterResolution = maxRes
End If

' 设置标签纸类型
printDocument.DefaultPageSettings.PaperSource = printDocument.PrinterSettings.PaperSources _
.Cast(Of PaperSource)() _
.FirstOrDefault(Function(ps) ps.SourceName.Contains("Label") OrElse
ps.Kind = PaperSourceKind.AutomaticFeed)
End Sub
End Class

关键优化点说明

  • 精确的标签内容布局:

    • 使用TableLayoutPanel精确控制每个信息块的位置

    • 按照原始标签的视觉层次组织内容

    • 警告信息添加了系统警告图标

  • 打印清晰度保障:

    • 强制使用GDI兼容模式字体(GraphicsUnit.Point, 0, True参数)

    • 为关键信息(型号、机号)加粗显示

    • 采用SingleBitPerPixelGridFit文本渲染模式

  • 自适应缩放:

    • 自动计算最佳缩放比例(保留5%边距)

    • 居中打印内容,确保不超出标签范围

  • 打印机配置:

    • 自动选择最高可用分辨率(≥300dpi)

    • 尝试识别标签纸纸盒

  • 使用建议

  • 测试打印步骤:

    ' 在打印前调用测试方法
    Private Sub btnTestPrint_Click(sender As Object, e As EventArgs) Handles btnTestPrint.Click
    OptimizePrintFonts()

    Dim previewDialog As New PrintPreviewDialog()
    previewDialog.Document = printDocument
    previewDialog.ShowDialog()
    End Sub

  • 字体调整建议:

    • 如果仍不够清晰,可以调整以下参数:

    ' 在OptimizePrintFonts方法中修改
    lbl.Font = New Font("Arial Narrow", lbl.Font.Size, lbl.Font.Style, GraphicsUnit.Point, 0, True)

  • 标签纸校准:

    • 如果出现位置偏移,调整边距设置:

    ' 在SetupPrinterSettings方法中修改
    printDocument.DefaultPageSettings.Margins = New Margins(15, 15, 10, 10) ' 左,右,上,下

  • 特殊符号处理:

    • 如需打印认证标志(如CCC),可添加图片控件:

    Dim picCCC As New PictureBox()
    picCCC.Image = Image.FromFile("ccc_logo.png")
    picCCC.SizeMode = PictureBoxSizeMode.Zoom
    tableLayout.Controls.Add(picCCC, 0, 6)

  • 此方案已针对得力DL-888T的标签内容进行专门优化,确保在81mm×60mm标签上获得清晰可读的打印效果。实际使用时,建议先进行预览测试,再根据具体打印效果微调字体大小和布局参数。

    赞(0)
    未经允许不得转载:网硕互联帮助中心 » 【VB.NET】将标签信息精确打印到81mm×60mm标签纸上:
    分享到: 更多 (0)

    评论 抢沙发

    评论前必须登录!