如何使用绘图 Vb.net 创建和打印 table
How to create and print a table using Drawing Vb.net
我正在尝试使用 Vb.net
重新创建标签
我有这个要重新创建的标签:
到目前为止我做了什么:
我试着换行,但做不到那样,我什至不知道如何排列、移动或添加新的列或行。
我在网上找到的代码:
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim ht As Single = 7 ' Table Height
Dim wt As Single = 5 ' Table Width
Dim r As Integer = 15, c As Integer = 2 ' Rows and Cols in Table
Dim Cht As Single ' Cell Height
Dim lst As Single ' last line drawn
Dim i As Integer
Dim p As Pen
Cht = CSng(Math.Round(ht / r, 2)) * c
lst = 0.5F + Cht ' 0.5->default margin
p = New Pen(Color.Black, 0.025F)
e.Graphics.PageUnit = GraphicsUnit.Inch
e.Graphics.DrawRectangle(p, 0.5F, 0.5F, wt, ht) ' border of the table
p.Color = Color.Blue
For i = 0 To CInt(r / c) - 1 ' lines in the table
e.Graphics.DrawLine(p, 0.5F, lst, 0.5F + wt, lst)
lst += Cht
Next
End Sub
我迷路了,我不知道如何创建类似的标签。最好的方法是什么?
以下内容将帮助您开始创建 OP 中所示的标签。
我将使用一个 Windows Forms App (.NET Framework) 项目和一个名为 Form1
.
的表单
添加以下导入语句:
Imports System.Drawing.Drawing2D
Imports System.Drawing.Printing
要绘制圆角矩形,我们会将代码从 转换为 VB.NET。
RoundRect:
Public Function RoundRect(bounds As Rectangle, radius1 As Integer, radius2 As Integer, radius3 As Integer, radius4 As Integer) As GraphicsPath
Dim diameter1 As Integer = radius1 * 2
Dim diameter2 As Integer = radius2 * 2
Dim diameter3 As Integer = radius3 * 2
Dim diameter4 As Integer = radius4 * 2
Dim arc1 As Rectangle = New Rectangle(bounds.Location, New Size(diameter1, diameter1))
Dim arc2 As Rectangle = New Rectangle(bounds.Location, New Size(diameter2, diameter2))
Dim arc3 As Rectangle = New Rectangle(bounds.Location, New Size(diameter3, diameter3))
Dim arc4 As Rectangle = New Rectangle(bounds.Location, New Size(diameter4, diameter4))
Dim path As GraphicsPath = New GraphicsPath()
'arc - top left
If radius1 = 0 Then
path.AddLine(arc1.Location, arc1.Location)
Else
path.AddArc(arc1, 180, 90)
End If
'arc - top right
arc2.X = bounds.Right - diameter2
If radius2 = 0 Then
path.AddLine(arc2.Location, arc2.Location)
Else
path.AddArc(arc2, 270, 90)
End If
'arc - bottom right
arc3.X = bounds.Right - diameter3
arc3.Y = bounds.Bottom - diameter3
If radius3 = 0 Then
path.AddLine(arc3.Location, arc3.Location)
Else
path.AddArc(arc3, 0, 90)
End If
'arc - bottom left
'arc4.X = bounds.Right - diameter4
arc4.Y = bounds.Bottom - diameter4
arc4.X = bounds.Left
If radius4 = 0 Then
path.AddLine(arc4.Location, arc4.Location)
Else
path.AddArc(arc4, 90, 90)
End If
path.CloseFigure()
Return path
End Function
注意:下面的代码演示了如何绘制外矩形以及第一行。它还展示了如何编写文本和使用MeasureString来辅助计算位置。
创建产品标签:
Private Sub CreateProductLabel(g As Graphics)
'ToDo: add (additional) desired code
Dim widthOuter As Integer = 600 'width of outer rectangle
Dim heightOuter As Integer = 325 'height of outer rectangle
Dim heightRow1 As Single = 80 'height of row 1
Dim xPosRightRow1Col1 As Single = 200 'x-position of row1, column 1
Dim xPosRightRow1Col2 As Single = 400 'x-position of row1, column 2
'specifying '0', indicates to use the smallest width possible
Using penDimGray As Pen = New Pen(Color.DimGray, 0)
'create rectangle for outer border
Dim outerRect As Rectangle = New Rectangle(0, 0, widthOuter, heightOuter)
'draw outer rectangle
Using path As GraphicsPath = RoundRect(outerRect, 10, 10, 10, 10)
g.DrawPath(penDimGray, path)
End Using
'draw horizontal line
g.DrawLine(penDimGray, New PointF(0, heightRow1), New PointF(outerRect.Width, heightRow1))
'draw vertical line - right side of row1, col 1
g.DrawLine(penDimGray, New PointF(xPosRightRow1Col1, 0), New PointF(xPosRightRow1Col1, heightRow1))
'draw vertical line - right side of row1, col 2
g.DrawLine(penDimGray, New PointF(xPosRightRow1Col2, 0), New PointF(xPosRightRow1Col2, heightRow1))
End Using
'size of the string(s); the height/width will be used in calculations
Dim sizeProductionDate As SizeF = New SizeF() 'initialize
Dim sizeShipper As SizeF = New SizeF() 'initialize
Dim sizeCosigner As SizeF = New SizeF() 'initialize
'draw text - headings
Using penBlack As Pen = New Pen(Color.Black, 2)
Using fontArial9Bold As Font = New Font("Arial", 9, FontStyle.Bold)
Using brush As SolidBrush = New SolidBrush(Color.Black)
'draw strings
g.DrawString("Shipper:", fontArial9Bold, brush, 5.0F, 2.0F)
g.DrawString("Cosigner:", fontArial9Bold, brush, xPosRightRow1Col1 + 2.0F, 2.0F)
'determine size of specified string
'the size (height/width) will be used in calculations below
sizeShipper = g.MeasureString("Shipper:", fontArial9Bold)
sizeCosigner = g.MeasureString("Cosigner:", fontArial9Bold)
End Using
End Using
Using fontArial8Bold As Font = New Font("Arial", 8, FontStyle.Bold)
Using brush As SolidBrush = New SolidBrush(Color.Black)
'draw String - Production Date
g.DrawString("Production Date:", fontArial8Bold, brush, xPosRightRow1Col2 + 2.0F, 2.0F)
'determine size of specified string
'the size (height/width) will be used in calculations below
sizeProductionDate = g.MeasureString("Production Date:", fontArial8Bold)
'draw string - Data de Producao
'this string Is positioned at the same Y-position, but for the X-position, add the height of the previous string
g.DrawString("Data de Producao", fontArial8Bold, brush, xPosRightRow1Col2 + 2.0F, 2.0F + sizeProductionDate.Height)
End Using
End Using
End Using
'draw product label information
Using penBlack As Pen = New Pen(Color.Black, 1)
Using fontArial9Regular As Font = New Font("Arial", 9, FontStyle.Regular)
Using brush As SolidBrush = New SolidBrush(Color.Black)
'draw strings
g.DrawString("A 1 VERDE LIMITADA", fontArial9Regular, brush, 5.0F + sizeShipper.Width, 2.0F)
g.DrawString("Plydor Seafood Limited", fontArial9Regular, brush, xPosRightRow1Col1 + 2.0F + sizeCosigner.Width, 2.0F)
End Using
End Using
End Using
End Sub
注意:要生成二维码,可以使用NuGet包,例如QRCoder.
为了测试,请按照以下说明绘制到 Panel
and/or 以使用 PrintDocument
.
进行打印
打开解决方案资源管理器
- 在 VS 菜单中,单击 查看
- Select 解决方案资源管理器
打开属性 Window
- 在 VS 菜单中,单击 查看
- Select属性Window
添加一个Panel
到Form1(名称:Panel1;尺寸:615、340)
订阅 Paint
活动
- 点击表格中的面板select它。
- 在属性 Window 中,单击
- Double-click Paint 将事件处理程序添加到表单
向 Form1 添加一个 PrintDocument
(名称:PrintDocument1)
订阅 Paint
活动
- 在属性 Window 中,select PrintDocument1 来自 drop-down
- 点击
- Double-click PrintPage 将事件处理程序添加到表单
在表格中添加一个Button
(名称:btnPrint)
订阅Click
活动
- 在属性 Window 中,select btnPrint 来自 drop-down
- 点击
- Double-click 单击将事件处理程序添加到表单
用法(面板):
Private Sub Panel1_Paint(sender As Object, e As PaintEventArgs) Handles Panel1.Paint
CreateProductLabel(e.Graphics)
End Sub
用法(打印文档):
Private Sub btnPrint_Click(sender As Object, e As EventArgs) Handles btnPrint.Click
PrintDocument1.Print()
End Sub
Private Sub PrintDocument1_PrintPage(sender As Object, e As PrintPageEventArgs) Handles PrintDocument1.PrintPage
CreateProductLabel(e.Graphics)
End Sub
表单如下所示:
资源:
我正在尝试使用 Vb.net
重新创建标签我有这个要重新创建的标签:
到目前为止我做了什么:
我在网上找到的代码:
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim ht As Single = 7 ' Table Height
Dim wt As Single = 5 ' Table Width
Dim r As Integer = 15, c As Integer = 2 ' Rows and Cols in Table
Dim Cht As Single ' Cell Height
Dim lst As Single ' last line drawn
Dim i As Integer
Dim p As Pen
Cht = CSng(Math.Round(ht / r, 2)) * c
lst = 0.5F + Cht ' 0.5->default margin
p = New Pen(Color.Black, 0.025F)
e.Graphics.PageUnit = GraphicsUnit.Inch
e.Graphics.DrawRectangle(p, 0.5F, 0.5F, wt, ht) ' border of the table
p.Color = Color.Blue
For i = 0 To CInt(r / c) - 1 ' lines in the table
e.Graphics.DrawLine(p, 0.5F, lst, 0.5F + wt, lst)
lst += Cht
Next
End Sub
我迷路了,我不知道如何创建类似的标签。最好的方法是什么?
以下内容将帮助您开始创建 OP 中所示的标签。
我将使用一个 Windows Forms App (.NET Framework) 项目和一个名为 Form1
.
添加以下导入语句:
Imports System.Drawing.Drawing2D
Imports System.Drawing.Printing
要绘制圆角矩形,我们会将代码从
RoundRect:
Public Function RoundRect(bounds As Rectangle, radius1 As Integer, radius2 As Integer, radius3 As Integer, radius4 As Integer) As GraphicsPath
Dim diameter1 As Integer = radius1 * 2
Dim diameter2 As Integer = radius2 * 2
Dim diameter3 As Integer = radius3 * 2
Dim diameter4 As Integer = radius4 * 2
Dim arc1 As Rectangle = New Rectangle(bounds.Location, New Size(diameter1, diameter1))
Dim arc2 As Rectangle = New Rectangle(bounds.Location, New Size(diameter2, diameter2))
Dim arc3 As Rectangle = New Rectangle(bounds.Location, New Size(diameter3, diameter3))
Dim arc4 As Rectangle = New Rectangle(bounds.Location, New Size(diameter4, diameter4))
Dim path As GraphicsPath = New GraphicsPath()
'arc - top left
If radius1 = 0 Then
path.AddLine(arc1.Location, arc1.Location)
Else
path.AddArc(arc1, 180, 90)
End If
'arc - top right
arc2.X = bounds.Right - diameter2
If radius2 = 0 Then
path.AddLine(arc2.Location, arc2.Location)
Else
path.AddArc(arc2, 270, 90)
End If
'arc - bottom right
arc3.X = bounds.Right - diameter3
arc3.Y = bounds.Bottom - diameter3
If radius3 = 0 Then
path.AddLine(arc3.Location, arc3.Location)
Else
path.AddArc(arc3, 0, 90)
End If
'arc - bottom left
'arc4.X = bounds.Right - diameter4
arc4.Y = bounds.Bottom - diameter4
arc4.X = bounds.Left
If radius4 = 0 Then
path.AddLine(arc4.Location, arc4.Location)
Else
path.AddArc(arc4, 90, 90)
End If
path.CloseFigure()
Return path
End Function
注意:下面的代码演示了如何绘制外矩形以及第一行。它还展示了如何编写文本和使用MeasureString来辅助计算位置。
创建产品标签:
Private Sub CreateProductLabel(g As Graphics)
'ToDo: add (additional) desired code
Dim widthOuter As Integer = 600 'width of outer rectangle
Dim heightOuter As Integer = 325 'height of outer rectangle
Dim heightRow1 As Single = 80 'height of row 1
Dim xPosRightRow1Col1 As Single = 200 'x-position of row1, column 1
Dim xPosRightRow1Col2 As Single = 400 'x-position of row1, column 2
'specifying '0', indicates to use the smallest width possible
Using penDimGray As Pen = New Pen(Color.DimGray, 0)
'create rectangle for outer border
Dim outerRect As Rectangle = New Rectangle(0, 0, widthOuter, heightOuter)
'draw outer rectangle
Using path As GraphicsPath = RoundRect(outerRect, 10, 10, 10, 10)
g.DrawPath(penDimGray, path)
End Using
'draw horizontal line
g.DrawLine(penDimGray, New PointF(0, heightRow1), New PointF(outerRect.Width, heightRow1))
'draw vertical line - right side of row1, col 1
g.DrawLine(penDimGray, New PointF(xPosRightRow1Col1, 0), New PointF(xPosRightRow1Col1, heightRow1))
'draw vertical line - right side of row1, col 2
g.DrawLine(penDimGray, New PointF(xPosRightRow1Col2, 0), New PointF(xPosRightRow1Col2, heightRow1))
End Using
'size of the string(s); the height/width will be used in calculations
Dim sizeProductionDate As SizeF = New SizeF() 'initialize
Dim sizeShipper As SizeF = New SizeF() 'initialize
Dim sizeCosigner As SizeF = New SizeF() 'initialize
'draw text - headings
Using penBlack As Pen = New Pen(Color.Black, 2)
Using fontArial9Bold As Font = New Font("Arial", 9, FontStyle.Bold)
Using brush As SolidBrush = New SolidBrush(Color.Black)
'draw strings
g.DrawString("Shipper:", fontArial9Bold, brush, 5.0F, 2.0F)
g.DrawString("Cosigner:", fontArial9Bold, brush, xPosRightRow1Col1 + 2.0F, 2.0F)
'determine size of specified string
'the size (height/width) will be used in calculations below
sizeShipper = g.MeasureString("Shipper:", fontArial9Bold)
sizeCosigner = g.MeasureString("Cosigner:", fontArial9Bold)
End Using
End Using
Using fontArial8Bold As Font = New Font("Arial", 8, FontStyle.Bold)
Using brush As SolidBrush = New SolidBrush(Color.Black)
'draw String - Production Date
g.DrawString("Production Date:", fontArial8Bold, brush, xPosRightRow1Col2 + 2.0F, 2.0F)
'determine size of specified string
'the size (height/width) will be used in calculations below
sizeProductionDate = g.MeasureString("Production Date:", fontArial8Bold)
'draw string - Data de Producao
'this string Is positioned at the same Y-position, but for the X-position, add the height of the previous string
g.DrawString("Data de Producao", fontArial8Bold, brush, xPosRightRow1Col2 + 2.0F, 2.0F + sizeProductionDate.Height)
End Using
End Using
End Using
'draw product label information
Using penBlack As Pen = New Pen(Color.Black, 1)
Using fontArial9Regular As Font = New Font("Arial", 9, FontStyle.Regular)
Using brush As SolidBrush = New SolidBrush(Color.Black)
'draw strings
g.DrawString("A 1 VERDE LIMITADA", fontArial9Regular, brush, 5.0F + sizeShipper.Width, 2.0F)
g.DrawString("Plydor Seafood Limited", fontArial9Regular, brush, xPosRightRow1Col1 + 2.0F + sizeCosigner.Width, 2.0F)
End Using
End Using
End Using
End Sub
注意:要生成二维码,可以使用NuGet包,例如QRCoder.
为了测试,请按照以下说明绘制到 Panel
and/or 以使用 PrintDocument
.
打开解决方案资源管理器
- 在 VS 菜单中,单击 查看
- Select 解决方案资源管理器
打开属性 Window
- 在 VS 菜单中,单击 查看
- Select属性Window
添加一个Panel
到Form1(名称:Panel1;尺寸:615、340)
订阅 Paint
活动
- 点击表格中的面板select它。
- 在属性 Window 中,单击
- Double-click Paint 将事件处理程序添加到表单
向 Form1 添加一个 PrintDocument
(名称:PrintDocument1)
订阅 Paint
活动
- 在属性 Window 中,select PrintDocument1 来自 drop-down
- 点击
- Double-click PrintPage 将事件处理程序添加到表单
在表格中添加一个Button
(名称:btnPrint)
订阅Click
活动
- 在属性 Window 中,select btnPrint 来自 drop-down
- 点击
- Double-click 单击将事件处理程序添加到表单
用法(面板):
Private Sub Panel1_Paint(sender As Object, e As PaintEventArgs) Handles Panel1.Paint
CreateProductLabel(e.Graphics)
End Sub
用法(打印文档):
Private Sub btnPrint_Click(sender As Object, e As EventArgs) Handles btnPrint.Click
PrintDocument1.Print()
End Sub
Private Sub PrintDocument1_PrintPage(sender As Object, e As PrintPageEventArgs) Handles PrintDocument1.PrintPage
CreateProductLabel(e.Graphics)
End Sub
表单如下所示:
资源: