在我的iText文档中,我有许多分散在各处的表,每个表只有一行两列。我希望自动缩小最左边的列以适合其内容,并展开最右边的列以填充剩余空间。
这两列的确切内容差别很大,因此无法提前确定确切的宽度。
此屏幕截图中的所有内容都包装在一个外层表格中。每个嵌套表都有两列突出显示为红色和蓝色。我希望将红色列缩小到尽可能窄的范围,而不会强迫文本占据超过必要的行数。

在本例中,红色单元格的内容只是一个段落,但它们可能包含一个具有两个单元格的进一步嵌套的表(这可能面临相同的问题)。
有没有一种简单的方法可以在不指定精确宽度或相对宽度的情况下扩展一列和收缩另一列?
发布于 2017-10-17 17:49:00
如果您使用的是iText7 (并且完全抛弃了表格的布局),则可以通过构建以下示例来实现此外观和布局:
输出如下所示:

用于生成上述输出的代码:
public void createPdf(String dest) throws IOException, FileNotFoundException{
PdfWriter writer = new PdfWriter(dest);
PdfDocument pdfDoc = new PdfDocument(writer);
Document doc = new Document(pdfDoc);
Paragraph p = new Paragraph();
Text t = new Text("Date:").setBold();
p.add(t);
t= new Text("10/12/17").setUnderline();
p.add(t);
p.add(new Tab());
p.add(createTwoPartBorderedText("Catalog Year: ","2017"));
p.add(new Tab());
p.add(createTwoPartBorderedText("L Number","2019284"));
doc.add(p);
doc.close();
}
public Paragraph createTwoPartBorderedText(String contentOne, String contentTwo){
Paragraph container= new Paragraph();
Text one = new Text(contentOne).setBold();
Border solidRed = new SolidBorder(Color.RED,1f);
one.setBorder(solidRed);
container.add(one);
Text two =new Text(contentTwo);
two.setUnderline();
Border solidBlue = new SolidBorder(Color.BLUE,1f);
two.setBorder(solidBlue);
container.add(two);
return container;
}https://stackoverflow.com/questions/46716149
复制相似问题