How to generate a large PDF report using itextsharp C#? Say for e.g. i wanted to create a PDF file which contains around 5000+ pages? Without Memory exception?
Document pdfDoc = new Document(PageSize.A3.Rotate(), 0, 0, 10, 10);
FileStream stream = new FileStream(fileName, FileMode.Create);
var writer = PdfWriter.GetInstance(pdfDoc, stream);
pdfDoc.Open();
iTextSharp.text.pdf.ByteBuffer.HIGH_PRECISION = true;
PdfPTable PdfTable = new PdfPTable(1);
PdfTable.SpacingBefore = 30f;
if (headerContent != null)
{
foreach (PDFHeaderItem item in headerContent.items)
{
PdfTable.AddCell(CreateTitlePDFCell(Lang, item.ItemHeader, fontHeaderFontAR, headerContent));
PdfTable.SpacingBefore = 10f;
}
}
//Columns headerCellFormats
for (int i = 0; i < 500000000 ; i++)
{
PdfTable.AddCell(CreatePDFCell("TEXT", cell.KeyName, yourFont, cellFormats));
}
PdfTable.Complete = true;
pdfDoc.Add(PdfTable);
pdfDoc.Close();
We can have the pdf table added to pdf document for every x no of records so that it free up the system memory, please refer the below code
for (int i = 0; i < 500000; i++)
{
PdfTable.AddCell(CreatePDFCell("TEXT", cell.KeyName, yourFont, cellFormats));
if (i > 0 && i % 5000 == 0)
{
pdfDoc.Add(PdfTable);
}
}