自动行高

请先打开CaseStyudy目录下的文件"PDFCreator示例.Table"再运行本节的示例代码。

本节的任务是打印员工表,但是要求高一点,需要根据备注列内容自动调整行高:

要实现自动行高,我们需要修改lambda过程drawRow,在原来的基础上,增加了一个tAutoIndex参数,用于指定自动行高列的索引位置。

修改后的drawRow过程如下:

Dim drawRow =
Function
(dr As DataRow, tFields() As String, tWidths() As Integer, tRect As RectangleF, tAutoIndex As String)
   
Dim tAutoField As String = tFields(tAutoIndex) '获取自动行高列
   
Dim tAutoFieldWidth As Integer = tWidths(tAutoIndex) '获得自动行高列的宽度
   
Dim height As Double = pdc.MeasureString(dr(tAutoField), fontData, tAutoFieldWidth - 6).Height '计算出AutoField列的文本高度,单元格边距为3,所以文本宽度比单元格宽度少6
    tRect.Height = Math.Max(height + 6, rowHeight)
'假定单元格边距为3磅,所以文本高度加上6磅为行高,最低行高为rowHeight变量指定的默认行高   
   
If tRect.Bottom > rectPage.Bottom Then '如果剩余空间不够
        pdc.NewPage()
'则另起一页
        tRect.Y = rectPage.Y
'定位到页首
        tRect = drawHeader.Invoke(tFields, tWidths, tRect)
'给新增加的页面绘制列标题
        tRect.Height = Math.Max(height + 6, rowHeight)
'重新设置tRect的高度为刚刚测量的高度
   
End If
   
Dim rectBorder = tRect '单元格边框矩形
   
For c As Integer = 0 To tFields.Length - 1 '逐个列绘制
        rectBorder.Width = tWidths(c)
'设置列宽
        pdc.DrawRectangle(pens.DarkGray, rectBorder)
'绘制单元格边框
       
Dim rectContent As RectangleF = rectBorder '内容矩形
        rectContent.Inflate( - 3, - 3)
'单元格边距为3
       
If dr.DataTable.DataCols(tFields(c)).IsNumeric Then '如果是数值列
            sf.Alignment = StringAlignment.Far
'水平靠右对齐
       
Else
            sf.Alignment = StringAlignment.Near
'否则居中对齐
       
End If
        pdc.DrawString(dr(tFields(c)), fontData, Color.Black, rectContent, sf)
'绘制单元格内容
        rectBorder.Offset(rectBorder.Width, 0)
''rectBorder右移到下一个单元格位置
   
Next
    tRect.Offset(0, tRect.Height)
'移动到下一个内容的起始位置
   
Return tRect '返回rect
End
Function  

示例

下面是完整的示例代码,你可以在命令窗口测试执行:

Dim file As String = "c:\temp\test.pdf"
Dim
pdc As New PDFCreator()
Dim
rectPage As RectangleF = pdc.PageRectangle
rectPage.Inflate( - 72, - 72)

Dim
sf As New StringFormat
sf.LineAlignment = StringAlignment.Center
'单元格内容垂直居中
Dim
rowHeight As Integer = 20 '行高为20
Dim
fontHeader As New Font("微软雅黑", 12, fontstyle.Bold) '列标题字体
Dim
fontData As New Font("微软雅黑", 10) '数据单元格字体 
'
绘制文本的lambda过程
Dim
drawText =
Function
(tText As String, tFont As Font, tAlignment As StringAlignment, tRect As RectangleF)
   
Dim tsf As New StringFormat
    tsf.Alignment = tAlignment
'设置对齐方式
    tRect.Height = pdc.MeasureString(tText, tFont, trect.Width).Height
'测量文本高度,并赋值给tRect
   
If tRect.Bottom > rectPage.Bottom Then '如果剩余空间不够
        pdc.NewPage()
'则另起一页
        tRect.Y = rectPage.Y
'定位到页首
   
End If
    tRect.Height = pdc.MeasureString(tText, tfont, trect.Width, tsf).Height
'将矩形高度设置为文本高度
    pdc.DrawString(tText, tFont, color.Black, tRect, tsf)
'绘制文本
    tRect.Offset(0, trect.Height)
'移到下一个内容的起始位置
   
Return tRect '返回tRec
End
Function  

'绘制列标题的lambda过程,tFields为列名,tWidths为列宽,tRect为列标题所在的矩形局域
Dim
drawHeader =
Function
(tFields() As String, tWidths() As Integer, tRect As RectangleF)
    tRect.Height = rowHeight
'设置行高
   
If tRect.Bottom > rectPage.Bottom Then '如果剩余空间不够
        pdc.NewPage()
'则另起一页
        tRect.Y = rectPage.Y
