首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >嵌套在PdfpTable单元格中的PdfpTable两侧有空格

嵌套在PdfpTable单元格中的PdfpTable两侧有空格
EN

Stack Overflow用户
提问于 2016-05-31 22:12:46
回答 1查看 896关注 0票数 0

我有一个主PdfpTable,其中有一列用作父表。然后,我将使用动态列数创建的表添加到父表。

因此,每个表都是父表中的一行。我得到了我想要的,除了几件事:

1)表的左侧和右侧都添加了显著的空白;我希望子表填充行空间。

2)列宽不能保持。每个新表的第一列都会根据它的总列数更改宽度。

3)主表宽度似乎不受.TotalWidth设置的影响

代码语言:javascript
复制
 PdfPTable mainTable = new PdfPTable(1);
    mainTable.TotalWidth = 1000f;

        //Iterating through some data, get the count then create tables.
        PdfPTable subTable = new PdfPTable(colCount);

//I tried setting the widths to fix issue 2
    float[] colWidths = new float[colCount];
                        colWidths[0] = 50f;
                        for (int i = 1; i < colCount; i++)
                        {
                            colWidths[i] = 50f;
                        }
                        subTable.SetWidths(colWidths);

        PdfPCell cell = new PdfPCell();
                                cell.AddElement("test");
                                subTable.AddCell(cell);

         PdfPCell tblCell = new PdfPCell();
                            //tblCell.Padding = 0f;
                            //tblCell.PaddingLeft = 0f;
                            //tblCell.PaddingRight = 0f;
                            tblCell.AddElement(subTable);
                            mainTable.AddCell(tblCell);

我已经尝试设置每列的宽度,删除填充,并设置父表和子表的总宽度,但结果好坏参半。

EN

回答 1

Stack Overflow用户

发布于 2016-05-31 23:08:03

嵌套表有不同的方式,每种不同的方式都有其特定的行为。我已经在NestedTables6示例中演示了这一点:

首先,我们创建主表:

代码语言:javascript
复制
PdfPTable mainTable = new PdfPTable(1);
mainTable.setTotalWidth(1000);
mainTable.setLockedWidth(true);

当我们定义一个总宽度时,我们还必须锁定宽度。我的代码是用Java语言编写的,但是很容易将代码改编成C#。这有点像mainTable.LockedWidth = true; (我对C#不是很精通,所以如果我的C#不完全正确,请原谅我)。

嵌套表的最简单方法是使用addCell()添加表。但是:在本例中,使用默认填充,这意味着您必须将默认填充设置为0。在C#中,这应该是这样的:mainTable.DefaultCell.Padding = 0;在Java语言中是这样做的:

代码语言:javascript
复制
mainTable.getDefaultCell().setPadding(0);
PdfPTable subTable1 = new PdfPTable(5);
subTable1.setTotalWidth(new float[]{200, 200, 200, 100, 300});
subTable1.setLockedWidth(true);
subTable1.addCell("test 1");
subTable1.addCell("test 2");
subTable1.addCell("test 3");
subTable1.addCell("test 4");
subTable1.addCell("test 5");
mainTable.addCell(subTable1);

嵌套表的第二种方法是创建一个以表为参数的PdfPCell。在本例中,默认填充为0

代码语言:javascript
复制
PdfPTable subTable2 = new PdfPTable(5);
subTable2.setTotalWidth(new float[]{200, 100, 200, 200, 300});
subTable2.setLockedWidth(true);
subTable2.addCell("test 1");
subTable2.addCell("test 2");
subTable2.addCell("test 3");
subTable2.addCell("test 4");
subTable2.addCell("test 5");
PdfPCell cell2 = new PdfPCell(subTable2);
mainTable.addCell(cell2);

您正在使用AddElement()方法。这也可以,但您需要将填充设置为0

代码语言:javascript
复制
PdfPTable subTable3 = new PdfPTable(5);
subTable3.setTotalWidth(new float[]{200, 200, 100, 200, 300});
subTable3.setLockedWidth(true);
subTable3.addCell("test 1");
subTable3.addCell("test 2");
subTable3.addCell("test 3");
subTable3.addCell("test 4");
subTable3.addCell("test 5");
PdfPCell cell3 = new PdfPCell();
cell3.setPadding(0);
cell3.addElement(subTable3);
mainTable.addCell(cell3);

请注意,我将表的总宽度定义为1000,并确保子表的所有列宽之和等于1000。我还使用了setTotalWidths()方法,因为我传递的是绝对值(而您传递的是相对值)。

最终结果如下所示:cmp_nested_tables6.pdf

如你所见,两边都没有空格。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37548146

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档