java - 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
)。
问题是什么?
解决方案
在 iText 中,并非所有元素都只能接受“简单”文本——一些元素是其他“块”元素的容器,而Text
是叶元素。实际文本由对象Text
或Paragraph
类型表示:
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);
推荐阅读
- arrays - 如何在没有内存泄漏的情况下正确分配 char[]
- ios - 如何使用 Frida 拦截 Swift 类的构造函数?
- postgresql - 将保存点与 JOOQ 和 JUnit 一起使用
- python - 在我的代码中,由于某种原因出现“索引超出范围”的错误,我实际上只是启动了 Python,因此不知道如何解决这个问题
- javascript - js防止textarea模糊
- c++ - Connect 4 Winning 横向功能
- javascript - 多步形式的 Javascript 验证
- javascript - 生成具有要求的随机密码
- java - 确保已安装 Java 并在 Amazon SageMaker Jupyter Notebook 中为“java”设置 PATH
- flutter - 除非我在 initState() 函数中放置断点,否则 Admob 横幅不会显示