''定位到页首
   
End If
   
Dim rectBorder = tRect 'rectBorder为单元格边框矩形
    sf.Alignment = StringAlignment.Center
'列标题水平居中
   
For c As Integer = 0 To tFields.Length - 1 '逐个绘制标题
        rectBorder.Width = tWidths(c)
'设置列宽
        pdc.DrawRectangle(pens.DarkGray, rectBorder)
'绘制单元格边框
        pdc.FillRectangle(Color.Gray, rectBorder)
'填充背景
       
Dim rectContent As RectangleF = rectBorder '列标题内容矩形
        rectContent.Inflate( - 3, - 3)
'单元格边距为3
        pdc.DrawString(tFields(c), fontHeader, Color.White, rectContent, sf)
'绘制单元格内容(列标题)
        rectBorder.Offset(rectBorder.Width, 0)
'rectBorder右移到下一个列标题位置
   
Next
    tRect.Offset(0, tRect.Height)
'移动到下一个内容的起始位置
   
Return tRect '返回rect
End
Function 

'绘制行的lambda过程,dr为要绘制的DataRowtFields为列名,tWidths为列宽,tRect为改行所在的矩形局域, tAutoIndex指定自动行高列的索引位置
Dim
drawRow =
Function
(dr As DataRow, tFields() As String, tWidths() As Integer, tRect As RectangleF, tAutoIndex As String)
   
Dim tAutoField As String = tFields(tAutoIndex) '获取自动行高列
   
Dim tAutoFieldWidth As Integer = tWidths(tAutoIndex) '获得自动行高列的宽度
   
Dim height As Double = pdc.MeasureString(dr(tAutoField), fontData, tAutoFieldWidth - 6).Height '计算出AutoField列的文本高度,单元格边距为3,所以文本宽度比单元格宽度少6
    tRect.Height = Math.Max(height + 6, rowHeight)
'假定单元格边距为3磅,所以文本高度加上6磅为行高,最低行高为rowHeight变量指定的默认行高   
   
If tRect.Bottom > rectPage.Bottom Then '如果剩余空间不够
        pdc.NewPage()
'则另起一页
        tRect.Y = rectPage.Y
'定位到页首
        tRect = drawHeader.Invoke(tFields, tWidths, tRect)
'给新增加的页面绘制列标题
        tRect.Height = Math.Max(height + 6, rowHeight)
'重新设置tRect的高度为刚刚测量的高度
   
End If
   
Dim rectBorder = tRect '单元格边框矩形
   
For c As Integer = 0 To tFields.Length - 1 '逐个列绘制
        rectBorder.Width = tWidths(c)
'设置列宽
        pdc.DrawRectangle(pens.DarkGray, rectBorder)
'绘制单元格边框
       
Dim rectContent As RectangleF = rectBorder '内容矩形
        rectContent.Inflate( - 3, - 3)
'单元格边距为3
       
If dr.DataTable.DataCols(tFields(c)).IsNumeric Then '如果是数值列
            sf.Alignment = StringAlignment.Far
'水平靠右对齐
       
Else
            sf.Alignment = StringAlignment.Near
'否则居中对齐
       
End If
        pdc.DrawString(dr(tFields(c)), fontData, Color.Black, rectContent, sf)
'绘制单元格内容
        rectBorder.Offset(rectBorder.Width, 0)
''rectBorder右移到下一个单元格位置
   
Next
    tRect.Offset(0, tRect.Height)
'移动到下一个内容的起始位置
   
Return tRect '返回rect
End
Function  

'调用lambda过程绘制员工表
Dim
rectRow As RectangleF = rectPage 'rectRow将传递给所有用于绘制的lambda过程的tRect参数,相当于一个位置游标,始终都在动态变化中
rectRow = drawText.Invoke(
"员工表", New Font("黑体", 20, fontstyle.Bold), StringAlignment.Center, rectRow) '文档标题
rectRow.Offset(0, 25)
'表标题下方25磅位置开始绘制后续内容(表格)
rectRow.Height = rowHeight
'设置行高
Dim
fields() As String = {"姓名", "部门", "职务", "办公电话", "备注"} '要绘制的列
Dim
Widths() As Integer = {75, 75, 80, 75, 163} '各列宽度,单位为磅
rectRow = drawHeader.Invoke(fields, widths, rectRow)
'绘制列标题
For
Each r As Row In Tables("员工").Rows '逐行绘制
    rectRow = drawRow.Invoke(r.DataRow, fields, widths, rectRow, 4)
'绘制行,指定第五列(索引从0开始)也就是备注列为自动行高列
Next

pdc.Save(file)
'保存文件
Process.Start(file)
'打开文件


本页地址:http://www.foxtable.com/webhelp/topics/6068.htm