ITextSharp - How to know if a table will go to a next page?

依然范特西╮ 提交于 2019-12-12 06:44:27

问题


I am writing into a document multiple tables using ITextSharp.

How to know if a table will go to a next page, so then I could remove a certain cell?

P.S. I am using table.KeepTogether = true;.


回答1:


You can use ColumnText in simulation mode to find out if elements fit a specific rectangle. See also How to fit a String inside a rectangle?

For instance:

ColumnText ct = new ColumnText(writer.DirectContent);
ct.SetSimpleColumn(36, 36, 559, 806);
ct.AddElement(table);
int status = ct.Go(true);

I used coordinates (36, 36) and (559, 806) because I assume that you have an A4 document with a margin of half an inch. If ColumnText.HasMoreText(status) is true, then the table object didn't fit the page. If that value is false, then the table fits the page and you can even use ct.GetYLine() method to find out the Y coordinate where the table ends.

Based on this information, you can add an altered table or the existing table to a ColumnText object, reset the simple column and use Go(false) (or Go()) to add the content for real.

See also:

  • iText - Strange column- / page changing behaviour with ColumnText
  • ColumnText and truncate issue
  • How to adjust the page height to the content height?


来源:https://stackoverflow.com/questions/33546893/itextsharp-how-to-know-if-a-table-will-go-to-a-next-page

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