iTextSharp - 如何将节标题移动到下一页

时间:2016-12-06 19:53:39

标签: itext

使用iTextSharp运行报告时,我们可以将所有子类别保持在一起,定义单独的PdfpTable和KeepTogether = True

这对于不太可能超过整页的子类别非常有效。

如果主要类别不适合当前页面,是否有办法将主要类别移动到下一页?

这就是我们得到的

enter image description here

费用从最后一个主要类别的末尾开始,但子类别都在下一页上

这是如何创建它们的片段......

由于

 vTable.AddCell(AddNewTextCell(True, "REVENUE", 15, ReportFontSize, False))
        vTable.AddCell(Spacer_Cell(10, 15))

        For Each Row As DataRow In RevenueData
            Dim vInnerTable As New PdfPTable(15)
            vInnerTable.KeepTogether = True

            Dim vCatID As Integer = Row("ID")
            vInnerTable.AddCell(AddNewTextCell(True, Row("Name"), 15, ReportFontSize, False))
            Dim vSelected() As DataRow = RevenueNomDT.Select("CatID = " & vCatID, "Position")



            For Each SubRow As DataRow In vSelected
                SetLeftPadding = 10
                vInnerTable.AddCell(AddNewTextCell(False, SubRow("NomCode") & " " & SubRow("NomName"), 2, ReportFontSize))
                Dim NomCode As Integer = SubRow("NomCode")
                Dim NegValue As Integer = SubRow("NegValue")
                Dim vNeg As Boolean = False
                If NegValue = 1 Then
                    vNeg = True
                End If
                Dim vNomType As Integer = 0
                Dim vTypes() As DataRow = NLCodes.Select("NL_Code = '" & NomCode & "'", Nothing)
                For Each NominalRow As DataRow In vTypes
                    vNomType = NominalRow("Account_Type")
                Next

                SetLeftPadding = 0
                '12 Columns of data
                For i As Integer = 0 To 11
                    Dim vNomValue As Decimal = 0
                    Dim ReportMonth As Integer = DateDiff(DateInterval.Month, ReportStartDate, ReportDate) + 1
                    If ReportMonth > i Then
                        If vNomType = 2 Then
                            vNomValue = ReturnMonthlyAmount(i, NomCode, True, ReportStartDate, ReportEndDate, Current_HOA_ID, vNeg)
                        Else
                            vNomValue = ReturnMonthlyAmount(i, NomCode, False, ReportStartDate, ReportEndDate, Current_HOA_ID, vNeg)
                        End If

                        RT += vNomValue
                        GT = DicRevenue.Item(i)
                        DicRevenue.Item(i) = GT + vNomValue
                        Dim vAmt As Decimal = DicSubs.Item(i)
                        DicSubs.Item(i) = vNomValue + vAmt

                        vInnerTable.AddCell(AddNewCurrencyCell(vNomValue, False, False, ReportFontSize))
                    Else
                        vInnerTable.AddCell(AddNewCurrencyCell(vNomValue, False, False, ReportFontSize))
                    End If
                Next
                vInnerTable.AddCell(AddNewCurrencyCell(RT, False, False, ReportFontSize))
                GT = DicRevenue(12)
                DicRevenue(12) = GT + RT
                DicSubs.Item(12) = RT
                RT = 0
            Next

            SetLeftPadding = 5

            'Add the total of each revenue sub header
            vInnerTable.AddCell(AddNewTextCell(True, "Total " & Row("Name"), 2, ReportFontSize, False))
            For i As Integer = 0 To 11
                vInnerTable.AddCell(AddNewCurrencyCell(DicSubs.Item(i), True, True, ReportFontSize, False))
                RT += DicSubs.Item(i)
            Next

            vInnerTable.AddCell(AddNewCurrencyCell(RT, True, True, ReportFontSize, False))
            vTable.AddCell(NoSplitTable(vInnerTable, 15))

            'Remove the DictionaryValues
            For i As Integer = 0 To 12
                DicSubs(i) = 0
            Next
            RT = 0
        Next

        vTable.AddCell(Spacer_Cell(10, 15))
        'Add in the Totals For Revenue
        vTable.AddCell(AddNewTextCell(True, "TOTAL REVENUE", 2, ReportFontSize, False))
        For i As Integer = 0 To 12
            vTable.AddCell(AddNewCurrencyCell(DicRevenue.Item(i), True, True, ReportFontSize, False))
        Next
        GT = 0
        RT = 0
        vTable.AddCell(Spacer_Cell(20, 15))

        '============================================START OF EXPENSES ==============================================
        vTable.AddCell(AddNewTextCell(True, "EXPENSES", 15, ReportFontSize, False))
        vTable.AddCell(Spacer_Cell(10, 15))


 Private Function NoSplitTable(TableName As PdfPTable, DataColumns As Integer) As PdfPCell
    Dim vCell As New iTextSharp.text.pdf.PdfPCell(TableName)
    Try
        With vCell
            .Border = 0
            .Colspan = DataColumns
        End With
        Return vCell
    Catch ex As Exception
        EmailError(ex)
        Return vCell
    End Try

End Function

2 个答案:

答案 0 :(得分:1)

比我想象的更简单 - 只需为每个标题添加subTables

开始一个表

 Dim vRevenueTable As New PdfPTable(15)
        vRevenueTable.KeepTogether = True

        vRevenueTable.AddCell(AddNewTextCell(True, "REVENUE", 15, ReportFontSize, False))
        vRevenueTable.AddCell(Spacer_Cell(10, 15))
        For Each Row As DataRow In RevenueData
            Dim vInnerTable As New PdfPTable(15)
            vInnerTable.KeepTogether = True

....该表的结尾

vInnerTable.AddCell(AddNewCurrencyCell(RT, True, True, ReportFontSize, False))
            vRevenueTable.AddCell(NoSplitTable(vInnerTable, 15))

            'Remove the DictionaryValues
            For i As Integer = 0 To 12
                DicSubs(i) = 0
            Next
            RT = 0
        Next
        vRevenueTable.AddCell(Spacer_Cell(10, 15))
        'Add in the Totals For Revenue
        vRevenueTable.AddCell(AddNewTextCell(True, "TOTAL REVENUE", 2, ReportFontSize, False))
        For i As Integer = 0 To 12
            vRevenueTable.AddCell(AddNewCurrencyCell(DicRevenue.Item(i), True, True, ReportFontSize, False))
        Next
        GT = 0
        RT = 0
        vRevenueTable.AddCell(Spacer_Cell(20, 15))

        vTable.AddCell(NoSplitTable(vRevenueTable, 15))

答案 1 :(得分:0)

除了使用keeptogether-tables之外,您还可以通过调用来强制分页 Document.NewPage()或用Chapters和Sections包装表而不是创建一个大表容器。 (4.1.6.0)