java 如何更改单元格的宽度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31988899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 19:28:13  来源:igfitidea点击:

How to change the width of a cell?

javapdf-generationitextpdfptable

提问by 99maas

I need your help in setting the length of a cell's bottom border. Currently, it is showing the bottom border of a cell too long and I need to shorten it. I tried to change the width of the table, but it is not coming properly.

我需要你的帮助来设置单元格底部边框的长度。目前,它显示单元格的底部边框太长,我需要缩短它。我试图改变表格的宽度,但它不正确。

Below is the code:

下面是代码:

Paragraph tableParagraph = new Paragraph();
tableParagraph.setAlignment(Element.ALIGN_LEFT);

PdfPTable table55 = new PdfPTable(2);
table55 = new PdfPTable(new float[] { 6, 6 });
table55.setWidthPercentage(90f);
table55.getDefaultCell().setBorder(PdfPCell.NO_BORDER);

PdfPCell cel2a = new PdfPCell(new Paragraph("Total of Net Profit ", font));

PdfPCell cel2b = new PdfPCell(new Paragraph("100.000" + " USD  ", font));

cel2a.setBorder(Rectangle.NO_BORDER);
cel2b.setBorder(Rectangle.BOTTOM);
cel2a.setLeading(1f, 1.5f);
cel2b.setLeading(1f, 1.5f);

table55.addCell(cel2a);
table55.addCell(cel2b);

回答by Bruno Lowagie

There are different ways to define the width of a cell. To explain the different options, we have to talk about defining the width of the table (all columns) first, and then talk about defining the width of the separate columns.

有多种方法可以定义单元格的宽度。为了解释不同的选项,我们必须先讨论定义表格(所有列)的宽度,然后再讨论定义单独列的宽度。

Width of a table:

桌子的宽度:

Option 1: You don't define an absolute width.

选项 1:您没有定义绝对宽度。

Instead you ask iText to calculate the width based on the available space. The available space is the width of the page minus the size of the left and the right margin.

相反,您要求 iText 根据可用空间计算宽度。可用空间是页面宽度减去左右边距的大小。

If you create a document like this:

如果您创建这样的文档:

Document document = new Document();

