iTextSharp:SplitLate/SplitRows?

发布于 2024-08-03 03:20:10 字数 2099 浏览 7 评论 0原文

我遇到一个问题,我的表格(PdfPTable)可能会超出页面的长度。我尝试查找如何将表格“拆分”到多个页面上,但 iTextSharp 在这方面的记录非常少。有谁知道如何做到这一点,而不需要在页面上选择任意 Y 位置并告诉它拆分(如果存在)?

我研究了 SplitLateSplitRows 属性,但没有关于它们的作用的文档。 编辑 他们什么也不做。

谢谢!

编辑

我希望将表格横向切成两半,因为表格始终适合页面的宽度。这就是说,我希望不垂直适合的行延伸到其下方的下一页。

EDIT2

这是一些代码:

Public Sub BuildPrintableDocument
    Dim doc As New Document(PageSize.LETTER, 0, 0, 0, BOTTOM_MARGIN)
    Dim writer As PdfWriter = PdfWriter.GetInstance(doc, _
        New FileStream("invoice.pdf", FileMode.Create)

    Dim footer As New HeaderFooter(New Phrase("www.columbussupply.com", _
        footerFont), False)
    footer.Border = Rectangle.NO_BORDER
    footer.Alignment = HeaderFooter.ALIGN_CENTER
    doc.Footer = footer

    doc.Open()

....

Dim items As PdfPTable = NewItemTable()
Dim count As Integer = 0
    For Each oi As OrderItem In TheInvoice.Items
        If oi.Status <> OrderItem.OrderItemStatus.Cancelled Then
            Dim qty As New PdfPCell(New Phrase(oi.Quantity, mainFont))
            qty.HorizontalAlignment = Element.ALIGN_CENTER
            qty.Padding = ITEMS_PADDING

            '...instantiate 3 other cells here (removed for repetitiveness)'

            items.AddCell(qty)
            items.AddCell(desc)
            items.AddCell(price)
            items.AddCell(total)
        End If
    Next

    items.WriteSelectedRows(0, -1, LEFT_MARGIN, GetItemsStartY, _
        writer.DirectContent)
End Sub


Protected Function NewItemTable() As PdfPTable
    Dim items As PdfPTable = New PdfPTable(4)
    Dim headers() As String = {"QTY", "DESCRIPTION", "PRICE", "TOTAL"}

    For Each s As String In headers
        Dim cell As New PdfPCell(New Phrase(s, mainFont))
        cell.HorizontalAlignment = Element.ALIGN_CENTER
        items.AddCell(cell)
    Next

    items.TotalWidth = ITEMS_TOTAL_WIDTH
    items.SetWidths(New Single() {QTY_COL_WIDTH, DESC_COL_WIDTH, _ 
        PRICE_COL_WIDTH, TOTALS_COL_WIDTH})
    Return items
End Function

I have an issue where I have a table (PdfPTable) that may extend past the length of the page. I have tried looking up how to "split" a table onto more than one page but iTextSharp is pretty poorly documented in this area. Does anyone know how to do this without choosing an arbitrary Y position on the page and telling it to split if it's there?

I looked into the SplitLate and SplitRows properties, but there's no documentation on what these do. EDIT They do nothing.

Thanks!

EDIT

I'm looking to cut the table in half widthwise as the table will always fit the width of the page. This is to say that I want the rows that don't fit vertically to extend to the next page below it.

EDIT2

Here's some code:

Public Sub BuildPrintableDocument
    Dim doc As New Document(PageSize.LETTER, 0, 0, 0, BOTTOM_MARGIN)
    Dim writer As PdfWriter = PdfWriter.GetInstance(doc, _
        New FileStream("invoice.pdf", FileMode.Create)

    Dim footer As New HeaderFooter(New Phrase("www.columbussupply.com", _
        footerFont), False)
    footer.Border = Rectangle.NO_BORDER
    footer.Alignment = HeaderFooter.ALIGN_CENTER
    doc.Footer = footer

    doc.Open()

....

Dim items As PdfPTable = NewItemTable()
Dim count As Integer = 0
    For Each oi As OrderItem In TheInvoice.Items
        If oi.Status <> OrderItem.OrderItemStatus.Cancelled Then
            Dim qty As New PdfPCell(New Phrase(oi.Quantity, mainFont))
            qty.HorizontalAlignment = Element.ALIGN_CENTER
            qty.Padding = ITEMS_PADDING

            '...instantiate 3 other cells here (removed for repetitiveness)'

            items.AddCell(qty)
            items.AddCell(desc)
            items.AddCell(price)
            items.AddCell(total)
        End If
    Next

    items.WriteSelectedRows(0, -1, LEFT_MARGIN, GetItemsStartY, _
        writer.DirectContent)
End Sub


Protected Function NewItemTable() As PdfPTable
    Dim items As PdfPTable = New PdfPTable(4)
    Dim headers() As String = {"QTY", "DESCRIPTION", "PRICE", "TOTAL"}

    For Each s As String In headers
        Dim cell As New PdfPCell(New Phrase(s, mainFont))
        cell.HorizontalAlignment = Element.ALIGN_CENTER
        items.AddCell(cell)
    Next

    items.TotalWidth = ITEMS_TOTAL_WIDTH
    items.SetWidths(New Single() {QTY_COL_WIDTH, DESC_COL_WIDTH, _ 
        PRICE_COL_WIDTH, TOTALS_COL_WIDTH})
    Return items
End Function

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花落人断肠 2024-08-10 03:20:10

如果您希望自动跨页拆分行,则应使用 Document.Add() 将表格添加到文档中。然后 SplitLateSplitRows 将按预期工作。

  1. SplitLate = true (默认)时,表将在
    适合页面的下一行。
  2. SplitLate = false 时,该行
    不完全适合页面的内容将被拆分。
  3. SplitRows =
    true
    (默认)不适合页面的行将被分割。
  4. SplitRows = false 时,该行将被省略。

所以

  • SplitLate && SplitRows:不适合页面的行将从下一页开始,如果该行也不适合该页面,则最终会拆分。

  • SplitLate && !SplitRows:不适合页面的行将从下一页开始,如果该行也不适合该页面,则将其省略。

  • !SplitLate && SplitRows:不适合页面的行将被拆分并继续在下一页上,如果对于下一页来说太大,则将再次拆分。

  • !SplitLate && !SplitRows:我对此有点不确定。但从消息来源来看,它与 SplitLate && 相同。 !SplitRows:不适合页面的行将从下一页开始,如果该行也不适合该页面,则将其省略。

但至于你的问题:仅当不需要绝对定位表格时, Document.Add() 才可用。但似乎有一种方法可以通过将表添加到 ColumnText 中(它实际上是一个 ColumnText 对象来完成所有表拆分),然后绝对定位那个ColumnText。我还没有研究过,但一旦有更多时间我就会研究:)

You should add the table to the document using Document.Add() if you want automatic splitting of the rows across pages. Then SplitLate and SplitRows will work as expected.

  1. When SplitLate = true (default) the table will be split before the
    next row that does fit on the page.
  2. When SplitLate = false the row
    that does not fully fit on the page will be split.
  3. When SplitRows =
    true
    (default) the row that does not fit on a page will be split.
  4. When SplitRows = false the row will be omitted.

So

  • SplitLate && SplitRows: A row that does not fit on the page will be started on the next page and eventually split if it does not fit on that page either.

  • SplitLate && !SplitRows: A row that does not fit on the page will be started on the next page and omitted if it does not fit on that page either.

  • !SplitLate && SplitRows: A row that does not fit on the page will be split and continued on the next page and split again if it too large for the next page too.

  • !SplitLate && !SplitRows: I'm a little unsure about this one. But from the sources it looks like it's the same as SplitLate && !SplitRows: A row that does not fit on the page will be started on the next page and omitted if it does not fit on that page either.

But as for your question: Document.Add() will only be usable if the table is not needed to be absolutely positioned. But it's seems like there is a way to do it though by adding the table to a ColumnText (it's actually a ColumnText object that does all the table splitting) and then absolutely positioning that ColumnText. I haven't looked into it yet, but I will as soon as I get a little more time :)

