控制每页打印行数

打开CaseStudy目录下的文件:统计演示.Table

假定希望打印订单表,每页打印20行,可以参考下面的代码:

Dim doc As New PrintDoc
Dim
tb As Table = Tables("订单")
Dim
prs As Integer = 20 '每页20行
For
p As Integer = 0 To math.Ceiling(tb.Rows.Count / prs) - 1
    Dim
rt As New prt.RenderTable
    rt
.Style.Gridlines.All = New prt.Linedef(Color.Gray)
    rt
.CellStyle.Spacing.All = 0.5
    For
c As Integer = 0 To tb.Cols.Count - 1
       
rt.Cells(0,c).Text = tb.Cols(c).Name
        For
r As Integer = p * prs To math.min(tb.Rows.Count - 1,( p + 1) * prs - 1)
            rt
.Cells(r - p * prs + 1, c).Text = tb.rows(r)(c)
        Next
    Next
    If
p < math.Ceiling(tb.Rows.Count / prs) - 1
        rt.BreakAfter = prt.BreakEnum.Page
    End If

    doc
.Body.Children.Add(rt)
Next

doc
.Preview()

如果是多层表头,可以采用下面的代码:

Dim doc As New PrintDoc
Dim
tb As Table = Tables("订单")
Dim
prs As Integer = 20 '每页20行
For
p As Integer = 0 To math.Ceiling(tb.Rows.Count / prs) - 1
    Dim
rt As New prt.RenderTable
    rt.Style.Gridlines.All = New prt.Linedef(Color.Gray)
    rt.CellStyle.Spacing.All =
0.5
    tb.CreateReportHeader(rt,
False) '生成多层表头
   
For c As Integer = 0 To tb.Cols.Count - 1
       
For r As Integer = p * prs To math.min(tb.Rows.Count - 1,( p + 1) * prs - 1)
            rt.Cells(r - p * prs + tb.HeaderRows, c).Text = tb.rows(r)(c)

        Next
    Next
    If
p < math.Ceiling(tb.Rows.Count / prs) - 1
        rt.BreakAfter = prt.BreakEnum.Page

    End
If
    doc.Body.Children.Add(rt)

Next

doc.Preview()

有了上面的基础,我们可以很方便地给每页加上标题,例如下面的代码会在每页显示标题“2010年订单”:

Dim doc As New PrintDoc
Dim
tb As Table = Tables("订单")
Dim
prs As Integer = 20 '每页20行
For
p As Integer = 0 To math.Ceiling(tb.Rows.Count / prs) - 1
    Dim
rx As New prt.RenderText
    rx
.Text = "2010年订单"
   
rx.Style.Font = New Font("宋体", 16, FontStyle.Bold)
    rx.Style.TextAlignHorz = prt.AlignHorzEnum.Center
    rx.Style.Spacing.Bottom =
3
    Doc.body.Children.Add(rx)

    Dim
rt As New prt.RenderTable
    rt
.Style.Gridlines.All = New prt.Linedef(Color.Gray)
    rt
.CellStyle.Spacing.All = 0.5
    For
c As Integer = 0 To tb.Cols.Count - 1
       
rt.Cells(0,c).Text = tb.Cols(c).Name
        For
r As Integer = p * prs To math.min(tb.Rows.Count - 1,( p + 1) * prs - 1)
            rt
.Cells(r - p * prs + 1, c).Text = tb.rows(r)(c)
        Next
    Next
    If
p < math.Ceiling(tb.Rows.Count / prs) - 1
       
rt.BreakAfter = prt.BreakEnum.Page
    End If

    doc
.Body.Children.Add(rt)
Next

doc
.Preview
()


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