2016-05-31 450 views
0

我有一个主PdfpTable,其中一列用作父表。然后,我将用动态列创建的表添加到父表中。PdfpTable嵌套在PdfpTable单元格两侧都有空白

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

1)在左右两侧添加了显着的空白空间;我想要子表填充行空间。

2)列宽未保留。每个新表格的第一列根据其具有的总列数改变宽度。

3)主表宽度似乎并不受.TotalWidth设置

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

我试图将宽度为每个列中,移除填充,并设置总宽度为两个父和子表,但结果不一。

+0

我没有时间做完整的答案,但有一点困惑是SetWidths()设置了列的_relative_的宽度。所以如果你通过“50,100”,你不会传递50个单位和100个单位,而是说第一列应该得到三分之一(50 /(50 + 100)),第二列应该得到三分之二(100 /(50 + 100))'。相反,您可能希望使用'SetTotalWidth(float [])'以及'LockedWidth' –

回答

0

嵌套表有不同的方法,每种不同的方法都有其自己的特定行为。我在NestedTables6例子证明了这一点:

首先,我们创建了主表:

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中,它是这样完成的:

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

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:我定义的表的总宽度为1000

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相同。我还使用了setTotalWidths()方法,因为我传递的是绝对值(而您正在传递相对值)。

最后的结果是这样的:cmp_nested_tables6.pdf

enter image description here

正如你所看到的,对双方没有空格。

+0

很好的例子,谢谢。我会尝试一下并做出更新。 – JWiley