Check if “Continued” or “New” page

落爺英雄遲暮 提交于 2019-12-11 05:07:03

问题


I would like to know if there is a way to check if the "New Page" happened because of exceeded table or programmatically (by using doc.NewPage();)?

If the new page caused because of exceeded table or text, I need to hide the header table and show a text instead, else if the new page caused programmatically I need to display the header table normally.

I tried to find a flag or something like this in the "OnStartPage" event that show me if the page exceeded or not, but I found nothing.

I hope that some one can help me here.

Thanks!


回答1:


I would look at the IPdfPTableEventSplit interface that you can implement and assign to a PdfPTable.TableEvent property. It has two methods, SplitTable and TableLayout. The first method is called whenever a table split happens, the second is called whenever the table actually gets written to the canvas. In the first method you could set a flag and disable the header rows if a split happened and in the second method you could write your content out.

The SplitTable method is fired before the new page is added so you need to keep track of a trinary state, "no split", "draw on next page" and "draw on this page". I've packaged these up as an enum:

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

The implemented interface would look like this:

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;
        }
    }
}

And finally the actual implementation of that class with some simple test data:

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();
        }
    }
}


来源:https://stackoverflow.com/questions/24990827/check-if-continued-or-new-page

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!