Click here to Skip to main content
15,075,806 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
   Document doc = new Document(PageSize.A4, 10, 10, 40, 35);
//   MemoryStream ms = new MemoryStream();

   string path = System.Web.Configuration.WebConfigurationManager.AppSettings["ExportFilePath"];

   String FilePath = path + "Test" + _exportCSV + ".pdf";
   if (File.Exists(FilePath))
   {
       File.Delete(FilePath);
   }
   PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(FilePath, FileMode.Create));

   writer.InitialLeading = 0;
   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 normalFont = FontFactory.GetFont(FontFactory.TIMES, 4);
   var boldFont = FontFactory.GetFont(FontFactory.TIMES_BOLD, 4);

   doc.Open();

   PdfPTable table = new PdfPTable(dt1.Columns.Count - 1) { WidthPercentage = 100 };
   table.Complete = false;
   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++)
       {
           for (int j = 0; j < dt1.Columns.Count; j++)
           {
               if (j == 0)
                   continue;
               table.AddCell(new Phrase(dt1.Rows[i][j].ToString(), normalFont));
           }
           var rows = (from bb in dt2.AsEnumerable()
                       where (bb.Field<string>("Request_ID") == Convert.ToString(dt1.Rows[i]["Req_Id"]))
                       select bb);

           int colDiff = (dt1.Columns.Count - dt2.Columns.Count) + 2;

           for (int k = 0; k < dt2.Columns.Count; k++)
           {
               PdfPCell cell1 = null;
               if (dt2.Columns[k].ColumnName == "Request_ID" || dt2.Columns[k].ColumnName == "Route_Header_ID" || dt2.Columns[k].ColumnName == "Path_ID")
                   continue;
               if (dt2.Columns[k].ColumnName == "Rec_Wt_Model" || dt2.Columns[k].ColumnName == "Rec_Wt_User")
                   cell1 = new PdfPCell(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")
                   cell1 = new PdfPCell(new Phrase(dt2.Columns[k].ColumnName.Replace("_", " ") + " (" + volUnit + ")", boldFont));
               else if (dt2.Columns[k].ColumnName == "Route_Cost")
                   cell1 = new PdfPCell(new Phrase(dt2.Columns[k].ColumnName.Replace("_", " ") + " (" + userRevenueUnit + ") /(" + OUser.UserWeightAbbriviation + ")", boldFont));
               else
                   cell1 = new PdfPCell(new Phrase(dt2.Columns[k].ColumnName.Replace("_", " "), boldFont));
               table.AddCell(cell1);
               if ((k == dt2.Columns.Count - 1) && colDiff > 0)
               {
                   cell1 = new PdfPCell(new Phrase(""));
                   cell1.Colspan = colDiff;
                   table.AddCell(cell1);
               }
           }


           if (rows.Count() > 0)
           {
               newDt2 = rows.CopyToDataTable();

               for (int i1 = 0; i1 < newDt2.Rows.Count; i1++)
               {
                   for (int j1 = 0; j1 < newDt2.Columns.Count; j1++)
                   {
                       if (j1 == 0 || j1 == 1 || j1 == 2)
                           continue;
                       PdfPCell cell1 = new PdfPCell(new Phrase(newDt2.Rows[i1][j1].ToString(), normalFont));
                       table.AddCell(cell1);

                       if ((j1 == newDt2.Columns.Count - 1) && colDiff > 0)
                       {
                           PdfPCell cell2 = null;
                           var rows3 = (from bb in dt3.AsEnumerable()
                                        where (bb.Field<string>("Request_ID") == Convert.ToString(newDt2.Rows[i1]["Request_ID"])
                                        && bb.Field<string>("Route_Header_ID") == Convert.ToString(newDt2.Rows[i1]["Route_Header_ID"]))
                                        select bb);
                           var totalRows = 0;
                           if (rows3.Count() > 0)
                           {
                                totalRows = rows3.Count() + 1;
                           }
                           cell2 = new PdfPCell(new Phrase(""));
                           cell2.Colspan = colDiff;
                           if (i1 == 0)
                           {
                               totalRows = totalRows + 1;//start end date hedaer
                           }

                           cell2.Rowspan = totalRows + 1;
                           table.AddCell(cell2);
                       }
                   }



                   var rows1 = (from bb in dt3.AsEnumerable()
                                where (bb.Field<string>("Request_ID") == Convert.ToString(newDt2.Rows[i1]["Request_ID"])
                                        && bb.Field<string>("Route_Header_ID") == Convert.ToString(newDt2.Rows[i1]["Route_Header_ID"]))
                                select bb);

                   int colDiff1 = (dt2.Columns.Count - dt3.Columns.Count);

                   for (int k = 0; k < dt3.Columns.Count; k++)
                   {
                       PdfPCell cell1 = null;
                       if (dt3.Columns[k].ColumnName == "Request_ID" || dt3.Columns[k].ColumnName == "Route_Header_ID" || dt3.Columns[k].ColumnName == "Path_ID")
                           continue;
                       if (dt3.Columns[k].ColumnName == "Flt_No")
                           cell1 = new PdfPCell(new Phrase(dt3.Columns[k].ColumnName.Replace("_No", " #"), boldFont));
                       else
                           cell1 = new PdfPCell(new Phrase(dt3.Columns[k].ColumnName.Replace("_", " "), boldFont));
                       table.AddCell(cell1);

                       if ((k == dt3.Columns.Count - 1) && colDiff1 > 0)
                       {
                           cell1 = new PdfPCell(new Phrase(""));
                           cell1.Colspan = colDiff1;
                           cell1.Rowspan = (rows1.Count()) + 1;
                           table.AddCell(cell1);
                       }
                   }
                   if (rows1.Count() > 0)
                   {
                       newDt3 = rows1.CopyToDataTable();
                       for (int i2 = 0; i2 < newDt3.Rows.Count; i2++)
                       {
                           int colLeg = 0;
                           for (int j2 = 0; j2 < newDt3.Columns.Count; j2++)
                           {
                               if (j2 == 0 || j2 == 1 || j2 == 2)
                                   continue;
                               PdfPCell cell1 = new PdfPCell(new Phrase(newDt3.Rows[i2][j2].ToString(), normalFont));
                               table.AddCell(cell1);
                               colLeg = colLeg + 1;
                           }
                       }
                   }
                   else
                   {
                       string rowData = "No child records to display.";
                       PdfPCell NoRowcell = new PdfPCell(new Phrase(rowData, normalFont));
                       NoRowcell.Colspan = dt2.Columns.Count - 1;
                       table.AddCell(NoRowcell);
                   }
                   doc.Add(table);
                   table.DeleteBodyRows();
               }
           }
           else
           {
               string rowData = "No child records to display.";
               PdfPCell Rowcell1 = new PdfPCell(new Phrase(rowData, normalFont));
               Rowcell1.Colspan = dt1.Columns.Count - 1;
               table.AddCell(Rowcell1);
           }

           doc.Add(table);
           table.DeleteBodyRows();
       }
   }
   iTextSharp.text.FontFactory.FontImp = new iTextSharp.text.FontFactoryImp();
   GC.Collect(GC.MaxGeneration);
   table.Complete = true;
   doc.Add(table);
   doc.Close();

   var file = new System.IO.FileInfo(FilePath);
   Response.Clear();
   Response.Buffer = true;
   Response.AddHeader("Content-Disposition", "attachment; filename=" + _exportCSV + ".pdf");
   Response.AddHeader("Content-Length", file.Length.ToString(CultureInfo.InvariantCulture));
   Response.ContentType = "application/pdf";
   Response.WriteFile(file.FullName);
   HttpContext.Current.Response.Flush(); // Sends all currently buffered output to the client.
   HttpContext.Current.Response.SuppressContent = true;  // Gets or sets a value indicating whether to send HTTP content to the client.
   HttpContext.Current.ApplicationInstance.CompleteRequest(); // Causes ASP.NET to bypass all events and filtering in the HTTP pipeline chain of ex
   Response.Close();
   if (File.Exists(FilePath))
   {
       File.Delete(FilePath);
   }


What I have tried:

I have search for c# optimization texhniques,But can not able to identify how to optimize
Posted
Updated 15-Aug-16 21:32pm
v2
Comments
InbarBarkai 16-Aug-16 5:20am
   
Which part takes most of the time?
Member 8358871 16-Aug-16 7:41am
   
Multiple for loop because code is running for near abou 85000 records

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900