如何生成使用itext7动态生成的table形式的LinkedHashMap

How to Generate LinkedHashMap in the form of table that has been generated dynamically using itext7

这是动态table生成的代码。任何人都可以帮助将此 table 数据转换为 LinkedHashMap table 格式。

请给我一些合并 LinkedHashMap 中的 table 的想法。

    Table content = new Table(UnitValue.createPercentArray(new float[]{5,5,5}));
    content.setWidth(UnitValue.createPercentValue(100));
    for (int i = 0; i < 2; i++) {
        Cell[] headerFooter = new Cell[] {
                new Cell().setTextAlignment(TextAlignment.CENTER).add(new Paragraph("#")),
                new Cell().setTextAlignment(TextAlignment.CENTER).add(new Paragraph("Key")),
                new Cell().setTextAlignment(TextAlignment.CENTER).add(new Paragraph("Value"))
        };
        for (Cell hfCell : headerFooter) {
            if (i == 0) {
                content.addHeaderCell(hfCell);
            } else {
                //content.addFooterCell(hfCell);
            }
        }
    }

    for(int counter = 1; counter <4; counter++){

        Cell[] contentTable = new Cell[]{
                new Cell(2,2).setTextAlignment(TextAlignment.CENTER).add(new Paragraph(String.valueOf(counter))).setBackgroundColor(ColorConstants.LIGHT_GRAY),
                new Cell().setTextAlignment(TextAlignment.CENTER).add(new Paragraph("key " + counter)),
                new Cell().setTextAlignment(TextAlignment.CENTER).add(new Paragraph("value " + counter))

        };


        for (Cell tabCell : contentTable){                              
            if(counter % 2 == 0){                   
                tabCell.setBackgroundColor(DeviceGray.WHITE);
            }else {                 
                tabCell.setBackgroundColor(new DeviceGray(0.75f));  
            }

            content.addFooterCell(tabCell);
        }

    }

我做到了。但这可以使用 LinkedHashMap.

在 table 中静态添加数据
    LinkedHashMap<String, String> hash = new LinkedHashMap<String, String>();

    // put values in hashmap
    hash.put("key1", "value1");
    hash.put("key2", "value2");
    hash.put("key3", "value3");

    Table table = new Table(UnitValue.createPercentArray(new float[]{5,5,5}));
    table.setWidth(UnitValue.createPercentValue(100));

    table.addCell(hash.get("key1"));
    table.addCell(hash.get("key2"));
    table.addCell(hash.get("key3"));

    Cell[] headerFooter = new Cell[] {
            new Cell().setTextAlignment(TextAlignment.CENTER).add(new Paragraph("#")),
            new Cell().setTextAlignment(TextAlignment.CENTER).add(new Paragraph("Key")),
            new Cell().setTextAlignment(TextAlignment.CENTER).add(new Paragraph("Value"))
    };
    for (Cell hfCell : headerFooter) {

        table.addHeaderCell(hfCell);

    }
    doc.add(table);

最后,这是我searched.Thanks的输出,大家支持:)

        LinkedHashMap<String, Cell> hash = new LinkedHashMap<>();

        for(int counter = 1; counter <4; counter++){


            Cell[] contentTable = new Cell[]{
                    new Cell().setTextAlignment(TextAlignment.CENTER).add(new Paragraph(String.valueOf(counter))).setBackgroundColor(ColorConstants.LIGHT_GRAY),
                    new Cell().setTextAlignment(TextAlignment.CENTER).add(new Paragraph("key " + counter)),
                    new Cell().setTextAlignment(TextAlignment.CENTER).add(new Paragraph("value " + counter))

            };

            for (Cell tabCell : contentTable){  
                hash.put("key",tabCell);
                if(counter % 2 == 0){                   
                    tabCell.setBackgroundColor(DeviceGray.WHITE);
                }else {                 
                    tabCell.setBackgroundColor(new DeviceGray(0.75f));  
                }

                content.addFooterCell(hash.get("key"));

            }

        }

你的问题还不是很crystal清楚,但是根据你的部分回答,我猜你要找的答案是:

public void enhanceHashMapWithTable() {
    Table content = new Table(UnitValue.createPercentArray(new float[] { 5, 5, 5 }));
    Map<String, Cell> hash = new LinkedHashMap<>();

    for (int counter = 1; counter < 4; counter++) {
        Cell[] contentTable = new Cell[] {
                new Cell().setTextAlignment(TextAlignment.CENTER).add(new Paragraph(String.valueOf(counter)))
                        .setBackgroundColor(ColorConstants.LIGHT_GRAY),
                new Cell().setTextAlignment(TextAlignment.CENTER).add(new Paragraph("key " + counter)),
                new Cell().setTextAlignment(TextAlignment.CENTER).add(new Paragraph("value " + counter))

        };

        for (Cell tabCell : contentTable) {
            hash.put("key " + counter, tabCell);
            if (counter % 2 == 0) {
                tabCell.setBackgroundColor(DeviceGray.WHITE);
            } else {
                tabCell.setBackgroundColor(new DeviceGray(0.75f));
            }

            content.addFooterCell(hash.get("key " + counter));
        }

    }
}

在这种情况下,您的密钥将被动态创建,并且 different/coherent 用于您的每个单元格。