java - Canvas 错误地绘制表 itext7 中的高度单元格

标签 java pdf itext itext7

我绘制表格如下:Draw custom borders for table with more flexibility in itext7

但是我但是对于大数据,表格中的高度单元格绘制得很糟糕。

 PdfDocument pdfDoc = new PdfDocument(new PdfWriter("_testPd/dashed_underline.pdf"));
        Document doc = new Document(pdfDoc, PageSize.A5);

        Table table = new Table(3).useAllAvailableWidth().setFixedLayout();
        table.addCell("Highway System that runs east from the Indiana state line near Lake Michigan through the southern Lower Peninsula to Detroit, then n");
        table.addCell("Highway System that runs east from the Indiana state line near Lake Michigan through the southern Lower Peninsula to Detroit, then n");
        table.addCell("Highway System that runs east from the Indiana state line near Lake Michigan through the southern Lower Peninsula to Detroit, then n");

        table.addCell("Pell morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus");



        table.setNextRenderer(new CustomTableRenderer(table));


        doc.add(table);

        doc.close();

enter image description here

还有一个例子:

  Table table = new Table(3);
    table.addCell("hello1 ");
    table.addCell("hello2 ");
    table.addCell("hello3 ");
    table.addCell("hello4\nWord ");
    table.addCell("hello5 ");

enter image description here

最佳答案

您的自定义渲染器中存在一个小错误:heights列表中的项目表示从最高到最低的行,但您是从最低到最高绘制边框。

应使用以下代码来绘制水平线:

        // Draw horizontal lines
        float curY = getOccupiedAreaBBox().getTop();
        for (int i = 0; i <= heights.size(); i++) {
            canvas.moveTo(getOccupiedAreaBBox().getLeft() - 3, curY);
            canvas.lineTo(getOccupiedAreaBBox().getRight() + 3, curY + r.nextInt(4));
            if (i < heights.size()) {
                float curHeight = heights.get(i);
                curY -= curHeight;
            }
        }

关于java - Canvas 错误地绘制表 itext7 中的高度单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56120453/

相关文章:

java - 通过直接更改字节来更改音频流的音量

java - 如何在android中为pdf查看器制作注释,如突出显示、删除线、下划线、绘制、添加文本等?

javascript - 为什么从 PDF 复制时缺少格式

java - 不会画瓷砖?

java - Android 单选按钮

java - 从列表中添加和检索对象

java - RegEx : Where should I start? 之后分割 PDF

c# - iTextSharp - 从 PictureBox 控件中获取图像并将其插入到 PDF 文件中

maven - JasperReports 库需要定制 iText 版本吗?

c# - 如何展平某些 PDF 表单字段但不是全部