【发布时间】:2021-11-16 15:21:40
【问题描述】:
我正在使用 iText 7.2.0(Java 8、Windows Server 2016)制作表格。
表格需要不溢出(比页面宽),但如果需要的话,可以在单词中间打断单元格事件。
某些单元格的文本未呈现,我收到以下警告:
[main] 警告 com.itextpdf.layout.renderer.RootRenderer - 元素不适合当前区域。
如果我稍微更改一下文本(例如将“i”替换为“I”),它就会起作用。
我该如何解决?这是我的代码或 iText 中的错误吗?
import java.io.FileNotFoundException;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.Style;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.element.Text;
import com.itextpdf.layout.properties.UnitValue;
public class Main {
private static void makeCell(Table table, Style style, String text) {
Cell c = new Cell();
Paragraph p = new Paragraph();
Text t = new Text(text);
c.addStyle(style);
p.add(t);
c.add(p);
table.addCell(c);
}
public static void main(String[] args) throws FileNotFoundException {
PdfWriter writer = new PdfWriter("C:\\Temp\\test.pdf");
PdfDocument pdf = new PdfDocument(writer);
try (Document document = new Document(pdf)) {
Style style = new Style().setBold();
Table table = new Table(UnitValue.createPercentArray(9));
table.useAllAvailableWidth();
table.setFixedLayout();
makeCell(table, style, "Description");
makeCell(table, style, "DescrIption"); /* Capital I */
makeCell(table, style, "Description");
makeCell(table, style, "Description");
makeCell(table, style, "Description");
makeCell(table, style, "DescrIption"); /* Capital I */
makeCell(table, style, "Description");
makeCell(table, style, "Description");
makeCell(table, style, "Description");
document.add(table);
}
}
}
Result - 带有“描述”的单元格不渲染,带有“描述”的单元格渲染。
【问题讨论】: