iTextSharp:SplitLate/SplitRows?
我遇到一个问题,我的表格(PdfPTable)可能会超出页面的长度。我尝试查找如何将表格“拆分”到多个页面上,但 iTextSharp 在这方面的记录非常少。有谁知道如何做到这一点,而不需要在页面上选择任意 Y 位置并告诉它拆分(如果存在)?
我研究了 SplitLate
和 SplitRows
属性,但没有关于它们的作用的文档。 编辑 他们什么也不做。
谢谢!
编辑
我希望将表格横向切成两半,因为表格始终适合页面的宽度。这就是说,我希望不垂直适合的行延伸到其下方的下一页。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您希望自动跨页拆分行,则应使用
Document.Add()
将表格添加到文档中。然后SplitLate
和SplitRows
将按预期工作。SplitLate = true
(默认)时,表将在适合页面的下一行。
SplitLate = false
时,该行不完全适合页面的内容将被拆分。
SplitRows =
(默认)不适合页面的行将被分割。true
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. ThenSplitLate
andSplitRows
will work as expected.SplitLate = true
(default) the table will be split before thenext row that does fit on the page.
SplitLate = false
the rowthat does not fully fit on the page will be split.
SplitRows =
(default) the row that does not fit on a page will be split.true
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 aColumnText
(it's actually aColumnText
object that does all the table splitting) and then absolutely positioning thatColumnText
. I haven't looked into it yet, but I will as soon as I get a little more time :)当我在 iTextSharp 中使用表格时,我发现此资源很有用:
iTextSharp 教程 - 第 5 章:表格
请参阅标题为“大型表”的部分。本教程包括一个示例;我希望你以前没有见过这个。
我不记得跨页面拆分表格是一个问题。但我确实遇到的一个问题是我希望各个行能够跨越页面。为此,我将
PdfPTable
的SplitLate
属性设置为 false。编辑
我检查了你的代码并将其与我的进行了比较。我看到的最大区别是,我没有使用
PdfPTable.WriteSelectedRows()
方法将PdfPTable
添加到我的Document
中。相反,我调用Document
的Add()
方法,传入设置了所有单元格的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 myPdfPTable
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 myDocument
using thePdfPTable.WriteSelectedRows()
method. Instead I call theDocument
'sAdd()
method, passing in myPdfPTable
with all the cells set. (BTW we load ourPdfPCells
in a similar manner.) I wonder if aPdfPTable
written to aDocument
viaWriteSelectedRows()
is causing your problem.You can also see if your code works if you don't add the
HeaderFooter
.