如何使PdfPTable动态计算列宽 [英] How to make PdfPTable calculate column-width dynamically

查看:478
本文介绍了如何使PdfPTable动态计算列宽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个包含不同长度字符串的列的PdfPTable。我了解到每个单元格/列的宽度对于每列都是相同的(默认),或者可以设置每列应占据的空间分数。

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.

但是,我希望列尽可能宽,但不能更宽,具体取决于插入的数据。假设表格很容易适合页面(没有中断!)。当然,我可以手动遍历所有数据并计算每列的最大字符串长度,相应地设置表的属性,但我想知道这个常用选项是否已由itext本身提供。

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.

推荐答案

iText不是HTML,因此它没有自动调整宽度的选项,而是面向页面,所以你要决定你的宽度,无论是百分比还是绝对值。

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计算字符串

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.

这篇关于如何使PdfPTable动态计算列宽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