【问题标题】:Check if "Continued" or "New" page检查是“继续”还是“新”页面
【发布时间】:2014-07-28 08:01:38
【问题描述】:

我想知道是否有办法检查“新页面”是由于超出表还是以编程方式(使用doc.NewPage();)而发生?

如果新页面是由于超出表格或文本引起的,我需要隐藏标题表并显示一个文本,否则如果新页面是由编程引起的,我需要正常显示标题表。

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

我希望有人可以在这里帮助我。

谢谢!

【问题讨论】:

  • 如何检查y-coordinate 是否已达到您设定的限制,例如10,然后您将开始一个新页面?
  • 首先,感谢您的快速回答。我可以试试这个,但我有 2 个问题。 1、如何读取当前页面的Y坐标? 2.假设我知道Y坐标,如果是,我该如何编辑标题?
  • 你从右上角开始?就像在我的情况下,我从715(用于肖像)开始,我添加的每一行,我都扣除了25,直到我达到设定的限制,说35,如果达到,我做了doc.NewPage();。跨度>
  • 你从哪里得到数字 715、25,35?
  • 这些是任意数字。

标签: c# itextsharp


【解决方案1】:

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

SplitTable 方法在添加新页面之前被触发,因此您需要跟踪三元状态,“不拆分”、“在下一页上绘制”和“在此绘制”页”。我已将这些打包为枚举:

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

【讨论】:

    猜你喜欢
    • 2010-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多