如何在ASP.NET和C#中使用iTextSharp合并PDF中具有相同值的行?

How to merge rows with same value in PDF using iTextSharp, in ASP.NET and C#?

我有下一个问题:

我需要合并具有该表相同值的行:

我需要的表

该表位于GridView中,并且我实现了合并该行的方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
private void AgruparGridView(GridViewRowCollection rows, int indiceInicial, int totalCeldasAgrupar)
{
    //Si no hay celdas a agrupar no realiza ninguna acción
    if (totalCeldasAgrupar == 0)
        return;
    int i, count = 1;
    ArrayList lst = new ArrayList();
    // Los elementos del gridview son llenados en la lista.
    lst.Add(rows[0]);
    var ctrl = rows[0].Cells[indiceInicial];
    //Recorrer los registros que se tengan para agrupar
    for (i = 1; i < rows.Count; i++)
    {
        TableCell nextCell = rows[i].Cells[indiceInicial];
        if (ctrl.Text == nextCell.Text)
        {
            count++;
            nextCell.Visible = false;
            lst.Add(rows[i]);
        }
        else
        {
            if (count > 1)
            {
                ctrl.RowSpan = count;
                AgruparGridView(new GridViewRowCollection(lst), indiceInicial + 1, totalCeldasAgrupar - 1);
            }
            count = 1;
            lst.Clear();
            ctrl = rows[i].Cells[indiceInicial];
            lst.Add(rows[i]);
        }
    }
    if (count > 1)
    {
        ctrl.RowSpan = count;
        AgruparGridView(new GridViewRowCollection(lst), indiceInicial + 1, totalCeldasAgrupar - 1);
    }
    count = 1;
    lst.Clear();
}

我需要使用iTextSharp在PDF报表上实现相同的结果,但是我不知道此过程。

构建本节的代码片段如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
public PdfPTable Tablas(int TipoSeccion, int TipoDoc, int ClaveEntidad, int ClaveSubSis, string descripcion)
    {
        PDFEvents oPDFEvents = new PDFEvents();
        string sError = string.Empty;
        int columns = 1;
        int i = 0;
        DataSet _DatosTabla = null;
        PdfPTable Table;
        PdfPCell CeldaNoBorde = new PdfPCell(CeldasSinBorde("", 1, 1, 4, 255, 255, 255));
        #region PerfilesDOCENTE
        #region Tipo3
        if (TipoSeccion == 3)
        {

            columns = 3;

            Table = new PdfPTable(columns);
            Table.WidthPercentage = 80;
            float[] widhts = new float[] { 15, 50 };
            //Header Superior
            if (this.TipoSeccionOriginal == 3 || this.TipoSeccionOriginal==0)
            {

               Table.AddCell(Celdas("ASIGNATURAS POR EXAMEN DISCIPLINAR PARA FUNCIONES DOCENTES", 2, 3, 8, 238, 233, 233));
                    Table.AddCell(Celdas("área disciplinar", 3, 1, 10, 238, 233, 233));
                    Table.AddCell(Celdas("Asignatura", 3, 1, 10, 238, 233, 233));
                    Table.AddCell(Celdas("Evaluación Disciplinar", 3, 1, 10, 238, 233, 233));
                    _DatosTabla = DameDatosTablaPerfilesTelebach(ClaveEntidad, ClaveSubSis);
            }
            else
            {
                Table.AddCell(Celdas("ASIGNATURAS A CONCURSO PARA FUNCIONES TéCNICO DOCENTES", 1, 2, 8, 238, 233, 233));
                Table.AddCell(Celdas("Evaluación", 2, 1, 10, 238, 233, 233));
                Table.AddCell(Celdas("Asignaturas", 2, 1, 10, 238, 233, 233));
                _DatosTabla = DameDatosTablaPerfiles(TipoDoc, ClaveEntidad, ClaveSubSis, true);
            }

            if (_DatosTabla != null)
            {
                while (i < _DatosTabla.Tables[0].Rows.Count)
                    {
                        Table.AddCell(oPDFEvents.FontPhrase(_DatosTabla.Tables[0].Rows[i][0].ToString(), 10));
                        Table.AddCell(oPDFEvents.FontPhrase(_DatosTabla.Tables[0].Rows[i][1].ToString(), 10));
                        Table.AddCell(oPDFEvents.FontPhrase(_DatosTabla.Tables[0].Rows[i][2].ToString(), 10));
                        i = i + 1;
                    }
            }
            return Table;
        }

我的结果是这样

我的实际结果

对不起,我的英语不好!!
谢谢您的关注!


您需要在适当的PdfPCell上设置Rowspan = n,并省略在该列中添加下一个n-1单元格。 这是一个首先构建常规3x3表的示例。 单元格标记为行,列。 然后在第一个单元格上使用Rowspan = 3构建表。 不添加第四(2,1)和第七(3,1)单元。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
Document doc = new Document();
PdfWriter writer =
  PdfWriter.GetInstance(doc, new FileStream("tables.pdf", FileMode.Create));
doc.Open();
doc.Add(new Paragraph("Table without rowspan:"));
PdfPTable table = new PdfPTable(3);
table.SpacingBefore = 10;
table.AddCell(new PdfPCell(new Phrase("1,1")));
table.AddCell(new PdfPCell(new Phrase("1,2")));
table.AddCell(new PdfPCell(new Phrase("1,3")));
table.AddCell(new PdfPCell(new Phrase("2,1")));
table.AddCell(new PdfPCell(new Phrase("2,2")));
table.AddCell(new PdfPCell(new Phrase("2,3")));
table.AddCell(new PdfPCell(new Phrase("3,1")));
table.AddCell(new PdfPCell(new Phrase("3,2")));
table.AddCell(new PdfPCell(new Phrase("3,3")));
doc.Add(table);
doc.Add(new Paragraph("Table with rowspan 3 on first cell:"));
PdfPTable tableWithRowspan = new PdfPTable(3);
tableWithRowspan.SpacingBefore = 10;
PdfPCell cellWithRowspan = new PdfPCell(new Phrase("1,1"));
// The first cell spans 3 rows
cellWithRowspan.Rowspan = 3;
tableWithRowspan.AddCell(cellWithRowspan);
tableWithRowspan.AddCell(new PdfPCell(new Phrase("1,2")));
tableWithRowspan.AddCell(new PdfPCell(new Phrase("1,3")));
// Cell 2,1 does not exist
tableWithRowspan.AddCell(new PdfPCell(new Phrase("2,2")));
tableWithRowspan.AddCell(new PdfPCell(new Phrase("2,3")));
// Cell 3,1 does not exist
tableWithRowspan.AddCell(new PdfPCell(new Phrase("3,2")));
tableWithRowspan.AddCell(new PdfPCell(new Phrase("3,3")));
doc.Add(tableWithRowspan);
doc.Close();

结果:

Table with rowspan