本文介绍了当一行被拆分时,可以使用iTextSharp单元格事件在下一页重复数据吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在pdfptable中有一排很深的一行。此行左侧的列包含一个子表,其中包含许多行。如果深层不能放在一个页面上,我希望将深层分割成多个页面。我可以将SplitLate设置为false,以确保该行拆分。但是,右侧的深色列的数据仅显示在第一页上,我希望在第二页上重复。



有点调查表明,我可以使用单元格事件将文本放入单元格,并在第二页重复。所以我创建了一个单元格事件如下:

 公共类PdfPCellEvent 
实现iTextSharp.text.pdf.IPdfPCellEvent

私有m_text As String
私有m_font As text.pdf.BaseFont

公共子新(ByVal文本为String,字体为text.pdf.BaseFont)
MyBase.New()

m_text = text
m_font = font

End Sub

Public Sub CellLayout(cell As PdfPCell,位置为text.Rectangle,canvases()As PdfContentByte)实现IPdfPCellEvent.CellLayout

Dim cb As PdfContentByte = canvases(PdfPTable.TEXTCANVAS)
cb.BeginText()
cb。 SetFontAndSize(m_font,8)
cb.ShowText(m_text)
cb.EndText()

End Sub

结束类

然后我按如下所示调用:

 code> Dim cell As PdfPCell = New PdfPCell()
Dim bfReport As BaseFont = BaseFont.CreateFont(BaseFont.HELVETICA,BaseFont.CP1252,False)

Dim c ellEvent As New PdfPCellEvent(Text,bfReport)
cell.CellEvent = cellEvent
mainTable.AddCell(cell)

document.Add(mainTable)

这一切都运行。我可以看到,在将mainTable添加到文档之后,单元格事件被调用两次。我认为这个行出现在每个页面上都被调用一次。我也可以看到正确的文本正在传入CellLayout。然而,所得的pdf中没有出现任何文本。我究竟做错了什么?我已经尝试过在CellLayout中添加文本的各种方法,但没有一个工作。

解决方案

此代码片段中缺少一些内容:

  Dim cb As PdfContentByte = canvases(PdfPTable.TEXTCANVAS)
cb.BeginText()
cb。 SetFontAndSize(m_font,8)
cb.ShowText(m_text)
cb.EndText()

您正在创建一个文本块,但您忘记指定坐标。我怀疑在内容流中添加了 BT / ET 序列,但是没有办法告诉文本将显示在页面上。



在单元格布局中,您可以访问 position As text.Rectangle 。您可以向坐标请求位置变量。您可以使用代码片段中的这些坐标(使用 SetTextMatrix()方法),但是如果您不使用 cb.BeginText(),cb.SetFontAndSize(),cb.SetTextMatrix(),cb.ShowText(),cb.EndText()。改用 ColumnText.ShowTextAligned()。您可以将 cb 实例作为参数,以及短语,对齐方式(左,右,中心) ,X,Y坐标和角度。


I have a very deep row in a pdfptable. The columns in the left-hand side of this row contain a sub table which has many rows within it. I would like the deep row to split onto more than one page if it is too deep to fit on one page. I can set SplitLate to false to ensure that the row splits. However, the data for the deep columns on the right hand side only shows on the first page and I would like it to be repeated on the second page.

A bit of investigation suggested that I could use a cell event to put the text into the cells and have it repeated on the second page. So I created a cell event as follows:

Public Class PdfPCellEvent
Implements iTextSharp.text.pdf.IPdfPCellEvent

Private m_text As String
Private m_font As text.pdf.BaseFont

Public Sub New(ByVal text As String, font As text.pdf.BaseFont)
    MyBase.New()

    m_text = text
    m_font = font

End Sub

Public Sub CellLayout(cell As PdfPCell, position As text.Rectangle, canvases() As PdfContentByte) Implements IPdfPCellEvent.CellLayout

    Dim cb As PdfContentByte = canvases(PdfPTable.TEXTCANVAS)
    cb.BeginText()
    cb.SetFontAndSize(m_font, 8)
    cb.ShowText(m_text)
    cb.EndText()

End Sub

End Class

I then call this as follows:

Dim cell As PdfPCell = New PdfPCell()
Dim bfReport As BaseFont = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, False)

Dim cellEvent As New PdfPCellEvent("Text", bfReport)
cell.CellEvent = cellEvent
mainTable.AddCell(cell)

document.Add(mainTable)

This all runs. I can see that the cell event is being called twice after the mainTable is added to the document. I presume it is being called once for each page the row appears on. I can also see that the correct text is being passed into CellLayout. However no text appears in the resulting pdf. What am I doing wrong? I have tried various ways of adding the text in CellLayout but none have worked.

解决方案

There is something missing in this code snippet:

Dim cb As PdfContentByte = canvases(PdfPTable.TEXTCANVAS)
cb.BeginText()
cb.SetFontAndSize(m_font, 8)
cb.ShowText(m_text)
cb.EndText()

You are creating a text block, but you forget to specify coordinates. I suspect that the BT/ET sequence is added to the content stream, but there's no way of telling where the text will show up on the page.

In the cell layout, you have access to position As text.Rectangle. You can ask the position variable for coordinates. You can use these coordinates in the code snippet about (using the SetTextMatrix() method), but it will be easier for you if you don't use cb.BeginText(), cb.SetFontAndSize(), cb.SetTextMatrix(), cb.ShowText(), cb.EndText(). Use ColumnText.ShowTextAligned() instead. You can pass the cb instance as a parameter, along with a Phrase, an alignment (left, right, center), an X,Y coordinate and an angle.

这篇关于当一行被拆分时,可以使用iTextSharp单元格事件在下一页重复数据吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 01:13