iTextSharp - Dynamic table

0

I'm trying via C # and with iTextSharp create a pdf doc with the following structure: I have a table with 3 Rows In the 1st I have X columns with X years. In the 2nd a Row with the Months (J, F, M ... etc) for each year, below I have another Row that I want to affect the property of the CellBackgroundColor for anything,

I'm having a lot of difficulties doing it, does anyone help me around here? Here is part of the code, I have already moved and remexi much that I will remake again.

        PdfPTable tabelaAnos = new PdfPTable(iAnos);         
        for (int i = 0; i < iAnos; i++)
        {
            PdfPCell cellAnos1 = new PdfPCell();
            AnoInicio = AnoInicio + 1;
            cellAnos1 = new PdfPCell(new Phrase("Ano " + AnoInicio));
            cellAnos1.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
            tabelaAnos.AddCell(cellAnos1);
        }

       for (int i = 1; i < 13; i++)
       {
           PdfPCell cell = new PdfPCell();
           cell = new PdfPCell(new Phrase("Meses com  " + i + " columns"));
           cell.Colspan = 12;
           cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
          tabelaAnos.AddCell(cell);   
        }

    
asked by anonymous 05.05.2015 / 18:53

1 answer

0

Thank you Paul, but in the meantime I settled the matter after much pain.

Here is the solution I found. Thanks for your help.

public static PdfPTable DRDRCriaTabelaCronogramaPDF(DataRow dr, iTextSharp.text.Font FontLinha)
       {
           PdfPTable tabelaCronograma = null;
           int NumAnos = Convert.ToInt16(dr["PeriodoAnosDiff"]);
           int AnoInt = Convert.ToInt16(dr["AnoInicio"]);

           tabelaCronograma = new PdfPTable(NumAnos * 12);
           tabelaCronograma.WidthPercentage = 100;
           PdfPCell CellMaster = new PdfPCell();
           for (int i = 0; i < NumAnos; i++)
           {
               CellMaster = new PdfPCell(new Phrase((AnoInt++).ToString(), FontLinha));
               CellMaster.HorizontalAlignment = 1;
               CellMaster.Colspan = 12;
               tabelaCronograma.AddCell(CellMaster);
           }
           CellMaster = new PdfPCell();
           int Mes = 1;
           for (int i = 0; i < NumAnos * 12; i++)
           {
               CellMaster = new PdfPCell(new Phrase((GeneralFuncs.DRDRRetornaLetraMes((Mes++).ToString())), FontLinha));
               tabelaCronograma.AddCell(CellMaster);
               if (Mes == 13)
                   Mes = 1;
           }

           Mes = 1;
           int MesInicio = Convert.ToDateTime(dr["DataInicioAcao"]).Month;
           int MesFim = Convert.ToDateTime(dr["DataFimAcao"]).Month;

           int AnoInicio = Convert.ToDateTime(dr["DataInicioAcao"]).Year;
           int AnoFim = Convert.ToDateTime(dr["DataFimAcao"]).Year;

           int AnoInicioProj = Convert.ToDateTime(dr["DataInicio"]).Year;
           int AnoFimProj = Convert.ToDateTime(dr["DataFim"]).Year;

           int IndiceMesInicio = Convert.ToInt16(dr["IndiceMesInicio"]);
           int MesesDeDuracao = Convert.ToInt16(dr["MesesDiff"]);

           for (int i = 0; i < NumAnos * 12; i++)
           {
               if (Mes >= IndiceMesInicio && Mes <= IndiceMesInicio + MesesDeDuracao - 1)
               {
                   CellMaster = new PdfPCell(new Phrase(" "));
                   CellMaster.BackgroundColor = new BaseColor(0, 150, 0);
                   tabelaCronograma.AddCell(CellMaster);
               }
               else
               {
                   CellMaster = new PdfPCell(new Phrase(" "));
                   CellMaster.BackgroundColor = new BaseColor(255, 255, 255);
                   tabelaCronograma.AddCell(CellMaster);
               }
               Mes++;
           }

           return tabelaCronograma;
       }

    
06.05.2015 / 17:49