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();