检查是否“continue "”或“;New"页面

本文关键字:quot New 页面 是否 检查 continue | 更新日期: 2023-09-27 18:15:06

我想知道是否有办法检查"新页面"是否因为超出表或编程(通过使用doc.NewPage();)而发生?

如果新页面由于超出表或文本而引起,则需要隐藏头表并显示文本,否则如果新页面以编程方式引起,则需要正常显示头表。

我试图在"OnStartPage"事件中找到一个标志或类似的东西,告诉我页面是否超出,但我什么也没找到。

我希望有人能在这里帮助我。

谢谢!

检查是否“continue "”或“;New"页面

我会看看IPdfPTableEventSplit接口,你可以实现并分配给PdfPTable.TableEvent属性。它有SplitTableTableLayout两种方法。第一个方法在发生表拆分时调用,第二个方法在实际将表写入画布时调用。在第一种方法中,你可以设置一个标志,并在分割发生时禁用标题行,在第二种方法中,你可以把你的内容写出来。

SplitTable方法在添加新页面之前触发,因此您需要跟踪三重状态,"不分割","在下一页上绘制"answers"在本页上绘制"。我把它们打包成一个enum:

[Flags]
public enum SplitState {
    None = 0,
    DrawOnNextPage = 1,
    DrawOnThisPage = 2
}

实现的接口看起来像这样:

public class SplitTableWatcher : IPdfPTableEventSplit {
    /// <summary>
    /// The current table split state
    /// </summary>
    private SplitState currentSplitState = SplitState.None;
    public void SplitTable(PdfPTable table) {
        //Disable header rows for automatic splitting (per OP's request)
        table.HeaderRows = 0;
        //We now need to split on the next page, so append the flag
        this.currentSplitState |= SplitState.DrawOnNextPage;
    }
    public void TableLayout(PdfPTable table, float[][] widths, float[] heights, int headerRows, int rowStart, PdfContentByte[] canvases) {
        //If a split happened and we're on the next page
        if (this.currentSplitState.HasFlag(SplitState.DrawOnThisPage)) {
            //Draw something, nothing too special here
            var cb = canvases[PdfPTable.TEXTCANVAS];
            cb.BeginText();
            cb.SetFontAndSize(BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, false), 18);
            //Use the table's widths and heights to find a spot, this probably could use some tweaking
            cb.SetTextMatrix(widths[0][0], heights[0]);
            cb.ShowText("A Split Happened!");
            cb.EndText();
            //Unset the draw on this page flag, it will be reset below if needed
            this.currentSplitState ^= SplitState.DrawOnThisPage;
        }
        //If we previously had the next page flag set change it to this page
        if (currentSplitState.HasFlag(SplitState.DrawOnNextPage)) {
            this.currentSplitState = SplitState.DrawOnThisPage;
        }
    }
}

最后用一些简单的测试数据实现这个类:

var testFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "test.pdf");
using (var fs = new FileStream(testFile, FileMode.Create, FileAccess.Write, FileShare.None)) {
    using (var doc = new Document()) {
        using (var writer = PdfWriter.GetInstance(doc, fs)) {
            doc.Open();
            var t = new PdfPTable(1);
            //Implement our class
            t.TableEvent = new SplitTableWatcher();
            //Add a single header row
            t.HeaderRows = 1;
            t.AddCell("Header");
            //Create 100 test cells
            for (var i = 1; i < 100; i++) {
                t.AddCell(i.ToString());
            }
            doc.Add(t);
            doc.Close();
        }
    }
}