如何使用C#将嵌套表添加到iText中的现有PDF表中

时间:2018-02-20 15:34:16

标签: c# pdf itext

以下代码为我提供了iText中的表格。

但是,我还想在表的右侧添加另外两列;一个显示另一个' ='标志,(基本上与现有表格的第二列完全相同),然后在右侧,我想添加另一列显示' a' (与现有表格的第1列相同)。

所以我真的想拿现有的表,然后按顺序将第2列和第1列添加到它的末尾,这样我就完成了一个包含5列的表。我需要为此生成嵌套表吗?任何帮助将不胜感激。

PdfPTable table0 = new PdfPTable(3);

float[] widths = new float[] { 7f, 0.5f, 4f };

table0.SetWidths(widths);

PdfPCell cell0 = new PdfPCell(new Phrase("a"));
cell0.Rowspan = 2;
cell0.VerticalAlignment = Element.ALIGN_MIDDLE;
cell0.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right            
table0.AddCell(cell0);

cell0 = new PdfPCell(new Phrase(" = "));
cell0.Rowspan = 2;
cell0.VerticalAlignment = Element.ALIGN_MIDDLE;
cell0.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right            
table0.AddCell(cell0);

cell0 = new PdfPCell(new Phrase("b"));
cell0.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right            
table0.AddCell(cell0);

cell0 = new PdfPCell(new Phrase("c"));
cell0.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right            
table0.AddCell(cell0);

document.Add(table0);

1 个答案:

答案 0 :(得分:0)

“将第2列和第1列添加到”表的末尾,您需要:

  • 为另外两列初始化PdfPTable table0;
  • float[] widths扩展为包含两列以上的宽度;和
  • 添加另外两列的单元格。

E.g。像这样:

PdfPTable table0 = new PdfPTable(5);

float[] widths = new float[] { 7f, 0.5f, 4f, 0.5f, 7f };

table0.SetWidths(widths);

PdfPCell cell0 = new PdfPCell(new Phrase("a"));
cell0.Rowspan = 2;
cell0.VerticalAlignment = Element.ALIGN_MIDDLE;
cell0.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right            
table0.AddCell(cell0);

cell0 = new PdfPCell(new Phrase(" = "));
cell0.Rowspan = 2;
cell0.VerticalAlignment = Element.ALIGN_MIDDLE;
cell0.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right            
table0.AddCell(cell0);

cell0 = new PdfPCell(new Phrase("b"));
cell0.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right            
table0.AddCell(cell0);

cell0 = new PdfPCell(new Phrase(" = "));
cell0.Rowspan = 2;
cell0.VerticalAlignment = Element.ALIGN_MIDDLE;
cell0.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right            
table0.AddCell(cell0);

cell0 = new PdfPCell(new Phrase("a"));
cell0.Rowspan = 2;
cell0.VerticalAlignment = Element.ALIGN_MIDDLE;
cell0.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right            
table0.AddCell(cell0);

cell0 = new PdfPCell(new Phrase("c"));
cell0.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right            
table0.AddCell(cell0);

document.Add(table0);