VB.NET打印预览添加边框并将标签值置于边框内的实现方法
VB.NET PrintDocument 添加边框并将文本置于边框内的实现方案
实现思路
你当前用下划线模拟输入区域,换成DrawRectangle绘制边框更规范,还能精确控制文本在边框内的位置。核心步骤:
- 用
Graphics.MeasureString计算文本尺寸,确定边框大小 - 调用
DrawRectangle绘制边框,替代原有的下划线 - 调整文本绘制位置,确保内容在边框内部留有适当内边距
修改后的完整代码
Private Sub PrintDocument1_PrintPage(sender As Object, e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage ' 定义通用字体与画笔 Dim titleFont As New Font("Arial", 20, FontStyle.Bold) Dim contentFont As New Font("Arial", 13, FontStyle.Regular) Dim blackPen As New Pen(Color.Black, 1) ' 绘制标题 e.Graphics.DrawString("CONOLOSCOPY", titleFont, Brushes.Red, New Point(300, 20)) ' 第一行:ENDOSCOPY NO.、REG NO.、HEIGHT DrawLabelWithBorder(e.Graphics, "ENDOSCOPY NO.: " & txt_ID.Text, contentFont, New Point(10, 60), 150) DrawLabelWithBorder(e.Graphics, "REG NO.: " & txt_REGNO.Text, contentFont, New Point(300, 60), 100) DrawLabelWithBorder(e.Graphics, "HEIGHT: " & txt_HEIGHT.Text, contentFont, New Point(500, 60), 100) ' 第二行:DATE、ROOM NO.、WEIGHT NO. DrawLabelWithBorder(e.Graphics, "DATE: " & txt_DATE.Text, contentFont, New Point(10, 80), 200) DrawLabelWithBorder(e.Graphics, "ROOM NO.: " & txt_ROOMNO.Text, contentFont, New Point(280, 80), 100) DrawLabelWithBorder(e.Graphics, "WEIGHT NO.: " & txt_WEIGHT.Text, contentFont, New Point(500, 80), 120) ' 第三行:LAST NAME、FIRST NAME DrawLabelWithBorder(e.Graphics, "LAST NAME: " & txt_LNAME.Text, contentFont, New Point(10, 130), 220) DrawLabelWithBorder(e.Graphics, "FIRST NAME: " & txt_FNAME.Text, contentFont, New Point(350, 130), 250) ' 第四行:AGE、SEX、CELL NO. DrawLabelWithBorder(e.Graphics, "AGE: " & txt_AGE.Text, contentFont, New Point(10, 160), 50) DrawLabelWithBorder(e.Graphics, "SEX: " & txt_SEX.Text, contentFont, New Point(120, 160), 50) DrawLabelWithBorder(e.Graphics, "CELL NO.: " & txt_CELLNO.Text, contentFont, New Point(250, 160), 180) ' 第五行:ADDRESS DrawLabelWithBorder(e.Graphics, "ADDRESS: " & txt_ADDRESS.Text, contentFont, New Point(10, 190), 580) ' 第六行:EMAIL DrawLabelWithBorder(e.Graphics, "EMAIL: " & txt_EMAIL.Text, contentFont, New Point(10, 240), 220) ' 第七行:REFERRING MD、ATTENDING MD DrawLabelWithBorder(e.Graphics, "REFERRING MD: " & txt_REFERMD.Text, contentFont, New Point(10, 270), 180) DrawLabelWithBorder(e.Graphics, "ATTENDING MD: " & txt_MD.Text, contentFont, New Point(350, 270), 200) ' 第八行:ANESTHESIOLOGIST DrawLabelWithBorder(e.Graphics, "ANESTHESIOLOGIST: " & txt_ANES.Text, contentFont, New Point(10, 300), 380) ' 第九行:PROCEDURE、INDICATION DrawLabelWithBorder(e.Graphics, "PROCEDURE: " & txt_PROC.Text, contentFont, New Point(10, 330), 200) DrawLabelWithBorder(e.Graphics, "INDICATION: " & txt_INDICATION.Text, contentFont, New Point(350, 330), 180) ' 第十行:PREMEDICATION DrawLabelWithBorder(e.Graphics, "PREMEDICATION: " & txt_PREMED.Text, contentFont, New Point(10, 360), 380) ' 第十一行:SCOPE USED、PROCTIME DrawLabelWithBorder(e.Graphics, "SCOPE USED: " & txt_SCOPE.Text, contentFont, New Point(10, 390), 200) DrawLabelWithBorder(e.Graphics, "PROCTIME: " & txt_PROCTIME.Text, contentFont, New Point(400, 390), 200) ' 第十二行:BIOPSY DrawLabelWithBorder(e.Graphics, "BIOPSY: " & txt_BIOPSY.Text, contentFont, New Point(370, 420), 200) ' 后续项目:ESOPHAGUS、CARDIA等 DrawLabelWithBorder(e.Graphics, "ESOPHAGUS: " & txt_ESOPHAGUS.Text, contentFont, New Point(10, 450), 580) DrawLabelWithBorder(e.Graphics, "CARDIA: " & txt_CARDIA.Text, contentFont, New Point(10, 480), 580) DrawLabelWithBorder(e.Graphics, "BODY: " & txt_BODY.Text, contentFont, New Point(10, 510), 580) DrawLabelWithBorder(e.Graphics, "ANTRUM: " & txt_ANTRUM.Text, contentFont, New Point(10, 540), 580) DrawLabelWithBorder(e.Graphics, "DUODENAL: " & txt_DUODENAL.Text, contentFont, New Point(10, 580), 580) DrawLabelWithBorder(e.Graphics, "ENDOSCOPIC DESCRIPTION: " & txt_ENDODESC.Text, contentFont, New Point(10, 610), 400) DrawLabelWithBorder(e.Graphics, "THERAPEUTIC INTERVENTION " & txt_THERAPEUTIC.Text, contentFont, New Point(10, 660), 400) DrawLabelWithBorder(e.Graphics, "DIAGNOSIS: " & txt_DIAGNOSIS.Text, contentFont, New Point(10, 810), 580) DrawLabelWithBorder(e.Graphics, "HISTOPATH: " & txt_HISTOPATH.Text, contentFont, New Point(10, 960), 580) DrawLabelWithBorder(e.Graphics, "ENDOSCOPIST: " & txt_ENDOSPCOPIST.Text, contentFont, New Point(10, 1010), 580) ' 释放GDI+资源 titleFont.Dispose() contentFont.Dispose() blackPen.Dispose() End Sub ' 辅助方法:统一绘制带边框的标签文本 Private Sub DrawLabelWithBorder(g As Graphics, text As String, font As Font, topLeft As Point, borderWidth As Integer) Dim pen As New Pen(Color.Black, 1) ' 计算文本高度,动态确定边框高度(增加2像素内边距) Dim textSize As SizeF = g.MeasureString(text, font) Dim borderRect As New Rectangle(topLeft.X, topLeft.Y, borderWidth, CInt(textSize.Height + 2)) ' 绘制边框 g.DrawRectangle(pen, borderRect) ' 绘制文本,左侧留3像素内边距 g.DrawString(text, font, Brushes.Black, New Point(topLeft.X + 3, topLeft.Y)) pen.Dispose() End Sub
关键说明
- 封装
DrawLabelWithBorder方法,减少重复代码,统一控制边框与文本的样式 - 通过
MeasureString获取文本尺寸,确保边框高度适配文本,避免内容溢出 - 可根据实际需求调整边框宽度,长文本(如ADDRESS)设置较大宽度即可
- 所有GDI+对象使用后及时释放,避免资源泄漏
内容的提问来源于stack exchange,提问作者Lloyd Merwyn Mandanas
相关产品推荐
相关产品推荐

