locked
I want to merge cells of same value in the first column in itextsharp for asp.net RRS feed

  • Question

  • User-2036387125 posted

    hi,

    Please help me.

    I want to merge cells of same value in the first column.

     //=============start PDF table===================
            foreach (DataTable table in ds.Tables)
            {
    
                if (first)
    
                    first = false;
    
                else
    
                    if (first != false)    //to prevent execution of the statement: document.NewPage();
                    {
                        document.NewPage();
                    }
    
                //document.Add(FormatHeaderPhrase(table.TableName));
    
                //to insert a blank line
                document.Add(FormatHeaderPhrase(" "));
    
                PdfPTable pdfTable = new PdfPTable(table.Columns.Count);
                PdfPCell CellMerg;
    
                //actual width of table in points
                pdfTable.TotalWidth = 1050f;
                //fix the absolute width of the table
                pdfTable.LockedWidth = true;
    
                //relative col widths in proportions - 1/3 and 2/3
                float[] widths1 = new float[] { 2f, 3f, 1f, 1f, 1f, 1f, 2f };
                pdfTable.SetWidths(widths1);
    
    
                pdfTable.DefaultCell.Padding = 3;
                pdfTable.DefaultCell.PaddingTop = 5f;
                pdfTable.DefaultCell.PaddingBottom = 5f;
                //pdfTable.WidthPercentage = 100; // percentage
    
    
                pdfTable.DefaultCell.BorderWidth = 2;
                pdfTable.DefaultCell.HorizontalAlignment = Element.ALIGN_LEFT;
    
                int cellCountData1 = 0;
                foreach (DataColumn column in table.Columns)
                {
                    if (cellCountData1 <= 7)
                    {
                        pdfTable.AddCell(FormatColumnHeaderPhrase(column.ColumnName));
    
                    }
                    cellCountData1++;
                }
    
                pdfTable.HeaderRows = 1;  // this is the end of the table header
                pdfTable.DefaultCell.BorderWidth = 1;
                Color altRow = new Color(242, 242, 242);
    
                int i = 0;
                foreach (DataRow row in table.Rows)
                {
                    i++;
                    if (i % 2 == 1)
                        pdfTable.DefaultCell.BackgroundColor = altRow;
    
                    int cellCountData = 0;
                    foreach (object cell in row.ItemArray)
                    {
                        if (cellCountData <= 7)
                        {
                            //assume toString produces valid output
                            pdfTable.AddCell(FormatPhrase(cell.ToString()));
    
                        }
                        //=======================================
                        cellCountData++;
                    }
                    if (i % 2 == 1)
    
                        pdfTable.DefaultCell.BackgroundColor = Color.WHITE;
    
                }
    
                document.Add(pdfTable);
                //document.NewPage();
            }
            //=====END PDF table===================
    

    Saturday, July 25, 2015 7:30 AM

