iTextSharp System.OutOfMemoryException

Luk*_*kas 4 c# itext

我在尝试创建大型 PDF 文件时遇到问题。基本上我有一个字节数组列表,每个数组都包含一个字节数组形式的 PDF。我想将字节数组合并到一个 PDF 中。这对于较小的文件(2000 页以下)非常有效,但是当我尝试创建 12,00 页的文件时,它崩溃了)。最初我使用的是 MemoryStream,但经过一番研究后,一个常见的解决方案是使用 FileStream。所以我尝试了文件流方法,但得到了类似的结果。该列表包含 3,800 条记录,每条包含 4 页。MemoryStream 在大约 570 条记录后爆炸。FileStream 在大约 680 条记录后爆炸。代码崩溃后当前文件大小为 60MB。我究竟做错了什么?这是我的代码,代码在“copy.AddPage(curPg);”上崩溃 指令,在“for(”循环内。

    private byte[] MergePDFs(List<byte[]> PDFs)
    {
        iTextSharp.text.Document doc = new iTextSharp.text.Document();
        byte[] completePDF;
        Guid uniqueId = Guid.NewGuid();
        string tempFileName = Server.MapPath("~/" + uniqueId.ToString() + ".pdf");

        //using (MemoryStream ms = new MemoryStream())
        using(FileStream ms = new FileStream(tempFileName, FileMode.Create, FileAccess.Write, FileShare.Read))
        {
            iTextSharp.text.pdf.PdfCopy copy = new iTextSharp.text.pdf.PdfCopy(doc, ms);
            doc.Open();

            int i = 0;
            foreach (byte[] PDF in PDFs)
            {
                i++;
                // Create a reader
                iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(PDF);

                // Cycle through all the pages
                for (int currentPageNumber = 1; currentPageNumber <= reader.NumberOfPages; ++currentPageNumber)
                {
                    // Read a page
                    iTextSharp.text.pdf.PdfImportedPage curPg = copy.GetImportedPage(reader, currentPageNumber);

                    // Add the page over to the rest of them
                    copy.AddPage(curPg);
                }

                // Close the reader
                reader.Close();
            }

            // Close the document
            doc.Close();

            // Close the copier
            copy.Close();

            // Convert the memorystream to a byte array
            //completePDF = ms.ToArray();
        }

        //return completePDF;
        return GetPDFsByteArray(tempFileName);
    }
Run Code Online (Sandbox Code Playgroud)

kuu*_*nbo 5

一些注意事项:

  1. PdfCopy实现iDisposable,所以你应该尝试看看是否有using帮助。
  2. PdfCopy.FreeReader()会有帮助的。

不管怎样,不确定您使用的是 MVC 还是 WebForms,但这里有一个简单的工作HTTP 处理程序,使用在我的工作站上运行的15 页 125KB测试文件进行了测试:

<%@ WebHandler Language="C#" Class="MergeFiles" %>
using System;
using System.Collections.Generic;
using System.Web;
using System.IO; 
using iTextSharp.text; 
using iTextSharp.text.pdf; 

public class MergeFiles : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        List<byte[]> pdfs = new List<byte[]>();
        var pdf = File.ReadAllBytes(context.Server.MapPath("~/app_data/test.pdf"));
        for (int i = 0; i < 4000; ++i) pdfs.Add(pdf);

        var Response = context.Response;
        Response.ContentType = "application/pdf";
        Response.AddHeader(
            "content-disposition",
            "attachment; filename=MergeLotsOfPdfs.pdf"
        );
        Response.BinaryWrite(MergeLotsOfPdfs(pdfs));
    }

    byte[] MergeLotsOfPdfs(List<byte[]> pdfs)
    {
        using (var ms = new MemoryStream())
        {
            using (Document document = new Document())
            {
                using (PdfCopy copy = new PdfCopy(document, ms))
                {
                    document.Open();
                    for (int i = 0; i < pdfs.Count; ++i)
                    {
                        using (PdfReader reader = new PdfReader(
                            new RandomAccessFileOrArray(pdfs[i]), null))
                        {
                            copy.AddDocument(reader);
                            copy.FreeReader(reader);
                        }
                    }
                }
            }
            return ms.ToArray();
        }
    }

    public bool IsReusable { get { return false; } }
}
Run Code Online (Sandbox Code Playgroud)

尝试使输出文件类似于您在问题中描述的内容,但是 YMMV,具体取决于您正在处理的单个 PDF 的大小。这是我运行的测试输出:

在此输入图像描述