首页 > 解决方案 > 如何在itext中的表格中遗漏表格宽度

问题描述

我是 iText 的新手。我想用一张桌子生成一个pdf。表由 3 个单元格(单元格 1、单元格 2、单元格 3)组成。

Cell1 和 cell2 数据是动态文本。填满单元格1 和单元格2 后,我想在单元格3 中放置一些文本以留出空间,即该表中的剩余空间。但我无法知道 cell3 的剩余空间,因为 cell1 和 2 是动态的。我添加了固定高度。所以文本会自动截断。如果文本被截断,我想在 cell3 中附加“...”? 示例图像

private static void addData(Document document) throws Exception {

    PdfPTable targetCompanyDetails = new PdfPTable(1);
    targetCompanyDetails.setWidthPercentage(100);
    targetCompanyDetails.setWidths(new float[] { 100 });

    PdfPCell cell = new PdfPCell(
            new Paragraph("", PdfUtil.getFont(FontNames.AVENIRLTCOM_MEDIUM.getFontName(), 12, 1, BaseColor.WHITE)));
    cell.setBorder(0);
    cell.setHorizontalAlignment(Element.ALIGN_LEFT);

    PdfPTable targetCompanyInfo = new PdfPTable(3);
    targetCompanyInfo.setWidthPercentage(100.5f);
    targetCompanyInfo.setWidths(new float[] { 69, 83, 63 });

    PdfPCell firstCell = new PdfPCell();
    //firstCell.setBorderWidth(0);
    firstCell.setFixedHeight(130f);
    Font companyNameFont = PdfUtil.getFont(FontNames.AVENIRLTCOM_85HEAVY.getFontName(), 10F, Font.NORMAL,
            new BaseColor(Color.decode(ColorCodes._336699.getColorCode()).getRGB()));
    Chunk ck = new Chunk("oracle oracle oracle oracle oracle oracle oracle oracle oracle oracle oracle oracle orac oracle oracle oracle orac", companyNameFont);
    Paragraph targetParagraph = new Paragraph();
    targetParagraph.setLeading(11.25f);
    targetParagraph.add(ck);
    PdfPCell targetCompanyName = new PdfPCell();
    targetCompanyName.addElement(targetParagraph);
    PdfPTable targetCompanyDescTable = new PdfPTable(1);
    targetCompanyDescTable.setWidthPercentage(100);
    targetCompanyDescTable.setWidths(new float[] { 100 });
    targetCompanyDescTable.addCell(targetCompanyName);

    firstCell.addElement(targetCompanyDescTable);

    Font sectorFont = PdfUtil.getFont(FontNames.AVENIRLTCOM_85HEAVY.getFontName(), 10F, Font.NORMAL,
            new BaseColor(Color.decode(ColorCodes._336699.getColorCode()).getRGB()));
    Chunk ck1 = new Chunk("oracle Akk soft ", sectorFont);
    Paragraph sectorParagraph = new Paragraph();
    sectorParagraph.setLeading(11.25f);
    sectorParagraph.add(ck1);
    PdfPCell sectorName = new PdfPCell();
    sectorName.addElement(sectorParagraph);
    PdfPTable sectorTable = new PdfPTable(1);
    sectorTable.setWidthPercentage(100);
    sectorTable.setWidths(new float[] { 100 });
    sectorTable.addCell(sectorName);
    firstCell.addElement(sectorTable);




    Font descFont = PdfUtil.getFont(FontNames.AVENIRLTCOM_85HEAVY.getFontName(), 10F, Font.NORMAL,
            new BaseColor(Color.decode(ColorCodes._336699.getColorCode()).getRGB()));
    String content = "As there are 11 entries in the collection this means that the for the final row of the table you only add one cell. Thus, that row is incomplete and won't be drawn.You should either track whether the number of entries you added is even (e.g. by counting or by switching a boolean value); if it is not even, you should finally add another, empty cell.(Or if you want to rely on iText not drawing incomplete rows, simply always add an empty cell after the loop.)";
    Chunk ck2 = new Chunk(content, descFont);
    Paragraph descParagraph = new Paragraph();
    descParagraph.setLeading(11.25f);
    descParagraph.add(ck2);
    PdfPCell descName = new PdfPCell();
    descName.addElement(descParagraph);
    PdfPTable descTable = new PdfPTable(1);
    descTable.setWidthPercentage(100);
    descTable.setWidths(new float[] { 100 });
    descTable.addCell(descName);
    firstCell.addElement(descTable);


    PdfPCell secondCell = new PdfPCell();
    //secondCell.setBorderWidth(0);
    secondCell.setFixedHeight(130f);

    PdfPCell thiredCell = new PdfPCell();
    //thiredCell.setBorderWidth(0);
    thiredCell.setFixedHeight(130f);


    targetCompanyInfo.addCell(firstCell);
    targetCompanyInfo.addCell(secondCell);
    targetCompanyInfo.addCell(thiredCell);
    cell.addElement(targetCompanyInfo);
    targetCompanyDetails.addCell(cell);
    document.add(targetCompanyDetails);

    float totalWidth = targetCompanyDescTable.getTotalWidth();
    System.out.println("totalWidth : " + totalWidth);
    System.out.println("totalWidth : " + targetCompanyDetails.getTotalWidth());
    System.out.println("totalWidth : " + targetCompanyInfo.getTotalWidth());
}

标签: javaitext

解决方案


推荐阅读