该模型接下来提供表格行中插入列的数据。研究了http://itextpdf.com/book/examples.php中的几个示例,但都没有指定当数据来自数据集并转换为 HashMap 时应该如何完成:
PdfPTable tabela = new PdfPTable(columnNumber);
PdfPCell celula;
for (Entry<String, List<String>> item : matriz.entrySet()) {
for (String linha : item.getValue()) {
celula = new PdfPCell(new Phrase(linha, FontFactory.getFont(FontFactory.HELVETICA, 12, Font.BOLD)));
tabela.addCell(celula);
dadosColuna.add(linha);
}
}
我正在尝试以下示例,其中列表数据被插入到单元格中。然而,有趣的是,每个都在一个单独的单元格中给出。
然后我尝试了以下方法:
// Imprimir linhas para cada chave/Coluna do Map
for (Entry<String, List<String>> item : matriz.entrySet()) {
for (String linha : item.getValue()) {
// celula = new PdfPCell(new Phrase(linha, FontFactory.getFont(FontFactory.HELVETICA, 12, Font.BOLD)));
// tabela.addCell(celula);
dadosColuna.add(linha);
}
// Envolve esta linha em uma frase
Phrase phrase = new Phrase();
for(int i = 0; i < dadosColuna.size(); i++)
phrase.add(dadosColuna.get(i).toString());
// phrase.add(dadosColuna.toString());
PdfPCell phraseCell = new PdfPCell();
phraseCell.addElement(phrase);
// Adiciona a celula a tabela
PdfPTable phraseTable = new PdfPTable(numeroColunas);
phraseTable.setSpacingBefore(5);
phraseTable.addCell(phraseCell);
// Aplica a frase em uma outra tabela
Phrase phraseTableWrapper = new Phrase();
phraseTableWrapper.add(phraseTable);
}
该行有bug:phraseTable.addCell(phraseCell);
即使不成功,每次迭代带来的 HashMap 列表也只会暴露在一个单元格中。我想在你的脊椎对应的单元格中输入每个项目。示例:当前问题是:
heading 1, heading 2, heading 3 = OK!
given column 1, column data 1, as column 1, column 2 data given column 2, ...
我正在尝试将此解决方案应用于该问题:http: //itextpdf.com/sandbox/tables/ListInCell