java 如何让 PdfPTable 动态计算列宽
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7687259/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make PdfPTable calculate column-width dynamically
提问by steffen
I want to create a PdfPTable with columns containing Strings of different lengths. I learned that the width of each cell/column are the same for each column (default) or one can set the fraction of space every column shall occupy.
我想创建一个包含不同长度字符串的列的 PdfPTable。我了解到每列的每个单元格/列的宽度都相同(默认),或者可以设置每列应占用的空间比例。
However, I want the columns to be as wide as required, but not any wider, depending on the data inserted. Assume that the table fits easily on the page (no breaks!). Of course, I can manually step through all my data and calculate the maximum-String-length of each column, setting the properties of the table accordingly, but I wondered whether such a common option is already provided by itext itself.
但是,我希望列的宽度与需要的一样宽,但不能更宽,具体取决于插入的数据。假设表格很容易放在页面上(没有中断!)。当然,我可以手动遍历所有数据并计算每列的maximum-String-length,相应地设置表的属性,但我想知道itext本身是否已经提供了这样的通用选项。
采纳答案by stivlo
iText is not HTML, so it doesn't have options to automatically adjust width, instead is page oriented and so you've to decide your widths, either as percentage or absolute values.
iText 不是 HTML,因此它没有自动调整宽度的选项,而是面向页面的,因此您必须决定宽度,无论是百分比还是绝对值。
I'm afraid that to do what you ask, you've to implement your own extension by measuring the widths, as you said. See how to calculate the string with with iText.
恐怕要按照您的要求进行操作,您必须通过测量宽度来实现自己的扩展,正如您所说。查看如何使用 iText 计算字符串。