0

i have create PDF document to display text box value using iTextSharp in c# asp.net. the pdf created successfully. but the problem is, i am displaying value in table format. i want to do row span and col span. please any one help me with that..

         PdfWriter.GetInstance(pdfDoc, System.Web.HttpContext.Current.Response.OutputStream);
         pdfDoc.Open();
         //Set Font Properties for PDF File
         Font fnt = FontFactory.GetFont("Times New Roman", 14);
         PdfPTable PdfTable = new PdfPTable(2);
         PdfPCell Cell = new PdfPCell();
         PdfTable.TotalWidth = 600f;
         float[] widths = new float[] { 4f, 8f };
         PdfTable.SetWidths(widths);

         PdfPCell cell=new PdfPCell(new Phrase("MOM TABLE HEADER"));
         cell.Rowspan=2;   // this not working
         PdfTable.AddCell("Meeting By");
         PdfTable.AddCell(txtheld.Text);

3 Answers3

1

I think you haven't added rowspan cell to table that's why you have problem. i have tried following code and its working as expected:

enter image description here

 protected void btnExport_Click(object sender, EventArgs e)
    {
        FileStream fs = new FileStream("C://test1.pdf", FileMode.Create, FileAccess.Write, FileShare.None);

        try
        {


            Document pdfDoc = new Document();
            PdfWriter.GetInstance(pdfDoc, fs);
            pdfDoc.Open();
            //Set Font Properties for PDF File
            Font fnt = FontFactory.GetFont("Times New Roman", 14);
            PdfPTable PdfTable = new PdfPTable(2);
            PdfPCell Cell = new PdfPCell();
            PdfTable.TotalWidth = 600f;
            float[] widths = new float[] { 4f, 8f };
            PdfTable.SetWidths(widths);


            PdfPCell cell = new PdfPCell(new Phrase("MOM TABLE HEADER"));
            cell.Rowspan = 2;   // this not working
            PdfTable.AddCell(cell);
            PdfTable.AddCell("Meeting By");
            PdfTable.AddCell("test1");

            pdfDoc.Add(PdfTable);

            pdfDoc.Close();
        }
        catch
        {
        }
        finally
        {
            fs.Close();
        }
    }
Sandeep
  • 1,182
  • 3
  • 11
  • 26
1

If you need to create a table with rowspan and colspan, you can use our PDFFlow library, we have many features that allow you to customize tables. Maybe this would be much easier for you. That's how it works:

    DocumentBuilder.New()
    .AddSection()
        .AddParagraph("Table with colspan and rowspan:")
            .SetBold().SetMarginBottom(5)
    .ToSection()
        .AddTable()
            .SetWidth(300)
            .AddColumnToTable().AddColumnToTable()
            .AddColumnToTable().AddColumnToTable()
            .AddRow()
                .SetBackColor(Color.FromRgba(0, 0.69, 0.94, 1)).SetBold()
                .AddCell("Product")
                    .SetRowSpan(2)
                    .SetVerticalAlignment(VerticalAlignment.Center)
            .ToRow()
                .AddCell("Month")
                    .SetColSpan(3)
                    .SetHorizontalAlignment(HorizontalAlignment.Center)
        .ToTable()
            .AddRow()
                    .SetBackColor(Color.FromRgba(0, 0.69, 0.94, 1)).SetBold()
.SetHorizontalAlignment(HorizontalAlignment.Center)
                    .AddCellToRow().AddCellToRow("January")
                    .AddCellToRow("February").AddCellToRow("March")
            .ToTable()
                .AddRow()
                    .AddCell()
                        .SetHorizontalAlignment(HorizontalAlignment.Left)
                        .AddParagraphToCell("Product 1")
                .ToRow()
                    .SetHorizontalAlignment(HorizontalAlignment.Center)
                    .AddCellToRow("100").AddCellToRow("115").AddCellToRow("103")
            .ToTable()
                .AddRow()
                    .AddCell()
                        .SetHorizontalAlignment(HorizontalAlignment.Left)
                        .AddParagraphToCell("Product 2")
                .ToRow()
                    .SetHorizontalAlignment(HorizontalAlignment.Center)
                    .AddCellToRow("200").AddCellToRow("204").AddCellToRow("207")
            .ToTable()
                .AddRow()
                    .SetBackColor(Color.FromRgba(0, 0.69, 0.94, 1)).SetBold()
                    .AddCellToRow("Total")
                    .AddCell("929")
                        .SetColSpan(3)
                        .SetHorizontalAlignment(HorizontalAlignment.Right)
    .ToDocument().Build("Table.pdf");

