Click here to Skip to main content
15,920,503 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi. I'm generating a PDF document based on template. The document has multiple pages. The document can have about 5000 pages. When creating the 500-th page I get an overflow RAM (memory). Any idea? Thanks in advance.

C#
public static void CreateBankBlank2012Year(string pdfTemplatePath, string directoryOutPdf, string nameOutPdf, AnnualReportsFilterParameters filterParametrs, string serverPath)
{
    // Get details salary
    IEnumerable<SalayDetailsForPdf> dataSalaryDetails = (IEnumerable<SalayDetailsForPdf>) GetSalaryData(filterParametrs);

    String fontPath = Path.Combine(serverPath + "\\Fonts", "STSONG.ttf");
    Font font = FontFactory.GetFont(fontPath, BaseFont.IDENTITY_H, 8);

    using (Document document = new Document())
    {
        using (PdfSmartCopy copy = new PdfSmartCopy(
            document, new FileStream(directoryOutPdf + nameOutPdf, FileMode.Create))
        )
        {
            document.Open();

            foreach (var data in dataSalaryDetails)                    
            {
                PdfReader reader = new PdfReader(pdfTemplatePath + @"\EmptyTemplateBankBlank_2012.pdf");
                using (var ms = new MemoryStream())
                {
                    using (PdfStamper stamper = new PdfStamper(reader, ms))
                    {
                        stamper.AcroFields.AddSubstitutionFont(font.BaseFont);
                        AcroFields form = stamper.AcroFields;
                                                                                                                            form.SetField("t1_address1", data.Address1);

                        form.SetField("t1_name", data.NameHieroglyphic);

                        // Other field ...

                        stamper.FormFlattening = true;
                    }
                    reader = new PdfReader(ms.ToArray());

                    copy.AddPage(copy.GetImportedPage(reader, 1));
                }
            }
        }                
    }
}
Posted
Updated 28-Mar-23 11:26am

I'm trying resolve my problem as follow:
generating empty pages based on template

C#
private static void GeneratePdfFromTemplate(string directoryOutPdf, string nameOutPdf, string pdfTemplatePath, int countPages)
        {                                   
            using (Document document = new Document())
            {
                using (PdfSmartCopy copy = new PdfSmartCopy(
                    document, new FileStream(directoryOutPdf + nameOutPdf, FileMode.Create))
                    )
                {
                    document.Open();                                        
                    PdfReader reader = new PdfReader(pdfTemplatePath + @"\EmptyTemplateBankBlank_2012.pdf");
                    for (int i = 0; i < countPages; i++)                    
                    {                                                    
                        copy.AddPage(copy.GetImportedPage(reader, 1));                                                 
                    }
                    reader.Close();
                    copy.Close();
                }
                document.Close();
            }            
            GC.Collect();            
        }

But after a generating I can't set values to the fields.
 
Share this answer
 
Hi! find a solution? I Have tje same problem and need support this.
 
Share this answer
 

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