如何在itext中的表中省略表宽度

时间:2018-06-19 07:21:28

标签: java itext

我是iText的新手。我想用一个表生成一个pdf。表由3个细胞(cell1,cell2,cell3)组成。

Cell1和cell2数据是动态文本。在填充cell1和cell2之后,我想在cell3中放入一些文本以留出空间,即该表中的剩余空间。但我无法知道cell3的剩余空间,因为cell1和2是动态的。 我添加了固定高度。所以文字会自动截断。我想追加' ...'在cell3中,如果文本被截断? sample image

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());
}

0 个答案:

没有答案