简体   繁体   English

使用iTextSharp的Rowspan无法正常工作

[英]Rowspan with iTextSharp doesn't work properly

I'm trying to create a simple table with iTextSharp, so with C#. 我正在尝试用iTextSharp创建一个简单的表,所以使用C#。 The goal is a table like this one: 目标是这样一个表:

The problem is that if I apply the rowspan as 2 on cell A, iTextSharp does not render the rowspanned cell, this means that the cell have the same height of cell B. Here's the code: 问题是,如果我在单元格A上将rowspan应用为2,则iTextSharp不会渲染rowpanned单元格,这意味着单元格具有相同的单元格B高度。这是代码:

    PdfPTable corporateTable = new PdfPTable(2);
    corporateTable.HeaderRows = 1;
    corporateTable.TotalWidth = pdfWidth - 50;

    PdfPCell vCell = new PdfPCell();
    vCell.Border = Rectangle.BOX;
    vCell.Rowspan = 2;
    vCell.Phrase = new Phrase("A", new Font(fontLh, 7f, 1, BaseColor.BLACK));
    corporateTable.CompleteRow();
    corporateTable.AddCell(vCell);


    PdfPCell vCellx = new PdfPCell();
    vCellx.Phrase = new Phrase("B", new Font(fontLh, 7f, 1, BaseColor.BLACK));
    vCellx.Colspan = 3;
    corporateTable.AddCell(vCellx);

    PdfPCell vCell1 = new PdfPCell();
    vCell1.Phrase = new Phrase("C", new Font(fontValue, 7f, 0, BaseColor.BLACK));
    corporateTable.AddCell(vCell1);

    corporateTable.WriteSelectedRows(0, -1, 100f, 100f, writer.DirectContent);
    document.Close();

What's wrong? 怎么了? I'm using the latest version of the dll. 我正在使用最新版本的DLL。

Well the basic answer is: it works! 那么基本的答案是:它有效! if you add two more cells, you will see that one cell (the one underneath A) is not filled. 如果再添加两个单元格,您将看到一个单元格(A下面的单元格)未填充。

But this is not what you expect (nor did I btw). 但这不是你所期望的(我也没有)。 To achive what you want use nested tables, that means: 要实现您想要的使用嵌套表,这意味着:

  • create a table with two cols 创建一个包含两个cols的表
  • insert cell A into table 将单元格A插入表格中
  • create cell 2 创建单元格2
  • create one more table with 1 col 用1 col创建一个表
    • insert cell B into table 2 将单元格B插入表2中
    • insert cell c into table 2 将单元格c插入表2中
  • insert table 2 into cell 2 将表2插入单元格2中

search for itext rowspan, you will find multiple fully typed out examples. 搜索itext rowspan,你会发现多个完全类型化的例子。

hth 心连心

Mario 马里奥

I think it will work. 我认为它会起作用。 Try to remove following code rows: 尝试删除以下代码行:

corporateTable.CompleteRow();
...
vCellx.Colspan = 3;

The problem is at 问题在于

vCellx.Colspan = 3; 

Use 使用

vCellx.Colspan = 1; 

because you declared pdf table with two columns. 因为你用两列声明了pdf表。 You already added one column with rowspan 3 so you have only another one column and not three columns 您已经添加了一个包含rowspan 3的列,因此您只有另一列而不是三列

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM