0
votes

I'm currently using iText for .pdf generation. One of my methods creates, formats and adds cells to a Table object but there are a number of cells(20-30) for this particular table, and the method itself has grown considerably.

The structure of the method is as follows:

/*create cells*/
Cell c1 = new Cell(new Paragraph("text1", textFont));
//...and so on for 20+ cells

/*format cell with custom method formatCell()*/
formatCell(c1, false);
//...and so on for 20+ cells

/*add cells to table object*/
table1.addCell(c1);
//...and so on again for 20+ cells

Is there a way where i could add the cells to the table without a stream of addCell statements?

EDIT: What I should've mentioned(apologies) is that formatCell will take a boolean parameter that will change on a per cell basis

2
use a list or an array? - user180100
Do you even need to access them later? If not, why even put them in a list? Just put a loop for 20+ iterations, and inside the loop body create a cell, format it, and add it to the table. This assumes the creation, formatting, and adding to the table can all happen at once for each cell, rather than needing to do each step to all cells at the same time as you are doing now. - ajp15243
Based on your edit, you're going to need to add details (or adapt a solution already given on your own) on how you determine what values to pass for creating Paragraphs and for formatCell's boolean, assuming they're all potentially different for different cells. - ajp15243
@ajp15243 The creation parameters will change on a per cell basis, so each cells paragraph will contain different data to other cells and the font will change as well, there will be no pattern to this, as some cells are label cells, some will be "value" cells etc. This also applies to the formatCell method, as not every cell will have a false value. - jbailie1991
@jbailie1991 To me, it sounds like you need to do some more thinking on the design of this, since you need to have some way to determine these different arguments for different types of cells. That kind of question (about program design) isn't entirely appropriate for SO, but should fit fine on programmers.stackexchange.com (that site deals with programming concepts and designs, while SO deals more with implementations). - ajp15243

2 Answers

1
votes

you could add your Cell to an ArrayList, and then iterate throught the List.

For example:

List<Cell> cells = new ArrayList<Cell>();
cells.add(new Cell(new Paragraph("text1", textFont)));
//...and so on for 20+ cells

for(Cell cell:cells){
    formatCell(cell, false);
    table1.addCell(cell);
}

And to create your list of cells, you could create a method like this

private static List<Cell> createCells(Font textFont, String... texts){
    List<Cell> cells = new ArrayList<Cell>();
    for(String text: texts){
        cells.add(new Cell(new Paragraph(text, textFont)));
    }
}
1
votes

You could use a List:

final List<Cell> cells = new ArrayList<>();
/*create cells*/
cells.add(/* c1 */ new Cell(new Paragraph("text1", textFont)));
//...and so on for 20+ cells

/*format cell with custom method formatCell()*/
for (final Cell cell : cells) {
    formatCell(cell, false);
}

/*add cells to table object (or use the previous loop if nothing inbetween) */
for (final Cell cell : cells) {
    table1.addCell(cell);
}