Then the width of the page is 595 user units (this is the width of an A4 page) and the width of the margins is 36 user units (these are default values if you don't define the margins explicitly). Hence the available width is 523 user units.

那么页面的宽度是 595 个用户单位(这是 A4 页面的宽度),边距的宽度是 36 个用户单位(如果您没有明确定义边距,这些是默认值)。因此可用宽度为 523 个用户单位。

When you define your table like this:

当你像这样定义你的表时:

PdfPTable table = new PdfPTable(2);

then the table will take up 80% of the available width when you add this table to a page. So if there's an available width of 523, the width of your table will be 418.4 user units.

那么当您将此表格添加到页面时,表格将占用可用宽度的 80%。因此,如果有 523 的可用宽度,则表格的宽度将为 418.4 用户单位。

You can change this by changing the width percentage. For instance if you add:

您可以通过更改宽度百分比来更改此设置。例如,如果您添加:

table.setWidthPercentage(100);

then 100% of the available width will be used and your table will be 523 user units wide.

那么将使用 100% 的可用宽度,您的表格将是 523 个用户单位宽。

Option 2: You define an absolute width.

选项 2:您定义绝对宽度。

Suppose that you are asked to create a table with a width of 4 inches. By default 1 inch is 72 user units, so you need a table of 288 user units.

假设要求您创建一个宽度为 4 英寸的表格。默认情况下,1 英寸是 72 个用户单位,因此您需要一个包含 288 个用户单位的表格。

This can be achieved like this:

这可以像这样实现:

PdfPTable table = new PdfPTable(2);
table.setTotalWidth(288);
table.setLockedWidth(true);

If you forget the line table.setLockedWidth(true);then iText will assume that you want iText to calculate the width based on the (default) width percentage and the available width. Locking the width, switches iText to use the total width as an absolute width.

如果您忘记了该行,table.setLockedWidth(true);则 iText 将假定您希望 iText 根据(默认)宽度百分比和可用宽度计算宽度。锁定宽度,将 iText 切换为使用总宽度作为绝对宽度。

Width of columns:

列宽:

Case 1: relative widths

案例 1:相对宽度

When you don't define any widths, each column will have the same width. This width will be calculated by dividing the width of the table by the number of columns. E.g. if the width of the table is 288 user units, and if the table has two columns, each column will be 144 user units.

当您未定义任何宽度时,每列将具有相同的宽度。该宽度将通过将表格的宽度除以列数来计算。例如,如果表格的宽度为 288 个用户单位,并且表格有两列,则每列将是 144 个用户单位。

Suppose that you want the second column to be three times as wide as the first column, then you can change the relative widths of the columns like this:

假设您希望第二列的宽度是第一列的三倍,那么您可以像这样更改列的相对宽度:

PdfPTable table = new PdfPTable(2);
table.setWidths(new float[] { 1, 3 });

or, if you want to use only one line:

或者,如果您只想使用一行:

PdfPTable table = new PdfPTable(new float[] { 1, 3 });

If the width of tableis 288, the width of the first column will now be 72 and the width of the second column will be 216.

如果 的宽度table为 288,则第一列的宽度现在将为 72,第二列的宽度将为 216。

You'll get the same result if you do this:

如果你这样做,你会得到相同的结果:

PdfPTable table = new PdfPTable(new float[] { 25, 75 });

The widths are relative values. { 1, 3 }is the equivalent of { 25, 75 }.

宽度是相对值。{ 1, 3 }相当于{ 25, 75 }

You did something strange:

你做了一些奇怪的事情:

PdfPTable table55 = new PdfPTable(2);
table55 = new PdfPTable(new float[] { 6, 6 });

In the first line you create a table with two columns of identical width new PdfPTable(2), but you never use that PdfPTable(2), because in the second row you redefine the table55variable as new PdfPTable(new float[]{ 6, 6 } ).

在第一行中,您创建了一个具有两列宽度相同的表格new PdfPTable(2),但您从不使用它PdfPTable(2),因为在第二行中您将table55变量重新定义为new PdfPTable(new float[]{ 6, 6 } )

As { 6, 6 }are relative values, this is equivalent to { 50, 50 }or { 1, 1 }... You're creating a table with two columns with the same width.

{ 6, 6 }相对值一样,这等价于{ 50, 50 }{ 1, 1 }...您正在创建一个包含两列宽度相同的表格。

Case 2: absolute widths

情况 2:绝对宽度

Suppose that you don't want to use relative widths (for instance because the Math of calculating 1/4 of 288 and 3/4 of 288 is too hard) and you want to create a table width two columns of which the first one is 1 inch wide and the second one is 3 inches wide, then you can once again use the principle of the locked width of a table. Take a look at:

假设您不想使用相对宽度(例如因为计算 288 的 1/4 和 288 的 3/4 的数学太难了)并且您想创建一个表格宽度为两列,其中第一列是1英寸宽,第二个是3英寸宽,那么你就可以再次使用锁住桌子宽度的原理了。看一眼:

PdfPTable table = new PdfPTable(2);
table.setTotalWidth(new float[]{ 72, 216 });
table.setLockedWidth(true);

Now we pass an array to the setTotalWidth()method. Widths are no longer relative, and we tell iText to use these absolute widths by locking the width.

现在我们将一个数组传递给该setTotalWidth()方法。宽度不再是相对的,我们通过锁定宽度来告诉 iText 使用这些绝对宽度。

Now the first column (and all the cells in the first column) will be 72 user units (1 inch) wide and the second column will be 216 user units (3 inch) wide.

现在第一列(以及第一列中的所有单元格)的宽度为 72 个用户单位(1 英寸),第二列的宽度为 216 个用户单位(3 英寸)。

Width of the cells:

单元格宽度:

The width of the cells follow the widths of the columns.

单元格的宽度遵循列的宽度。

All of this has, of course, been explained in large detail in the documentation that is available online. See for instance examples such as ColumnWidthsor the entire chapter that is dedicated to tables in the free ebook The Best iText Questions on StackOverflow. One wonders why no one ever reads that documentation. Does it make sense to keep on answering questions if no one is interested in the answers? (Sorry for this philosophical note, it has been a long day.)

当然,所有这些都已在可在线获取的文档中进行了详细解释。例如,请参阅ColumnWidths或免费电子书The Best iText Questions on StackOverflow 中专门介绍表格的整个章节。有人想知道为什么没有人阅读该文档。如果没有人对答案感兴趣,继续回答问题有意义吗?(对不起,这个哲学笔记,这是漫长的一天。)