【问题标题】:Error code 0x800704CD when generating PDF to zip生成 PDF 到 zip 时出现错误代码 0x800704CD
【发布时间】:2021-12-17 14:48:27
【问题描述】:

我知道之前有人问过并回答过这个问题,但这些解决方案对我不起作用。我必须动态创建一个 PDF 列表,并且每一行都有一个复选框。您检查要下载的 PDF,其 ID 被传递给创建 PDF 的函数。我将这些 PDF 存储在一个列表中,该列表被传递给 Zip 函数 (DotNetZip)。问题是,当第一次生成 PDF 时,在 PDF 中添加新页面时,我在创建它的中间某个地方出现了这个错误,大约一半。通常它是同一个地方,但偶尔它会改变它崩溃的地方。谁能看看我的代码并指出我在哪里搞砸了?

    protected void Download_PDF_Click(object sender, EventArgs e)
    {
        Response.Clear();
        Response.BufferOutput = false;
        HttpContext c = HttpContext.Current;

        String archiveName = "Arhiva Inspectii.zip";
        Response.ContentType = "application/zip";
        Response.AddHeader("content-disposition", "filename=\"" + archiveName + "\"");

        int nr_rows = tbl_inspectii.Rows.Count - 1;

        foreach (String id_chbx in ID_Checkbox)
        {
            CheckBox chbx = tbl_inspectii.FindControl(id_chbx) as CheckBox;
            String PDF_id = "";
            if(chbx != null)
            {
                if(chbx.Checked)
                {
                    PDF_id = chbx.ID.Replace("ChBx", string.Empty);
                    Create_PDF(PDF_id);
                }
            }  
        }

        string destdir = Server.MapPath("~/Zip/") + archiveName;


        using (ZipFile zip = new ZipFile())
        {
            zip.AddFiles(PDF_list, destdir);

            zip.Save(destdir);
        }

    }


    protected byte[] Create_PDF(String id_insp_max)
    {
        using (MemoryStream ms = new MemoryStream())
        {
                Document brosura = new Document(PageSize.A4);
                brosura.SetMargins(40, 40, 40, 40);
                PdfWriter wri = PdfWriter.GetInstance(brosura, ms);//new FileStream(Server.MapPath("Pdf/") + titlu_pdf + ".pdf", FileMode.Create)
                HttpContext.Current.Response.AppendHeader("Content-Disposition", "inline; filename=\"" + titlu_pdf + ".pdf\"");
                PdfWriter.GetInstance(brosura, HttpContext.Current.Response.OutputStream);
                brosura.Open();

                //lots of SQL, and brosura.Add();
                //at some point, a brosura.Add() has the error halfway through the pdf

                brosura.Close();
                PDF_list.Add(titlu_pdf);
                wri.Close();

                return ms.ToArray();
        }
    }

【问题讨论】:

  • 该错误代码可能会提示位大小。我会强制项目使用 x64 位 - 看看是否有帮助。

标签: c# asp.net pdf itext zip


【解决方案1】:

关闭文档后尝试移动响应块。 并尝试添加 Response.End();

为什么你在 Create_PDF 中返回 Array 但不使用它?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    相关资源
    最近更新 更多