1

我的文档中有一个PdfPTable正在使用 iTextSharp 编写的文档。每当表格溢出到另一个页面时,都会重复一些标题行。其中一行包含一个带有一些文本的单元格,例如“John Doe”。

我想要的是在随后的每一页上将该文本更改为“John Doe(续)”。据我所知,OnEndPage如果以某种方式操纵该页面的标题单元格的实例,我需要做一些事情,但我正在努力找出如何准确地在书面内容中找到单元格然后操纵它。

我如何实现这个目标?

4

2 回答 2

4

经过反复试验,我得到了一个可行的解决方案。我实现了IPdfCellEventwhich 声明了一个方法,CellLayout. 根据iText 文档,这是在渲染单元格后调用的,这意味着,第一次调用它时,它已经渲染了表格第一页的单元格。所以,我使用这个调用来添加额外的文本,以便所有后续渲染都包含额外的文本。

这是我的接口实现:

private class ContinuedCellEvent : IPdfPCellEvent
{
    public void CellLayout( PdfPCell cell, Rectangle position, PdfContentByte[] canvases )
    {
        if ( !_continuationApplied )
        {
            // This is called AFTER cell rendering so this should set the cell for the next time it is rendered
            // which will always be on a continuation.
            cell.Phrase.Add( new Chunk( " Continued" ) );

            _continuationApplied = true;
        }
    }

    private bool _continuationApplied;
}

它在定义单元格时使用:

cell.CellEvent = new ContinuedCellEvent();
于 2013-06-03T13:33:41.223 回答
1

使用onEndPage()可以工作,但PdfPTableEventSplit如果我是你,我会实现接口。我会使用一个成员变量cellContent并将其设置为"John Doe"在创建事件实例时。我会cellContenttableLayout()方法中绘制内容并将其内容更改"John Doe (continued)"splitTable()方法中。

试一试并分享您的代码。如果有效,其他人将得到帮助;如果没有,我会看看出了什么问题(但请理解我不是 C# 开发人员;我用 Java 编写了 iText;我不得不雇人将它移植到 C#)。

于 2013-06-01T09:31:00.383 回答