iTextSharp - Dynamic table

Asked

Viewed 1,937 times

0

I am trying via C# and with iTextSharp create a pdf doc with the following structure: I have a table with 3 rows In the first I have X columns with X years. In the 2nd one Row with the Months (J, F, M...etc) for each year, below I have another Row that I want to affect the property of theBackgroundColor cell for anything,

I’m having a hard time doing it, so can someone help me around here? Here’s part of the code, I’ve been through it and I’ve been through it so much.

        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);   
        }

Tabela que quero criar

  • The procedure is to create a table with years*months columns and use colspan 12 in the first Row in column 1, 13, etc.

  • Yes I tried it, but in theory it is very simple, I have created dozens of tables in html and aspnet, but iText does not behave as we want.

  • Tonight I prepare an example.

2 answers

0

good afternoon here follows my dynamic table with pictures.

       PdfPTable table = new PdfPTable(4);

        PdfPCell cell = new PdfPCell(new Phrase(" "));
        table.AddCell(cell);

        cell = new PdfPCell(new Phrase("Produtos"));
        table.AddCell(cell);

        cell = new PdfPCell(new Phrase("Quantidade"));
        table.AddCell(cell);

        cell = new PdfPCell(new Phrase("Preco"));
        table.AddCell(cell);          
        

        foreach (var item in detalhes_do_pedido)
        {                         
            Image fotos = Image.GetInstance(@"C:\imagens\" + item.Produto.ImagemUrl);
            ViewBag.foto = fotos;
            fotos.ScaleAbsolute(70, 70);
            table.AddCell(fotos);

            cell = new PdfPCell(new Phrase(item.Produto.Nome));
            table.AddCell(cell);

            cell = new PdfPCell(new Phrase(item.Quantidade.ToString()));
            table.AddCell(cell);

            cell = new PdfPCell(new Phrase(item.Produto.Preco.ToString()));
            table.AddCell(cell);
        }

        document.Add(table);

        

0


Thank you Paul, but I resolved the question after much pain.

Here’s the solution I found. Thank you 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;
       }

Imagem final do cronograma

Browser other questions tagged

You are not signed in. Login or sign up in order to post.