The above code snippet generates the Table.pdf document which contains the following table

As you see, you can set the number of columns or rows a cell spans. You can also customize each row and column separately, for example, by defining the font and text alignment for the cells or setting the background color you need. Names of methods in the library are straightforward, so it will be easy to find the feature that you want to implement.

To see more examples, explore our real document samples. You can reproduce these samples or create similar documents by following the steps described in the accompanying articles. Our library is free for non-commercial and open source projects.

Arina
  • 1
  • 2
0

For Create PDF in asp.net using c# with row span & colspan

1) Please install the Install-Package iTextSharp-LGPL nuget

2)For Rowspan

private static void addCellWithRowSpan(PdfPTable table, string text, int rowspan, bool colorStatus, bool fontStatus)
{
    BaseFont bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);
    iTextSharp.text.Font times;
    if (fontStatus == false)
    {
        times = new iTextSharp.text.Font(bfTimes, 8, iTextSharp.text.Font.NORMAL, Color.BLACK);
    }
    else
    {
        times = new iTextSharp.text.Font(bfTimes, 8, iTextSharp.text.Font.BOLD, Color.BLACK);
    }
    PdfPCell cell = new PdfPCell(new Phrase(text, times));
    cell.Rowspan = rowspan;

    cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
    cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE;

    if (colorStatus == true)
    {
        cell.BackgroundColor = new Color(179, 179, 179);
    }
    table.AddCell(cell);
}

3)For Colspan

private static void addCellWithColSpan(PdfPTable table, string text, int colspan, bool colorStatus, bool alignmentStatus, bool fontStatus)
{
    BaseFont bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);
    iTextSharp.text.Font times;
    if (fontStatus == false)
    {
        times = new iTextSharp.text.Font(bfTimes, 8, iTextSharp.text.Font.NORMAL, Color.BLACK);
    }
    else
    {
        times = new iTextSharp.text.Font(bfTimes, 8, iTextSharp.text.Font.BOLD, Color.BLACK);
    }
    PdfPCell cell = new PdfPCell(new Phrase(text, times));
    cell.Colspan = colspan;
    if (alignmentStatus == false)
    {
        cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
        cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE;
    }
    else
    {
        cell.HorizontalAlignment = PdfPCell.ALIGN_RIGHT;
        cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE;
    }
    if (colorStatus == true)
    {
        cell.BackgroundColor = new Color(179, 179, 179);
    }
    table.AddCell(cell);
}

4)Below code will gives you an idea of how to use addCellWithRowSpan and addCellWithColSpan

