Create multi-column and rows PDF report using C# and VB.Net in ASP.Net

Last Reply one month ago By dharmendr

Posted one month ago

Hi,

I have seen the PDF report sample using Northwind's employee table. What I need to do is a multi-column / rows PDF report as shown below. Have you got such sample?

https://www.corobori.com/sos/2021-04-06_22-00-48.jpg

You are viewing reply posted by: dharmendr one month ago.
Posted one month ago

Hi Corobori,

Check this example. Now please take its reference and correct your code.

Namepsaces

C#

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

VB.Net

Imports System.Data
Imports System.IO
Imports iTextSharp.text
Imports iTextSharp.text.pdf

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    DataTable dt = new DataTable();
    dt.Columns.AddRange(new DataColumn[]
    {
        new DataColumn("Id"),
        new DataColumn("Name"),
        new DataColumn("Country")
    });
    dt.Rows.Add(1, "John Hammond", "United States");
    dt.Rows.Add(2, "Mudassar Khan", "India");
    dt.Rows.Add(3, "Suzanne Mathews", "France");
    dt.Rows.Add(4, "Robert Schidner", "Russia");
    using (MemoryStream memoryStream = new MemoryStream())
    {
        Document document = new Document(PageSize.A4, 200f, 200f, 50f, 50f);
        PdfWriter writer = PdfWriter.GetInstance(document, memoryStream);
        document.Open();
        PdfPTable table = new PdfPTable(3);
        table.TotalWidth = 500f;
        table.LockedWidth = true;
        table.SetWidths(new float[] { 0.33f, 0.33f, 0.33f });
        table.DefaultCell.Border = 0;

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            PdfPCell cell = null;

            cell = new PdfPCell(new Phrase(dt.Rows[i]["Id"].ToString()));
            cell.Border = Rectangle.NO_BORDER;
            cell.BackgroundColor = BaseColor.WHITE;
            cell.PaddingRight = 20f;
            cell.PaddingBottom = 30f;
            table.AddCell(cell);

            cell = new PdfPCell(new Phrase(dt.Rows[i]["Name"].ToString()));
            cell.Border = Rectangle.NO_BORDER;
            cell.BackgroundColor = BaseColor.WHITE;
            cell.PaddingRight = 20f;
            cell.PaddingBottom = 30f;
            table.AddCell(cell);

            cell = new PdfPCell(new Phrase(dt.Rows[i]["Country"].ToString()));
            cell.Border = Rectangle.NO_BORDER;
            cell.BackgroundColor = BaseColor.WHITE;
            cell.PaddingBottom = 30f;
            table.AddCell(cell);
        }

        document.Add(table);
        document.Close();
        byte[] bytes = memoryStream.ToArray();
        memoryStream.Close();
        Response.Clear();
        Response.ContentType = "application/pdf";
        Response.AddHeader("Content-Disposition", "attachment; filename=Employee.pdf");
        Response.ContentType = "application/pdf";
        Response.Buffer = true;
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.BinaryWrite(bytes);
        Response.End();
        Response.Close();
    }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    Dim dt As DataTable = New DataTable()
    dt.Columns.AddRange(New DataColumn() {
                        New DataColumn("Id"),
                        New DataColumn("Name"),
                        New DataColumn("Country")})
    dt.Rows.Add(1, "John Hammond", "United States")
    dt.Rows.Add(2, "Mudassar Khan", "India")
    dt.Rows.Add(3, "Suzanne Mathews", "France")
    dt.Rows.Add(4, "Robert Schidner", "Russia")

    Using memoryStream As MemoryStream = New MemoryStream()
        Dim document As Document = New Document(PageSize.A4, 200.0F, 200.0F, 50.0F, 50.0F)
        Dim writer As PdfWriter = PdfWriter.GetInstance(document, memoryStream)
        document.Open()
        Dim table As PdfPTable = New PdfPTable(3)
        table.TotalWidth = 500.0F
        table.LockedWidth = True
        table.SetWidths(New Single() {0.33F, 0.33F, 0.33F})
        table.DefaultCell.Border = 0

        For i As Integer = 0 To dt.Rows.Count - 1
            Dim cell As PdfPCell = Nothing
            cell = New PdfPCell(New Phrase(dt.Rows(i)("Id").ToString()))
            cell.Border = Rectangle.NO_BORDER
            cell.BackgroundColor = BaseColor.WHITE
            cell.PaddingRight = 20.0F
            cell.PaddingBottom = 30.0F
            table.AddCell(cell)

            cell = New PdfPCell(New Phrase(dt.Rows(i)("Name").ToString()))
            cell.Border = Rectangle.NO_BORDER
            cell.BackgroundColor = BaseColor.WHITE
            cell.PaddingRight = 20.0F
            cell.PaddingBottom = 30.0F
            table.AddCell(cell)

            cell = New PdfPCell(New Phrase(dt.Rows(i)("Country").ToString()))
            cell.Border = Rectangle.NO_BORDER
            cell.BackgroundColor = BaseColor.WHITE
            cell.PaddingBottom = 30.0F
            table.AddCell(cell)
        Next

        document.Add(table)
        document.Close()
        Dim bytes As Byte() = memoryStream.ToArray()
        memoryStream.Close()
        Response.Clear()
        Response.ContentType = "application/pdf"
        Response.AddHeader("Content-Disposition", "attachment; filename=Employee.pdf")
        Response.ContentType = "application/pdf"
        Response.Buffer = True
        Response.Cache.SetCacheability(HttpCacheability.NoCache)
        Response.BinaryWrite(bytes)
        Response.End()
        Response.Close()
    End Using
End Sub

Screenshot