if语句在新的Cell语句中

huangapple go评论76阅读模式
英文:

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 &lt; 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);

huangapple
  • 本文由 发表于 2020年8月10日 00:08:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/63328576.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定