简体   繁体   中英

exporting to PDF in a loop in crystal reports

I am facing difficulties in generating more than 1 PDF's of Crystal report ie in a loop. Basically I am creating PDF's then zipping those files and generating HTTP Response.For the sake of demonstration of code i am running loop twice here is my code.

Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/zip";
Response.AppendHeader("content-disposition", "attachment; filename=Report.zip");
using (ZipFile zip = new ZipFile())
{                    
        for (int i = 0; i < 2; i++)
        {
            var re = rpt.ExportToStream(ExportFormatType.PortableDocFormat);
            string Name="PDF"+i+".pdf";
            zip.AddEntry(Name, re);
        }
        zip.Save(Response.OutputStream);                          
}

Response.Clear();

It successfully generates the zip file but when i try to extract it i gives an error No archive found (The archive is either in unknown formate or damaged). Any help would be appreciated. Btw I followed this link Export Crystal Report to PDF in a Loop only works with first

Using of memorystreem if zip file not attached to local path.

Response.ClearContent(); Response.ClearHeaders();
Response.ContentType = "application/zip";
Response.AppendHeader("content-disposition", "attachment;
filename=Report.zip");

using (var memoryStream = new MemoryStream()) { using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
{
for (int i = 0; i < 2; i++)
{
var re = rpt.ExportToStream(ExportFormatType.PortableDocFormat);
string Name="PDF"+i+".pdf";
zip.AddEntry(Name, re);
zip.Save(memoryStream);
} } }

memoryStream.Seek(0, SeekOrigin.Begin); memoryStream.WriteTo(Response.OutputStream);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM