0

I'm trying to create PDF from the DataTable in web api using ADO.Net. Unfortunately based on filters some times I may get very less records & able to download without any problem. Sometimes may be very huge like 200 thousand of records. When I'm checking in local my system its getting hang while converting the dt to PDF. My code is like below:

private FileContentResult ExportPDF(DataTable dataTable)
{
    string Name = "Logs";
    System.IO.MemoryStream mStream = new System.IO.MemoryStream();
    byte[] content = null;
    try
    {
        string[] columnNames = (from dc in dataTable.Columns.Cast<DataColumn>() select dc.ColumnName).ToArray();
        int count = columnNames.Length;
        object[] array = new object[count];

        dataTable.Rows.Add(array);

        Document pdfDoc = new Document(PageSize.A2, 10f, 10f, 10f, 0f);

        PdfWriter writer = PdfWriter.GetInstance(pdfDoc, mStream);
        int cols = dataTable.Columns.Count;
        int rows = dataTable.Rows.Count;

        HeaderFooter header = new HeaderFooter(new Phrase(Name), false);

        // Remove the border that is set by default  
        header.Border = iTextSharp.text.Rectangle.TITLE;
        // Align the text: 0 is left, 1 center and 2 right.  
        header.Alignment = Element.ALIGN_CENTER;
        pdfDoc.Header = header;
        // Header.  
        pdfDoc.Open();
        iTextSharp.text.Table pdfTable = new iTextSharp.text.Table(cols, rows);
        pdfTable.BorderWidth = 1; pdfTable.Width = 100;
        pdfTable.Padding = 1; pdfTable.Spacing = 4;

        //creating table headers  
        for (int i = 0; i < cols; i++)
        {
            Cell cellCols = new Cell();
            Chunk chunkCols = new Chunk();
            iTextSharp.text.Font ColFont = FontFactory.GetFont(FontFactory.HELVETICA, 14, iTextSharp.text.Font.BOLD, iTextSharp.text.BaseColor.Black);

            chunkCols = new Chunk(dataTable.Columns[i].ColumnName, ColFont);

            cellCols.Add(chunkCols);
            pdfTable.AddCell(cellCols);
        }
        //creating table data (actual result)   

        for (int k = 0; k < rows; k++)
        {
            for (int j = 0; j < cols; j++)
            {
                Cell cellRows = new Cell();
                iTextSharp.text.Font RowFont = FontFactory.GetFont(FontFactory.HELVETICA, 12);
                Chunk chunkRows = new Chunk(dataTable.Rows[k][j].ToString(), RowFont);
                cellRows.Add(chunkRows);

                pdfTable.AddCell(cellRows);
            }
        }

        pdfDoc.Add(pdfTable);
        pdfDoc.Close();

        content = mStream.ToArray();
        return File(content, "application/pdf", "LogReports.pdf");
    }
    catch (Exception ex)
    {
        throw new Exception(ex.Message);
    }
}
Amedee Van Gasse
  • 7,280
  • 5
  • 55
  • 101
Srikanth Reddy
  • 447
  • 1
  • 8
  • 23
  • Save time for repeatable tasks, extract your fonts from the for loops, reuse your columnNames array that you created inside your code. I am not entirely sure about you adding your selfcreated empty array object to your datatables rows (is this a part of the code you left out ). If you just intend to throw an exception, then ditch the try cast, or just do `throw;` so you don't loose your stacktrace afterwards – Icepickle May 15 '20 at 09:01
  • Because of the huge amount of data, you need to reduce the redundancy in the code and simplify your code as much as possible. Here is a good suggestion, hoping to help you: https://stackoverflow.com/a/15483598/12884742 – LouraQ May 16 '20 at 03:03

0 Answers0