Answers

  • User-2036387125 posted

    Thanks for supporting us.

    actually i am not getting all features of itextsharp in Assembly itextsharp.dll, v5.5.6.0 and getting error of color and HeaderFooter error for not found.

    so,now i have used itextSharp version-4.1.6.0 that provide us rowspan feature and all previous implemented features as well.

    in first column we have show 4 categores like Product/Service, Prices, Associated Services, Social and CSR Compliance.

    we have implemented as code below:

            //=============start First PDF table===================
            foreach (DataTable table in dsMain.Tables)
            {
                if (first)
                    first = false;
                else
                    if (first != false)    //to prevent execution of the statement: document.NewPage();
                    {
                        document.NewPage();
                    }
    
                //document.Add(FormatHeaderPhrase(table.TableName));
    
                //to insert a blank line
                document.Add(FormatHeaderPhrase(" "));
    
                PdfPTable pdfTable = new PdfPTable(table.Columns.Count);
    
                //actual width of table in points
                pdfTable.TotalWidth = 1050f;
                //fix the absolute width of the table
                pdfTable.LockedWidth = true;
    
                //relative col widths in proportions - 1/3 and 2/3
                float[] widths1 = new float[] { 2f, 3f, 2f, 1f, 1f, 1f, 2f };
                pdfTable.SetWidths(widths1);
    
    
                pdfTable.DefaultCell.Padding = 3;
                pdfTable.DefaultCell.PaddingTop = 5f;
                pdfTable.DefaultCell.PaddingBottom = 5f;
                //pdfTable.WidthPercentage = 100; // percentage
    
    
                pdfTable.DefaultCell.BorderWidth = 2;
                pdfTable.DefaultCell.HorizontalAlignment = Element.ALIGN_LEFT;
    
                int cellCountData1 = 0;
                foreach (DataColumn column in table.Columns)
                {
                    if (cellCountData1 <= 7)
                    {
                        pdfTable.AddCell(FormatColumnHeaderPhrase(column.ColumnName));
    
                    }
                    cellCountData1++;
                }
    
                pdfTable.HeaderRows = 1;  // this is the end of the table header
                pdfTable.DefaultCell.BorderWidth = 1;
                Color altRow = new Color(242, 242, 242);
    
                int i = 0;
                foreach (DataRow row in table.Rows)
                {
                    i++;
    
                    if (i % 2 == 1)
                        pdfTable.DefaultCell.BackgroundColor = altRow;
    
                    int cellCountData = 0;
                    foreach (object cell in row.ItemArray)
                    {
                        if (cellCountData <= 7)
                        {
                            if (cellCountData == 0)
                            {
                                if (i == 1)
                                {
                                    PdfPCell pdfcell = new PdfPCell(FormatPhrase(cell.ToString()));
                                    pdfcell.BackgroundColor = new Color(242, 242, 242);
                                   // pdfcell.Rowspan = table.Rows.Count;
                                    pdfcell.Rowspan = 6;
                                    pdfTable.AddCell(pdfcell);
                                }
                                if (i == 7)
                                {
                                    PdfPCell pdfcell = new PdfPCell(FormatPhrase(cell.ToString()));
                                    pdfcell.BackgroundColor = new Color(242, 242, 242);
                                    pdfcell.Rowspan = 4;
                                    pdfTable.AddCell(pdfcell);
                                }
                                if (i == 11)
                                {
                                    PdfPCell pdfcell = new PdfPCell(FormatPhrase(cell.ToString()));
                                    pdfcell.BackgroundColor = new Color(242, 242, 242);
                                    pdfcell.Rowspan = 8;
                                    pdfTable.AddCell(pdfcell);
                                }
                                if (i == 19)
                                {
                                    PdfPCell pdfcell = new PdfPCell(FormatPhrase(cell.ToString()));
                                    pdfcell.BackgroundColor = new Color(242, 242, 242);
                                    pdfcell.Rowspan = 6;
                                    pdfTable.AddCell(pdfcell);
                                }
                            }
                            else
                            {
                                pdfTable.AddCell(FormatPhrase(cell.ToString()));
                            }
                        }
                        //=======================================
                        cellCountData++;
                    }
                    if (i % 2 == 1)
    
                        pdfTable.DefaultCell.BackgroundColor = Color.WHITE;
                }
    
                document.Add(pdfTable);
                //document.NewPage();
            }
            ////=====END First PDF table===================
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 28, 2015 8:12 AM

