连续打印
本节的这个例子来自合并单元格实战,不过之前的例子每次只能打印一个员工。
这节的例子一次打印所有员工,并针对每个员工设置一个书签,方便用户选择不同的员工查看。
请先打开CaseStyudy目录下的文件"PDFCreator示例.Table"后再运行下面的示例代码。
完整代码如下,请在命令窗口测试执行:
'定义一个二维数组,一个元素对应一个单元格
Dim
cells(, )As
String
= {
{"1",
"1",
"1",
"1",
"5,1"},
{"1",
"1",
"1",
"1",
"0"},
{"1",
"1",
"1",
"1",
"0"},
{"1",
"1,3",
"0",
"0",
"0"},
{"1",
"1",
"1",
"1",
"0"},
{"1,5",
"0",
"0",
"0",
"0"}
}
'定义一个二维数组,用于表示单元格内容
Dim
contents(, )
As
String
= {
{"姓名",
"[姓名]",
"出生日期",
"[出生日期]",
"{照片}"},
{"部门",
"[部门]",
"雇佣日期",
"[雇佣日期]",
""},
{"性别",
"[性别]",
"职务",
"[职务]",
""},
{"地址",
"[地址]",
"",
"",
""},
{"家庭电话",
"[家庭电话]",
"办公电话",
"[办公电话]",
""},
{"[备注]",
"",
"",
"",
""}
}
Dim
defaultRowHeight
As
Integer
= 28
'默认(单倍)行高
Dim
colWidths()
As
Double
= {54, 128, 54, 128, 124}
'各列宽度
Dim
rowTimes()
As
Integer
= {1, 1, 1, 1, 1, 3}
'各行的行高倍数,第6行为3倍行高,其他为默认(单倍)行高
'下面的代码是通用的,不管表格如何变化,你基本都不需要修改代码,只需修改前面的各数组即可:
Dim
file
As
String
=
"c:\temp\test.pdf"
Dim
pdc
As
New
PDFCreator()
pdc.ConformanceLevel = PDF.PdfAConformanceLevel.PdfA1b
'加上这一行提高兼容性
Dim
rectPage
As
RectangleF = pdc.PageRectangle
rectPage.Inflate( - 72, - 72)
pdc.Pages.Clear()
For
Each
tRow
As
Row
In
Tables("员工").Rows
pdc.NewPage()
pdc.AddBookmark(tRow("姓名"),
0, rectPage.y)
'绘制标题
Dim
title
As
String
=
"员工资料卡"
Dim
titleFont
As
New
Font("宋体",
16, fontstyle.Bold)
Dim
sf
As
New
StringFormat()
sf.Alignment = StringAlignment.Center
pdc.DrawString(title, titlefont, color.Black, rectpage, sf)
Dim
rectCell
As
RectangleF = rectPage
'绘制表格
rectCell.Y = rectCell.Y + pdc.MeasureString(title, titleFont,
rectPage.Width).Height + 15
'标题之后15磅为表格
Dim
contentFont
As
New
Font("宋体",
10)
sf.Alignment = StringAlignment.Near
'单元格内容水平靠左
For
r
As
Integer
= 0
To
cells.GetLength(0) - 1
'逐行绘制
For
c
As
Integer
= 0
To
cells.GetLength(1) - 1
'绘制这一行的单元格
Dim
ifo
As
String
= cells(r, c)
Dim
cellHeight
As
Double
= 0
'单元格高度
Dim
cellWidth
As
Double
= 0
'单元格宽度
sf.LineAlignment = StringAlignment.Center
'单元格内容默认垂直居中
If
rowTimes(r) > 1
Then
sf.LineAlignment = StringAlignment.Near
'如果多倍行高,则单元格内容垂直靠上对齐
End
If
If
ifo =
"0"
OrElse
ifo =
"1"
Then
cellHeight = defaultRowHeight * rowTimes(r)
'根据行高倍数计算单元格高度
cellWidth = colWidths(c)
'取列宽为单元格宽度
Else
'如果是合并单元格
Dim
vls()
As
String
= cells(r, c).Split(",")
For
mr
As
Integer
= r
To
r +
CInt(vls(0))
- 1
'根据合并行数计算单元格高度
cellHeight = cellHeight + defaultRowHeight * rowTimes(mr)
Next
For
mc
As
Integer
= c
To
c +
CInt(vls(1))
- 1
'根据合并列数计算单元格宽度
cellWidth = cellWidth + colWidths(mc)
Next
End
If
If
ifo <>
"0"
Then
'绘制单元格
rectCell.Height = cellHeight
rectCell.Width = cellWidth
Dim
content
As
String
= contents(r, c)
'从二维数组获取单元格内容
If
content >
""
Then
'如果有内容
Dim
rectContent
As
RectangleF = rectCell
rectContent.Inflate( - 3, - 3)
'单元格内容边距为3磅
If
content.StartsWith("[")
AndAlso
content.EndsWith("]")
Then
'如果是[列名]格式
content = tRow(content.Trim("[",
"]"))
'获取列内容,注意先要去掉首尾的方括号
End
If
If
content.StartsWith("{")
AndAlso
content.EndsWith("}")
Then
'如果是{列名}格式
Dim
img
As
Image = GetImage(tRow(content.Trim("{",
"}")))
'获取图片,注意先要去掉首尾的大括号
pdc.DrawImage(img, rectContent,
ContentAlignment.MiddleCenter, PDF.ImageSizeModeEnum.Scale)
'绘制图片
Else
pdc.DrawString(content, contentFont, color.Black,
rectContent, sf)
'绘制单元格文本
End
If
End
If
pdc.DrawRectangle(pens.Black, rectCell)
'绘制单元格边框
End
If
rectCell.Offset(colWidths(c), 0)
'右移单元格
Next
rectCell.Offset(0, defaultRowHeight * rowTimes(r))
'移到下一行
rectCell.X = rectPage.X
'单元格回到水平初始位置,准备绘制下一行
Next
Next
pdc.Save(file)
'保存文件
Process.Start(file)
'打开文件
生成的文档如下,左边有个书签,用于选择员工: