c # – Saving a slow PDF file after 10,000 pages in iTextSharp

I'm trying to build an application that creates a PDF and saves it to a local file. I'm using this via iTextSharp in C #. I need to register at least 1 PDF file (100,000) lake.

After 10,000 files, the recording becomes slow. The first 10 kilometers are recorded in 2 minutes, the rest of the files last almost 5 hours.

I believe the problem is the use of memory. But I could not find a solution to solve this problem. I have posted the source code.

Private Blank CreatePDF11 ()
{
Tail QTempFileNames;
QTempFileNames = new queue();
string fileName = string.Empty;

DateTime fileCreationDatetime = DateTime.Now;

filename = @ "D:  StatementMassPrint  tesystw14j414435.PDF";

string DestinationDirectory = @ "D:  StatementMassPrint";

string StrTempFilePath = String.Empty;

string pdfPath = filename;

int TempPdfFileName = 1;
if (DestinationDirectory! = string.Empty)
{
StrTempFilePath = DestinationDirectory + "temp" + "(" + DateTime.Now.ToFileTime () + ")";
}

if (! (Directory.Exists (StrTempFilePath))))
{
Directory.CreateDirectory (StrTempFilePath);
}
string reportFileName = StrTempFilePath + "\" + TempPdfFileName.ToString () + ".Pdf";

for (int f = 0; f <100000; f ++)
{
reportFileName = StrTempFilePath + "\" + TempPdfFileName.ToString () + ".Pdf";

using (FileStream msReport = new FileStream (reportFileName, FileMode.Create))
{
//Step 1
using (Document pdfDoc = new Document (PageSize.A5.Rotate (), 10f, 10f, 200f, 40f))
{
try
{
// 2nd step
PdfWriter pdfWriter = PdfWriter.GetInstance (pdfDoc, msReport);
pdfWriter.PageEvent = new EpisodePageHeaderAndFooter ();

// open the stream
pdfDoc.Open ();

DataTable dtEpisodeWise = new DataTable ();
dtEpisodeWise.Columns.Add ("INVOICE_NO");
dtEpisodeWise.Columns.Add ("INVOICE_DATE");
dtEpisodeWise.Columns.Add ("CODE");
dtEpisodeWise.Columns.Add ("SERVICE_DESCRIPTION");
dtEpisodeWise.Columns.Add ("QTY", typeof (decimal));
dtEpisodeWise.Columns.Add ("UNIT_PRICE", typeof (decimal));
dtEpisodeWise.Columns.Add ("GROSS", typeof (decimal));
dtEpisodeWise.Columns.Add ("REJECT", typeof (decimal));
dtEpisodeWise.Columns.Add ("NET", typeof (decimal));
dtEpisodeWise.Columns.Add ("DEDUCTION", typeof (decimal));
dtEpisodeWise.Columns.Add ("NET_PAYABLE_WITHOUT_VAT", typeof (decimal));
dtEpisodeWise.Columns.Add ("VAT", typeof (decimal));
dtEpisodeWise.Columns.Add ("NET_PAYABLE_WITH_VAT", typeof (decimal));

PdfPTable table = new PdfPTable (dtEpisodeWise.Columns.Count);
table.WidthPercentage = 100;

Font fontH1 = new font (Font.FontFamily.HELVETICA, 6, Font.BOLDITALIC);
for (int k = 0; k <dtEpisodeWise.Columns.Count; k ++)
{

string str = System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase (dtEpisodeWise.Columns[k].ColumnName.Replace ("_", "") .ToLower ()); ;
PdfPCell cell = new PdfPCell (new sentence (str, fontH1));
cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
cell.VerticalAlignment = PdfPCell.ALIGN_CENTER;
// cell.BackgroundColor = new iTextSharp.text.BaseColor (51, 102, 102);

table.AddCell (cell);
}

// for (int i = 0; i <1000; i ++)
// {
dtEpisodeWise.Rows.Add ("CR100005", "25-05-1989", "CPT004", "SERVICE005", 1, 10, 100, 10, 90, 45, 45, 5, 50);
dtEpisodeWise.Rows.Add ("CR100006", "25-05-1992", "CPT00555", "SERVICE105", 6, 60, 600, 60, 450, 45, 45, 5, 500);

//}

Font fontH2 = new font (Font.FontFamily.HELVETICA, 6, Font.ITALIC);

for (int i = 0; i <dtEpisodeWise.Rows.Count; i ++)
{
for (int j = 0; j <dtEpisodeWise.Columns.Count; j ++)
{

PdfPCell cell = new PdfPCell (new sentence (dtEpisodeWise.Rows[i][j].ToString (), fontH2));

// Align the cell in the center

if (dtEpisodeWise.Columns[j].DataType == typeof (decimal))
{
cell.HorizontalAlignment = PdfPCell.ALIGN_RIGHT;
cell.VerticalAlignment = PdfPCell.ALIGN_CENTER;
}
other
{
cell.HorizontalAlignment = PdfPCell.ALIGN_LEFT;
cell.VerticalAlignment = PdfPCell.ALIGN_CENTER;
}

table.HeaderRows = 1;

table.AddCell (cell);
}
}
pdfDoc.Add (table);

pdfDoc.Close ();
TempPdfFileName ++;



}

catch (Exception ex)
{
// handle an exception
}

finally
{


}

}

}
}


}
`` `