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

如何在ASP.NET和C#中使用iTextSharp合并PDF中具有相同值的行?,c#,asp.net,pdf,itextsharp,C#,Asp.net,Pdf,Itextsharp,我有下一个问题: 我需要合并此表中具有相同值的行: 这个表位于GridView中,我实现了合并这些行的方法 private void AgruparGridView(GridViewRowCollection rows, int indiceInicial, int totalCeldasAgrupar) { //Si no hay celdas a agrupar no realiza ninguna acción if (totalCeldasAgrupar == 0)

我有下一个问题:

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

这个表位于GridView中,我实现了合并这些行的方法

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();
}
private void AgruparGridView(GridViewRowCollection行,int indicateinical,int totalCeldasAgrupar)
{
//这是一个无法实现的目标
如果(totalCeldasAgrupar==0)
返回;
int i,计数=1;
ArrayList lst=新的ArrayList();
//格里德维尤酒店是利斯塔酒店的子酒店。
lst.Add(行[0]);
var ctrl=行[0]。单元格[指示];
//登记处的登记员必须遵守本条例
对于(i=1;i1)
{
ctrl.RowSpan=count;
AgruparGridView(新GridViewRowCollection(lst),指示+1,totalCeldasAgrupar-1);
}
计数=1;
lst.Clear();
ctrl=行[i]。单元格[indicinical];
增加(第[i]行);
}
}
如果(计数>1)
{
ctrl.RowSpan=count;
AgruparGridView(新GridViewRowCollection(lst),指示+1,totalCeldasAgrupar-1);
}
计数=1;
lst.Clear();
}
我需要用iTextSharp在PDF报告上实现相同的结果,但我不知道这个过程

构建此部分的代码片段如下:

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;
        }
公共PdfPTable表格(int-TipoSeccion、int-TipoDoc、int-claveintidad、int-ClaveSubSis、字符串描述)
{
PDFEvents oPDFEvents=新的PDFEvents();
string-sError=string.Empty;
int列=1;
int i=0;
数据集_DatosTabla=null;
PDFP表格;
PdfPCell CeldaNoBorde=新的PdfPCell(CeldasSinBorde(“,1,1,4,255,255,255));
#完成任务地区
#Tipo3区域
if(TipoSeccion==3)
{
列=3;
表=新的PdfPTable(列);
表1.1所示百分比=80;
float[]widhts=新的float[]{15,50};
//上等队长
if(this.TipoSeccionOriginal==3 | | this.TipoSeccionOriginal==0)
{
表2.AddCell(Celdas(第2、3、8、238、233、233页)中的“规范性文件”;
表1.AddCell(Celdas(“纪律部队”,3,1,10238233233);
表1.AddCell(Celdas(“Asignatura”,3,1,10238233233));
表1.AddCell(Celdas(“评估学科”,3、1、10、238、233、233));
_DatosTabla=DamedatoStablaperFileTelebach(ClaveEntidad,ClaveSubSis);
}
其他的
{
表1.AddCell(Celdas(“ASIGNATURAS A CONCURSO PARA Functiones TÉCNICO DOCENTES”,1,2,8,238,233,233));
表2.AddCell(Celdas(“Evaluación”,2,1,10238233233));
表2.AddCell(Celdas(“Asignaturas”,2,1,10238233233));
_DatosTabla=数据库文件(TipoDoc、ClaveEntidad、ClaveSubSis、true);
}
如果(_DatosTabla!=null)
{
而(i<_DatosTabla.Tables[0].Rows.Count)
{
Table.AddCell(optdfevents.fontphase(_DatosTabla.Tables[0]。行[i][0]。ToString(),10));
Table.AddCell(optdfevents.fontphase(_DatosTabla.Tables[0]。行[i][1]。ToString(),10));
Table.AddCell(optdfevents.fontphase(_DatosTabla.Tables[0]。行[i][2]。ToString(),10));
i=i+1;
}
}
返回表;
}
我的结果是这样的

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

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

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();
结果是:


当您说“合并具有相同值的行”时,是否意味着将不同行的单元格合并为一个单元格,即一个行跨度?在屏幕截图中,是带有文本“Matemáticas y Ciencias Experimentales”的单元格行跨度为14。是的,在屏幕截图中是在aspx中,但我不知道如何使用iTextSharp导出为PDF,因为这是我的job@MarcoLuna您上面发布的代码仅用于合并
GridView
控件的行。您需要发布与iTextSharp相关的代码。如何导出从ows到PDF,到目前为止,您在iTextSharp中尝试实现了什么?我所知道的是,该方法使用PdfPTable来构建文档,它正在重新加载我用于填充GridView的相同数据集,我编辑该问题以更好地理解我正在实现上述相同的概念,但所有的行跨度行都直接来自数据库,所以,若我们通过数据库,我们是否能实现同样的目标呢。