首页 > 解决方案 > iTextSharp:即使当前页面有空间,长单元格也会开始到新页面

问题描述

我有一个带有 2 个单元格的 PDFPTable。第一个有 1 行。第二个有 100 行。第二个单元格进入新页面,但我希望它在第一个单元格之后开始。

        Dim cFile As String = "c:\temp\ccc.pdf"

        Dim doc = New Document(PageSize.A4)
        Dim writer As PdfWriter = PdfWriter.GetInstance(doc, New FileStream(cFile, FileMode.Create))
        doc.Open()
        Dim oPdf As PdfContentByte = writer.DirectContent


        Dim baseFont As BaseFont = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, False)
        Dim baseFontBold As BaseFont = BaseFont.CreateFont(BaseFont.TIMES_BOLD, BaseFont.CP1252, False)

        Dim courier As New Font(baseFont, 12)
        Dim bold As New Font(baseFontBold, 12, Font.Bold, Color.RED)

        Dim tb0 As New PdfPTable(1)
        Dim cell0 As New PdfPCell
        Dim ch0 As New Chunk("AA")
        Dim ph0 As New Phrase(ch0)
        cell0.AddElement(ph0)
        tb0.AddCell(cell0)

        'Dim tb1 As New PdfPTable(1)
        Dim cell1 As New PdfPCell
        For i = 1 To 100
            Dim ch1 As New Chunk("CIAO")
            Dim ph1 As New Phrase(ch1)
            cell1.AddElement(ph1)

        Next

        tb0.AddCell(cell1)


        doc.Add(tb0)
        'doc.Add(tb1)


        doc.Close()

我希望第二个单元格保留在第一页但实际转到新页面

标签: vb.netitext

解决方案


SplitLate在你的桌子tb0上设置False,即添加

tb0.SplitLate = False

doc.Add(tb0)

如果SplitLateTrue(默认情况下),iText 尝试通过尽可能长时间地推迟拆分来防止不必要地拆分表行,即尽可能晚地拆分它们。特别是,如果新行从已经有内容的页面开始并且该行不完全适合,iText 首先开始一个新页面并在新页面上绘制该行。


推荐阅读