public void GenerateInvoice()
{
    var doc = new Document(PageSize.A4);

    PdfWriter.GetInstance(doc, new FileStream(@"D:\" + "/Doc19.pdf", FileMode.Create));
    doc.Open();

    PdfPTable headerTable = new PdfPTable(new float[] { 30f, 40f });
    // add a image
    BaseFont bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);
    Font fontH1 = new Font(bfTimes, 9, Font.BOLD);
    PdfPCell imageCell;
    iTextSharp.text.Image logo = iTextSharp.text.Image.GetInstance("ImagePath");
    logo.ScaleToFit(90f, 90f);
    imageCell = new PdfPCell(logo);
    imageCell.Colspan = 1; // either 1 if you need to insert one cell
    imageCell.Border = 0;

    // add a image
    PdfPCell rowCell;
    float[] headerWidths = new float[] { 250f, 250f };
    headerTable.AddCell(imageCell);

    rowCell = new PdfPCell(new Phrase("Invoice No : INV10001\nInvoices Type : ", fontH1));
    rowCell.Border = 0;
    rowCell.PaddingTop = 30f;
    headerTable.AddCell(rowCell);
    headerTable.HorizontalAlignment = 0;
    headerTable.TotalWidth = 700f;
    headerTable.LockedWidth = true;
    headerTable.SetWidths(headerWidths);
    headerTable.DefaultCell.Border = Rectangle.NO_BORDER;
    doc.Add(headerTable);

    Paragraph p = new Paragraph(new Chunk(new iTextSharp.text.pdf.draw.LineSeparator(0.0F, 100.0F, new Color(65, 105, 225), Element.ALIGN_LEFT, 1)));
    doc.Add(p);

    Paragraph p1 = new Paragraph("\n");
    doc.Add(p1);


    PdfPTable ContentTable = new PdfPTable(7);
    ContentTable.HorizontalAlignment = 0;
    ContentTable.TotalWidth = 523f;
    ContentTable.LockedWidth = true;
    float[] widths = new float[] { 20f, 60f, 60f, 60f, 60f, 60f, 60f };
    ContentTable.SetWidths(widths);

    addCellWithRowSpan(ContentTable, "#", 1, true, true);

    addCellWithRowSpan(ContentTable, "Request NO", 1, true, true);
    addCellWithRowSpan(ContentTable, "Shipment Date", 1, true, true);
    addCellWithRowSpan(ContentTable, "VIN", 1, true, true);
    addCellWithColSpan(ContentTable, "Vehicle Name", 2, true, false, true);
    addCellWithRowSpan(ContentTable, "Amount", 1, true, true);

    addCellWithRowSpan(ContentTable, "1", 3, false, false);
    addCellWithRowSpan(ContentTable, "SHC!10002", 1, false, false);
    addCellWithRowSpan(ContentTable, "1-1-1", 1, false, false);
    addCellWithRowSpan(ContentTable, "VIEEEIEWWNdfssssssssssssssssssssssssssssssssssssssssss", 1, false, false);
    addCellWithColSpan(ContentTable, "Vehicle Name", 2, false, false, false);
    addCellWithRowSpan(ContentTable, "254567576", 3, false, false);


    addCellWithRowSpan(ContentTable, "Consignee Name", 1, true, true);
    addCellWithRowSpan(ContentTable, "Notify Name", 1, true, true);
    addCellWithRowSpan(ContentTable, "Port Of Loading", 1, true, true);
    addCellWithRowSpan(ContentTable, "Port Of Destination", 1, true, true);
    addCellWithRowSpan(ContentTable, "Millage", 1, true, true);

    addCellWithRowSpan(ContentTable, "Tata Birla", 1, false, false);
    addCellWithRowSpan(ContentTable, "Dipanki Jadav", 1, false, false);
    addCellWithRowSpan(ContentTable, "POL", 1, false, false);
    addCellWithRowSpan(ContentTable, "POD@", 1, false, false);
    addCellWithRowSpan(ContentTable, "Millagekhefjkhjhf", 1, false, false);




    addCellWithRowSpan(ContentTable, "2", 3, false, false);
    addCellWithRowSpan(ContentTable, "SHC!10002", 1, false, false);
    addCellWithRowSpan(ContentTable, "1-1-1", 1, false, false);
    addCellWithRowSpan(ContentTable, "VIEEEIEWWNdfssssssssssssssssssssssssssssssssssssssssss", 1, false, false);
    addCellWithColSpan(ContentTable, "Vehicle Name", 2, false, false, false);
    addCellWithRowSpan(ContentTable, "254567576", 3, false, false);


    addCellWithRowSpan(ContentTable, "Consignee Name", 1, true, true);
    addCellWithRowSpan(ContentTable, "Notify Name", 1, true, true);
    addCellWithRowSpan(ContentTable, "Port Of Loading", 1, true, true);
    addCellWithRowSpan(ContentTable, "Port Of Destination", 1, true, true);
    addCellWithRowSpan(ContentTable, "Millage", 1, true, true);

    addCellWithRowSpan(ContentTable, "Tata Birla", 1, false, false);
    addCellWithRowSpan(ContentTable, "Dipanki Jadav", 1, false, false);
    addCellWithRowSpan(ContentTable, "POL", 1, false, false);
    addCellWithRowSpan(ContentTable, "POD@", 1, false, false);
    addCellWithRowSpan(ContentTable, "Millagekhefjkhjhf", 1, false, false);

    addCellWithColSpan(ContentTable, "20i083408", 6, true, true, true);
    addCellWithRowSpan(ContentTable, "-----", 1, true, true);

    doc.Add(ContentTable);

    doc.Add(p1);
    Font fontH2 = new Font(bfTimes, 9, Font.BOLD);
    Paragraph p2 = new Paragraph("Company Name \n -----------\nPhone No: ----------------", fontH2);
    doc.Add(p2);
    doc.Close();
}
Dipanki Jadav
  • 360
  • 3
  • 7