问题描述
我正在使用ITextSharp将多个表写入文档。
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?
PS我正在使用 table.KeepTogether = true;
。
推荐答案
你可以在模拟模式下使用 ColumnText
来确定元素是否适合特定的矩形。另请参见
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?
例如:
ColumnText ct = new ColumnText(writer.DirectContent);
ct.SetSimpleColumn(36, 36, 559, 806);
ct.AddElement(table);
int status = ct.Go(true);
我用坐标(36,36)
和(559,806)
因为我认为你的A4文件边距为半英寸。如果 ColumnText.HasMoreText(status)
是 true
,则表对象不适合页面。如果该值为 false
,则该表适合页面,您甚至可以使用 ct.GetYLine()
方法找出表结束的Y坐标。
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.
根据此信息,您可以将更改的表或现有表添加到 ColumnText
object,重置简单列并使用 Go(false)
(或 Go()
)添加真实的内容。
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.
参见:
- iText - Strange column- / page changing behaviour with ColumnText
- ColumnText and truncate issue
- How to adjust the page height to the content height?
这篇关于ITextSharp - 如何知道表格是否会转到下一页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!