英文:
if statement in new Cell statement
问题
我使用iText来创建一个将放入PDF文件中的表格。在使用for循环创建表格时,我想要实现一个if语句,用来检查for循环计数器。如果计数器等于...,那么表格单元将获得特定的格式。我无法确定在哪里放置for循环。以下是我的代码:
for (int counter = 0; counter < 8; counter++) {
Cell cell = new Cell()
.setBorder(Border.NO_BORDER)
.setPadding(1);
if (counter == 6) {
cell.setFont(f);
}
table.addCell(cell);
table.addCell(new Cell()
.setTextAlignment(TextAlignment.LEFT)
.setBorder(Border.NO_BORDER)
.add(new Paragraph(order.getReceiver().getAllReceiverDetails().get(counter)))
.setPadding(1));
table.addCell(new Cell()
.setTextAlignment(TextAlignment.LEFT)
.setBorder(Border.NO_BORDER)
.add(new Paragraph(order.getSender().getAllSenderDetails().get(counter)))
.setPadding(1));
}
希望有所帮助。
英文:
I use iText to create a table that will be put in an PDF-file. While creating a table using a for-loop, I want to implement an if-statement that checks the for-loop counter. If the counter equals... than the table cell will get a specific format. I can't figure out where to put in the for-loop. This is my code so far:
for (int counter = 0; counter < 8; counter++) {
table.addCell(new Cell()
.setBorder(Border.NO_BORDER)
.setPadding(1)
if (counter == 6) {
.setFont(f);
});
table.addCell(new Cell()
.setTextAlignment(TextAlignment.LEFT)
.setBorder(Border.NO_BORDER)
.add(new Paragraph(order.getReceiver().getAllReceiverDetails().get(counter)))
.setPadding(1));
table.addCell(new Cell()
.setTextAlignment(TextAlignment.LEFT)
.setBorder(Border.NO_BORDER)
.add(new Paragraph(order.getSender().getAllSenderDetails().get(counter)))
.setPadding(1));
}
Any help is appreciated.
答案1
得分: 1
只使用变量:
Cell cell1 = new Cell();
cell1.setBorder(Border.NO_BORDER);
// 其他设置
if (condition)
cell.setWhateverYouWant(...);
table.addCell(cell1);
英文:
Just use variables:
Cell cell1 = new Cell();
cell1.setBorder(Border.NO_BORDER);
// other setters
if (condition)
cell.setWhateverYouWant(...);
table.addCell(cell1);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论