阳光下的泡沫是彩色的 2024-08-10 03:20:10

当我在 iTextSharp 中使用表格时,我发现此资源很有用:

iTextSharp 教程 - 第 5 章:表格

请参阅标题为“大型表”的部分。本教程包括一个示例;我希望你以前没有见过这个。

我不记得跨页面拆分表格是一个问题。但我确实遇到的一个问题是我希望各个能够跨越页面。为此,我将 PdfPTableSplitLate 属性设置为 false。

编辑
我检查了你的代码并将其与我的进行了比较。我看到的最大区别是,我没有使用 PdfPTable.WriteSelectedRows() 方法将 PdfPTable 添加到我的 Document 中。相反,我调用 DocumentAdd() 方法,传入设置了所有单元格的 PdfPTable。 (顺便说一句,我们以类似的方式加载 PdfPCells。)我想知道 PdfPTable 是否通过 WriteSelectedRows()< 写入 Document /code> 导致了你的问题。

如果不添加 HeaderFooter,您还可以查看代码是否有效。

When I was working with tables in iTextSharp, I found this resource useful:

iTextSharp Tutorial - Chapter 5: Tables

See the section entitled 'Large tables'. The tutorial includes a sample; I hope you haven't seen this before.

I don't recall splitting tables across pages being an issue. A problem I did have though was I wanted individual rows to be able to span pages. For this, I set the SplitLate property of my PdfPTable to false.

Edit
I checked through your code and compared it to mine. The big difference I saw was that I'm not adding my PdfPTable to my Document using the PdfPTable.WriteSelectedRows() method. Instead I call the Document's Add() method, passing in my PdfPTable with all the cells set. (BTW we load our PdfPCells in a similar manner.) I wonder if a PdfPTable written to a Document via WriteSelectedRows() is causing your problem.

You can also see if your code works if you don't add the HeaderFooter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文