2016-05-31 68 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