All replies

  • User-2036387125 posted

    I want same like below link

    http://stackoverflow.com/questions/22878410/constant-column-for-multiple-rows

    Saturday, July 25, 2015 7:56 AM
  • User-84896714 posted

    Hi yamunasingh,

    Thank you for your post. According  to your description, you could use Rowspan property to fix it. Change your code as follow.

    foreach (DataRow row in table.Rows)
    {
        i++;
        if (i % 2 == 1)
            pdfTable.DefaultCell.BackgroundColor = altRow;
    
        int cellCountData = 0;
        foreach (object cell in row.ItemArray)
        {
            if (cellCountData <= 7)
            {
                if (cellCountData == 0)
                {
                    if (i == 1)
                    {
                        PdfPCell pdfcell = new PdfPCell(FormatPhrase(cell.ToString()));
                        pdfcell.Rowspan = table.Rows.Count;
                        pdfTable.AddCell(pdfcell);
                    }
                }
                else
                {
                    pdfTable.AddCell(FormatPhrase(cell.ToString()));
                }
            }
            //=======================================
            cellCountData++;
        }
        if (i % 2 == 1)
    
            pdfTable.DefaultCell.BackgroundColor = Color.WHITE;
    
    }

    Best Regards,
    Wang Li

    Sunday, July 26, 2015 9:46 PM
  • User-2036387125 posted

    Sir,

    i have followed your answer but problem is that i am getting two error that is

    1. Error 29 A using namespace directive can only be applied to namespaces; 'iTextSharp.text.pdf.PdfCell'.

    2. Error 30 'iTextSharp.text.pdf.PdfPCell' does not contain a definition for 'Rowspan' and no extension method 'Rowspan' accepting a first argument of type 'iTextSharp.text.pdf.PdfPCell' could be found (are you missing a using directive or an assembly reference?).

    I have already used those namespace in using drictive

    using iTextSharp.text;
    using iTextSharp.text.pdf;
    using System.Data.SqlClient;
    using System.IO;
    using iTextSharp.text.pdf.PdfCell;

    I am using itextsharp version 4.1.2.0 and it is on my project bin folder.

    Please help me.

    Monday, July 27, 2015 1:59 AM
  • User-84896714 posted

    Hi yamunasingh,

    1. Error 29 A using namespace directive can only be applied to namespaces; 'iTextSharp.text.pdf.PdfCell'.

    Please remove using iTextSharp.text.pdf.PdfCell;

    Error 30 'iTextSharp.text.pdf.PdfPCell' does not contain a definition for 'Rowspan' and no extension method 'Rowspan'

    I use Assembly itextsharp.dll, v5.5.6.0. You could upgrade your itextsharp.

    Best Regards,
    Wang Li

    Tuesday, July 28, 2015 1:58 AM
  • User-2036387125 posted

    Thanks for supporting us.

    actually i am not getting all features of itextsharp in Assembly itextsharp.dll, v5.5.6.0 and getting error of color and HeaderFooter error for not found.

    so,now i have used itextSharp version-4.1.6.0 that provide us rowspan feature and all previous implemented features as well.

    in first column we have show 4 categores like Product/Service, Prices, Associated Services, Social and CSR Compliance.

    we have implemented as code below:

            //=============start First PDF table===================
            foreach (DataTable table in dsMain.Tables)
            {
                if (first)
                    first = false;
                else
                    if (first != false)    //to prevent execution of the statement: document.NewPage();
                    {
                        document.NewPage();
                    }
    
                //document.Add(FormatHeaderPhrase(table.TableName));
    
                //to insert a blank line
                document.Add(FormatHeaderPhrase(" "));
    
                PdfPTable pdfTable = new PdfPTable(table.Columns.Count);
    
                //actual width of table in points
                pdfTable.TotalWidth = 1050f;
                //fix the absolute width of the table
                pdfTable.LockedWidth = true;
    
                //relative col widths in proportions - 1/3 and 2/3
                float[] widths1 = new float[] { 2f, 3f, 2f, 1f, 1f, 1f, 2f };
                pdfTable.SetWidths(widths1);
    
    
                pdfTable.DefaultCell.Padding = 3;
                pdfTable.DefaultCell.PaddingTop = 5f;
                pdfTable.DefaultCell.PaddingBottom = 5f;
                //pdfTable.WidthPercentage = 100; // percentage
    
    
                pdfTable.DefaultCell.BorderWidth = 2;
                pdfTable.DefaultCell.HorizontalAlignment = Element.ALIGN_LEFT;
    
                int cellCountData1 = 0;
                foreach (DataColumn column in table.Columns)
                {
                    if (cellCountData1 <= 7)
                    {
                        pdfTable.AddCell(FormatColumnHeaderPhrase(column.ColumnName));
    
                    }
                    cellCountData1++;
                }
    
                pdfTable.HeaderRows = 1;  // this is the end of the table header
                pdfTable.DefaultCell.BorderWidth = 1;
                Color altRow = new Color(242, 242, 242);
    
                int i = 0;
                foreach (DataRow row in table.Rows)
                {
                    i++;
    
                    if (i % 2 == 1)
                        pdfTable.DefaultCell.BackgroundColor = altRow;
    
                    int cellCountData = 0;
                    foreach (object cell in row.ItemArray)
                    {
                        if (cellCountData <= 7)
                        {
                            if (cellCountData == 0)
                            {
                                if (i == 1)
                                {
                                    PdfPCell pdfcell = new PdfPCell(FormatPhrase(cell.ToString()));
                                    pdfcell.BackgroundColor = new Color(242, 242, 242);
                                   // pdfcell.Rowspan = table.Rows.Count;
                                    pdfcell.Rowspan = 6;
                                    pdfTable.AddCell(pdfcell);
                                }
                                if (i == 7)
                                {
                                    PdfPCell pdfcell = new PdfPCell(FormatPhrase(cell.ToString()));
                                    pdfcell.BackgroundColor = new Color(242, 242, 242);
                                    pdfcell.Rowspan = 4;
                                    pdfTable.AddCell(pdfcell);
                                }
                                if (i == 11)
                                {
                                    PdfPCell pdfcell = new PdfPCell(FormatPhrase(cell.ToString()));
                                    pdfcell.BackgroundColor = new Color(242, 242, 242);
                                    pdfcell.Rowspan = 8;
                                    pdfTable.AddCell(pdfcell);
                                }
                                if (i == 19)
                                {
                                    PdfPCell pdfcell = new PdfPCell(FormatPhrase(cell.ToString()));
                                    pdfcell.BackgroundColor = new Color(242, 242, 242);
                                    pdfcell.Rowspan = 6;
                                    pdfTable.AddCell(pdfcell);
                                }
                            }
                            else
                            {
                                pdfTable.AddCell(FormatPhrase(cell.ToString()));
                            }
                        }
                        //=======================================
                        cellCountData++;
                    }
                    if (i % 2 == 1)
    
                        pdfTable.DefaultCell.BackgroundColor = Color.WHITE;
                }
    
                document.Add(pdfTable);
                //document.NewPage();
            }
            ////=====END First PDF table===================
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 28, 2015 8:12 AM