0

I have exporting large amount of data nearly 50,000 - 60,000 records to pdf using itextsharp,but its giving me memory overflow exception and performance is too slow also.can any one can help me to get out from this problem?My code is given below

Document doc = new Document(PageSize.A4, 10, 10, 40, 35);
        MemoryStream ms = new MemoryStream();
        string path = System.Web.Configuration.WebConfigurationManager.AppSettings["AllotmentExportFilePath"];

        String FilePath = path + "TestPdf.pdf";
        if (File.Exists(FilePath))
        {
            File.Delete(FilePath);
        }
        PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(FilePath, FileMode.Create));
        writer.InitialLeading = 0;
        //writer.CloseStream = false;
        DataTable dt1 = ds.Tables[0];
        DataTable dt2 = ds.Tables[1];
        DataTable dt3 = ds.Tables[2];

        DataTable newDt2 = new DataTable();
        DataTable newDt3 = new DataTable();

        UserSession OUser = (UserSession)HttpContext.Current.Session["User"];
        string wtUnit = OUser.UserWeightAbbriviation;
        string volUnit = OUser.UserVolumeAbbriviation;
        string userRevenueUnit = (OUser.UserCurrencyID == null ? OUser.BUCurrencyAbbriviation : OUser.UserCurrencyAbbriviation);

        //  var colWidthPercentages = new[] { 6f, 10f, 10f, 10f, 20f, 20f, 8f, 8f, 8f };
        // table.SetWidths(colWidthPercentages);
        var normalFont = FontFactory.GetFont(FontFactory.TIMES, 6);
        var boldFont = FontFactory.GetFont(FontFactory.TIMES_BOLD, 6);

        doc.Open();

        PdfPTable table = new PdfPTable(dt1.Columns.Count - 1) { WidthPercentage = 100 };
        table.SplitLate = false;
        for (int j = 0; j < dt1.Columns.Count; j++)
        {
            if (dt1.Columns[j].ColumnName == "Req_Id")
                continue;
            if (dt1.Columns[j].ColumnName == "Wt")
                table.AddCell(new Phrase((dt1.Columns[j].ColumnName).Replace("_", " ") + " (" + wtUnit + ")", boldFont));
            else if (dt1.Columns[j].ColumnName == "Vol")
                table.AddCell(new Phrase((dt1.Columns[j].ColumnName).Replace("_", " ") + " (" + volUnit + ")", boldFont));
            else if (dt1.Columns[j].ColumnName == "Rate" || dt1.Columns[j].ColumnName == "Bid_Price")
                table.AddCell(new Phrase((dt1.Columns[j].ColumnName).Replace("_", " ") + " (" + userRevenueUnit + ") /(" + OUser.UserWeightAbbriviation + ")", boldFont));
            else if (dt1.Columns[j].ColumnName == "ULD_No")
                table.AddCell(new Phrase(dt1.Columns[j].ColumnName.Replace("_No", " #"), boldFont));
            else if (dt1.Columns[j].ColumnName == "Release_Per")
                table.AddCell(new Phrase(dt1.Columns[j].ColumnName.Replace("_Per", " (%)"), boldFont));
            else
                table.AddCell(new Phrase(dt1.Columns[j].ColumnName.Replace("_", " "), boldFont));
        }
        if (dt1.Rows.Count >= 1 && !String.IsNullOrEmpty(Convert.ToString(dt1.Rows[0][0])))
        {

            for (int i = 0; i < dt1.Rows.Count; i++)
            {

                PdfPCell[] cell = new PdfPCell[dt1.Columns.Count - 1];
                int colRequest = 0;
                for (int j = 0; j < dt1.Columns.Count; j++)
                {
                    if (j == 0)
                        continue;
                    cell[colRequest] = new PdfPCell(new Phrase(dt1.Rows[i][j].ToString(), normalFont));
                    colRequest = colRequest + 1;
                }

                PdfPRow row = new PdfPRow(cell);
                table.Rows.Add(row);

                PdfPTable table1 = new PdfPTable(dt2.Columns.Count - 2) { WidthPercentage = 100 };
                //var colWidthPercentages = new[] { 6f, 10f, 10f, 10f, 5f, 20f, 8f, 8f, 8f };
                //table1.SetWidths(colWidthPercentages);
                var rows = (from bb in dt2.AsEnumerable()
                            where (bb.Field<string>("Request_ID") == Convert.ToString(dt1.Rows[i]["Req_Id"]))
                            select bb);


                for (int k = 0; k < dt2.Columns.Count; k++)
                {
                    if (dt2.Columns[k].ColumnName == "Request_ID" || dt2.Columns[k].ColumnName == "Path_ID")
                        continue;
                    if (dt2.Columns[k].ColumnName == "Rec_Wt_Model" || dt2.Columns[k].ColumnName == "Rec_Wt_User")
                        table1.AddCell(new Phrase(dt2.Columns[k].ColumnName.Replace("_", " ") + " (" + wtUnit + ")", boldFont));
                    else if (dt2.Columns[k].ColumnName == "Rec_Vol_Model" || dt2.Columns[k].ColumnName == "Rec_Vol_User")
                        table1.AddCell(new Phrase(dt2.Columns[k].ColumnName.Replace("_", " ") + " (" + volUnit + ")", boldFont));
                    else if (dt2.Columns[k].ColumnName == "Route_Cost")
                        table1.AddCell(new Phrase(dt2.Columns[k].ColumnName.Replace("_", " ") + " (" + userRevenueUnit + ") /(" + OUser.UserWeightAbbriviation + ")", boldFont));
                    else
                        table1.AddCell(new Phrase(dt2.Columns[k].ColumnName.Replace("_", " "), boldFont));
                }
                if (rows.Count() > 0)
                {
                    newDt2 = rows.CopyToDataTable();

                    for (int i1 = 0; i1 < newDt2.Rows.Count; i1++)
                    {
                        PdfPCell[] Innercell = new PdfPCell[newDt2.Columns.Count - 2];
                        int colRoute = 0;
                        for (int j1 = 0; j1 < newDt2.Columns.Count; j1++)
                        {
                            if (j1 == 0 || j1 == 1)
                                continue;
                            Innercell[colRoute] = new PdfPCell(new Phrase(newDt2.Rows[i1][j1].ToString(), normalFont));
                            colRoute = colRoute + 1;
                        }
                        PdfPRow Innerrow = new PdfPRow(Innercell);

                        table1.Rows.Add(Innerrow);

                        PdfPTable table2 = new PdfPTable(dt3.Columns.Count - 2) { WidthPercentage = 100 };
                        var rows1 = (from bb in dt3.AsEnumerable()
                                     where (bb.Field<string>("Request_ID") == Convert.ToString(newDt2.Rows[i1]["Request_ID"])
                                     && bb.Field<string>("Path_ID") == Convert.ToString(newDt2.Rows[i1]["Path_ID"]))
                                     select bb);

                        for (int k = 0; k < dt3.Columns.Count; k++)
                        {
                            if (dt3.Columns[k].ColumnName == "Request_ID" || dt3.Columns[k].ColumnName == "Path_ID")
                                continue;
                            if (dt3.Columns[k].ColumnName == "Flt_No")
                                table2.AddCell(new Phrase(dt3.Columns[k].ColumnName.Replace("_No", " #"), boldFont));
                            else
                                table2.AddCell(new Phrase(dt3.Columns[k].ColumnName.Replace("_", " "), boldFont));
                        }
                        if (rows1.Count() > 0)
                        {
                            newDt3 = rows1.CopyToDataTable();
                            for (int i2 = 0; i2 < newDt3.Rows.Count; i2++)
                            {
                                PdfPCell[] Innercell1 = new PdfPCell[newDt3.Columns.Count - 2];
                                int colLeg = 0;
                                for (int j2 = 0; j2 < newDt3.Columns.Count; j2++)
                                {
                                    if (j2 == 0 || j2 == 1)
                                        continue;
                                    Innercell1[colLeg] = new PdfPCell(new Phrase(newDt3.Rows[i2][j2].ToString(), normalFont));
                                    colLeg = colLeg + 1;
                                }
                                PdfPRow Innerrow1 = new PdfPRow(Innercell1);
                                table2.Rows.Add(Innerrow1);
                            }

                        }
                        else
                        {
                            string rowData = "No child records to display.";
                            PdfPCell[] NoRowcell = new PdfPCell[dt3.Columns.Count - 2];// (new Phrase(rowData));
                            //   NoRowcell[0] = new PdfPCell(table) { Colspan = newDt2.Columns.Count - 2 };
                            NoRowcell[0] = new PdfPCell(new Phrase(rowData, normalFont)) { Colspan = dt3.Columns.Count - 2 };
                            PdfPRow Norow = new PdfPRow(NoRowcell);
                            table2.Rows.Add(Norow);
                        }


                        PdfPCell[] Rowcell1 = new PdfPCell[dt2.Columns.Count - 2];
                        Rowcell1[0] = new PdfPCell(table2) { Colspan = dt3.Columns.Count - 2 };

                        //int diff1 = (dt2.Columns.Count - 2) - (dt3.Columns.Count - 2);

                        //if (diff1 > 0)
                        //{
                        //    Rowcell1[dt3.Columns.Count - 2] = new PdfPCell() { Colspan = 6 };
                        //    //for (var l = newDt3.Columns.Count-2; l < dt2.Columns.Count-2; l++)
                        //    //{
                        //    //    Rowcell1[l] = new PdfPCell();
                        //    //}
                        //}

                        PdfPRow row11 = new PdfPRow(Rowcell1);
                        table1.Rows.Add(row11);
                    }

                }
                else
                {
                    string rowData = "No child records to display.";
                    PdfPCell[] Rowcell1 = new PdfPCell[dt2.Columns.Count - 2];// (new Phrase(rowData));
                    Rowcell1[0] = new PdfPCell(new Phrase(rowData, normalFont)) { Colspan = dt2.Columns.Count - 2 };
                    PdfPRow row11 = new PdfPRow(Rowcell1);
                    table1.Rows.Add(row11);
                }

                PdfPCell[] Rowcell = new PdfPCell[dt1.Columns.Count - 1];
                Rowcell[0] = new PdfPCell(table1) { Colspan = dt2.Columns.Count - 2 };
                int diff = (dt1.Columns.Count - 1) - (dt2.Columns.Count - 2);

                if (diff > 0)
                {
                    Rowcell[dt2.Columns.Count - 2] = new PdfPCell() { Colspan = diff };
                    //for (var l = newDt2.Columns.Count-2; l < dt1.Columns.Count-1; l++)
                    //{
                    //    Rowcell[l] = new PdfPCell();
                    //}
                }

                PdfPRow row1 = new PdfPRow(Rowcell);
                table.Rows.Add(row1);
            }
        }
        else
        {
            string rowData = "No records to display.";
            PdfPCell[] Rowcell1 = new PdfPCell[dt1.Columns.Count - 2];// (new Phrase(rowData));
            Rowcell1[0] = new PdfPCell(new Phrase(rowData, normalFont)) { Colspan = dt1.Columns.Count - 2 };
            PdfPRow row11 = new PdfPRow(Rowcell1);
            table.Rows.Add(row11);
        }

        doc.Add(table);
        doc.Close();
  • 2
    For your own safety, iTextSharp is deliberately slow when you don't fasten your seatbelts. – Bruno Lowagie Aug 04 '16 at 09:22
  • This question was already asked here: [PDF creation using iTextsharp taking too much of time](http://stackoverflow.com/questions/38739843) This question can't be answered because you aren't telling us how you are using iTextSharp. I am going to mark it as duplicate with a question that could help you avoid memory overflow and improve performance, but you should post better questions in the future. In case you didn't understand my previous comment: I was criticizing the low quality of your question. – Bruno Lowagie Aug 04 '16 at 09:25
  • I have closed your question, assuming that you are using `PdfPTable` to store all of your records in memory, instead of gradually writing the records to the document. That's bad use of iTextSharp. It's a bad craftsman who blames his tools. – Bruno Lowagie Aug 04 '16 at 09:27
  • Hi @brunoLowagie, i have attached my code and i am making hierachical level table,can u please guide me how to optimize my code for better performance – Shivani Sharma Aug 04 '16 at 09:39
  • Read [this answer](http://stackoverflow.com/a/15483598/1622493) and your problem will be solved. Also avoid nested tables; use rowspan instead. Those are the two major issues with your code. – Bruno Lowagie Aug 04 '16 at 09:43
  • Its working fine by using rowspan and complete property but taking 15 min for 85000 records because of for loop.For loop taking so much time. – Shivani Sharma Aug 10 '16 at 07:37
  • Now i am using parallel.for loop instead of for loop but its giving me error. – Shivani Sharma Aug 10 '16 at 07:40

0 Answers0