计算iText 7中表格的宽度 [英] Calculate the width of a table in iText 7

查看:576
本文介绍了计算iText 7中表格的宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将iText 7html2pdf 2.1.2版本一起使用,并尝试将HTML文件转换为PDF.

我使用HtmlConverter#convertToElements处理html文件.然后,我想计算其中一个元素(尤其是表格)的宽度.

我尝试实现自定义的TagWorker类,但是只有在表被填充后才能获得表.

也尝试了convertToDocument和其他方法,但没有成功.

是否可以在运行时获取表格宽度?

谢谢.

解决方案

坏消息:通常,只有在放置表格后才能获得表格的实际宽度.高度取决于布局区域.

好消息:可以模拟桌子的布局并检查占用的位置:

    Rectangle area = new Rectangle(0, 0, 500, 500);
    LayoutResult result = table.createRendererSubTree().setParent(doc.getRenderer()).layout(new LayoutContext(new LayoutArea(0, area)));
    Rectangle tableArea = result.getOccupiedArea().getBBox();

在上面的代码中,我为表(createRendererSubTree())创建了一个渲染器,设置了它的父级(setParent(doc.getRenderer())-此行对于正确的属性处理很重要),并对其进行了布局(layout(new LayoutContext(new LayoutArea(0, area)));

UPD:我已经阅读了您的第一个问题,并为您提供了另一个解决方案. 假设您没有要放置的任何区域,但想查看表可以占用多少空间.

然后使用下一个代码段:

MinMaxWidth minMaxWidth = ((TableRenderer)table.createRendererSubTree().setParent(doc.getRenderer())).getMinMaxWidth();
float minWidth = minMaxWidth.getMinWidth();
float maxWidth = minMaxWidth.getMaxWidth();

在上面的代码中,我计算了表格的最小和最大宽度.

I use iText 7 with html2pdf 2.1.2 version and try to convert HTML file to PDF.

I use HtmlConverter#convertToElements to process an html file. Then I would like to calculate the width of one of the elements (a table in particular).

I have tried to implement custom TagWorker class, but i get table only after it's filled.

Also tried convertToDocument and some other ways but with no success.

Is there a way to get a table width at run time?

Thanks in advance.

解决方案

The bad news: in general, it's not possible to get the real width of the table until it has been placed. It highly depends on the layout area.

The good news: one can simulate table's layout and check the occupied place:

    Rectangle area = new Rectangle(0, 0, 500, 500);
    LayoutResult result = table.createRendererSubTree().setParent(doc.getRenderer()).layout(new LayoutContext(new LayoutArea(0, area)));
    Rectangle tableArea = result.getOccupiedArea().getBBox();

In the code above, I've created a renderer for the table (createRendererSubTree()), set its parent (setParent(doc.getRenderer()) - this line is important for correct properties processing) and layouted it (layout(new LayoutContext(new LayoutArea(0, area)));

UPD: I've read your initial question and have another solution for you. Suppose that you don't have any area to place, but want to see how much space the table could occupy.

Use the next snippet then:

MinMaxWidth minMaxWidth = ((TableRenderer)table.createRendererSubTree().setParent(doc.getRenderer())).getMinMaxWidth();
float minWidth = minMaxWidth.getMinWidth();
float maxWidth = minMaxWidth.getMaxWidth();

In the code above I've calculated the min and the max width of the table.

这篇关于计算iText 7中表格的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