0

我正在基于模板生成 PDF 文档。该文档有多个页面。该文档可以有大约 5000 页。创建第 500 页时,我得到一个溢出 RAM(内存)。任何的想法?

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

ps 我正在尝试解决我的问题如下:根据模板生成空页面

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

但是在生成之后,我无法为字段设置值。

4

1 回答 1

1

如果其他人有兴趣,我找到了我的问题的解决方案:

private static void SettingFieltValue(Font font, IEnumerable<SalayDetailsForPdf> dataSalaryDetails, int selectedYear, string directoryOutPdf, string nameOutPdf, string pdfTemplatePath)
    {
    string pdfTemplate = pdfTemplatePath + @"\EmptyTemplateBankBlank_2012.pdf";
    string newFile = directoryOutPdf + nameOutPdf;

    var fs = new FileStream(newFile, FileMode.Create);
    var conc = new PdfConcatenate(fs, true);
    foreach (var data in dataSalaryDetails)
    {
        var reader = new PdfReader(pdfTemplate);
        using (var ms = new MemoryStream())
        {
            using (PdfStamper stamper = new PdfStamper(reader, ms))
            {
                stamper.AcroFields.AddSubstitutionFont(font.BaseFont);
                AcroFields form = stamper.AcroFields;

                form.SetField("t1_name", data.NameHieroglyphic);
                //Other field

                stamper.FormFlattening = true;
                stamper.Close();
            }
            reader = new PdfReader(ms.ToArray());
            ms.Close();                    
        }
        conc.AddPages(reader);
        reader.Close();
    }
    conc.Close();
}
于 2012-07-07T09:26:25.663 回答