下载多个pdf

本文关键字:pdf 下载 | 更新日期: 2023-09-27 18:22:19

我正试图在我的asp.net应用程序中下载多个pdf作为附件。我已经使用pdfstamper(itextsharp)创建了一些模板和填充值。我可以填写数值,但无法下载。

private void FillForm(string path, DataTable BridgeValues, DataTable Comments, DataTable Maintenance,string Newfilename)
    {
        try
        {
            string pdfTemplate = path;
            string newFile = Newfilename;
            string Pathser = "";
            if (!System.IO.Directory.Exists(Server.MapPath(@"~/PDF/")))
            {
                System.IO.Directory.CreateDirectory(Server.MapPath(@"~/PDF/"));
            }
            if (Directory.Exists(Server.MapPath(@"~/PDF/")))
            {
               Pathser = Server.MapPath(@"~/PDF/" + Newfilename);
            }
            System.IO.MemoryStream mStream = new System.IO.MemoryStream();
            // create a new PDF reader based on the PDF template document
            PdfReader pdfReader = new PdfReader(pdfTemplate);
            PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(Pathser, FileMode.Create));
            AcroFields pdfFormFields = pdfStamper.AcroFields;
            DataColumn dc = null;
            for (int i = 0; i < BridgeValues.Columns.Count - 1; i++)
            {
                dc = BridgeValues.Columns[i];
                pdfFormFields.SetField(dc.ColumnName.ToString(),  BridgeValues.Rows[0][dc].ToString());
            }
            pdfStamper.FormFlattening = true;
            // close the pdf
            pdfStamper.Close();
            ////Response.ContentType = "application/octet-stream";
            Response.ContentType = "application/pdf";
            ////Response.AddHeader("Content-Disposition", "attachment; filename=Report.pdf");
            Response.AddHeader("Content-Disposition", "attachment; filename=" + Newfilename + "");
            ////Response.BinaryWrite(mStream.ToArray());
            Response.TransmitFile(Server.MapPath(("~/PDF/"+ Newfilename)));
            Response.Clear();
            Response.End();
        }
        catch (System.Threading.ThreadAbortException lException)
        {
            // do nothing
        }
    }

我第一次尝试创建一个pdf文件时,它成功了,但后来当我尝试下载多个文件时,却出现了异常。无法计算表达式,因为代码已优化,或者本机帧位于调用堆栈的顶部。

下载多个pdf

我不认为您可以通过发出一个请求并从操作返回一个集合来下载多个文件。我建议您需要允许用户下载多个文件,然后对它们进行压缩,并将存档流式传输到浏览器。

以下是压缩多个文件的示例:http://devpinoy.org/blogs/keithrull/archive/2008/01/25/how-to-create-zip-files-in-c-with-sharpziplib-ziplib.aspx