首页 > 解决方案 > Cell.add(" Cell Content ") 在 Itext7 版本 7.1.12 中不起作用。这是来自我还是来自 Itext7 ?这是一些代码

问题描述

Cell SubTitle = new Cell().setBold();
Cell CA1Title = new Cell().setBold().setTextAlignment(TextAlignment.CENTER);
Cell CA2Title = new Cell().setBold().setTextAlignment(TextAlignment.CENTER);
Cell ExamTitle = new Cell().setBold().setTextAlignment(TextAlignment.CENTER);
Cell TotalTitle = new Cell().setBold().setTextAlignment(TextAlignment.CENTER);
Cell RemarkTitle = new Cell().setBold();
SubTitle.add("Subject");
CA1Title.add("1st C.A");
CA2Title.add("2nd C.A");
ExamTitle.add("Exam");
TotalTitle.add("Total Score");
RemarkTitle.add("Remark");

该方法Cell.add()不接受参数(String)。

问题是什么?

标签: javaitext7

解决方案


在 iText 中,并非所有元素都只能接受“简单”文本——一些元素是其他“块”元素的容器,而Text是叶元素。实际文本由对象TextParagraph类型表示:

Text text1 = new Text("Text 1");
Paragraph p1 = new Paragraph(text1);
Paragraph p2 = new Paragraph("Text 2");

Cell本身(如其文档所述)只是一个包含其他元素的容器(并为表格提供列/行跨越)。所以要给它添加文本,你需要给它一个Paragraph元素来保持:

Cell myCell = new Cell()
    .add(new Paragraph("My Cell Title"))
    .setBold()
    .setTextAlignment(TextAlignment.CENTER);

推荐阅读