簡體   English   中英

iTextSharp System.OutOfMemoryException

[英]iTextSharp System.OutOfMemoryException

我在嘗試創建大型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);
    }

幾個筆記:

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

嘗試使輸出文件類似於您在問題中描述的內容,但YMMV,取決於您正在處理的單個PDF的大小。 這是我運行的測試輸出:

在此輸入圖像描述

因此,在經歷了很多混亂之后,我意識到它無處可去。 但是,我確實設法找到了解決辦法。 我沒有返回字節數組,而是返回一個臨時文件路徑,然后我將其傳輸並刪除。

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

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

            int i = 0;
            foreach (byte[] PDF in PDFs)
            {
                i++;
                // Create a reader
                PdfReader reader = new PdfReader(new RandomAccessFileOrArray(PDF), null);

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

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

                    // This is a lie, it still costs money, hue hue hue :)~
                    copy.FreeReader(reader);
                }
                reader.Close();
            }

            // Close the document
            doc.Close();

            // Close the document
            copy.Close();
        }

        // Return temp file path
        return tempFileName;
    }

以下是我將數據發送給客戶端的方法。

        // Send the merged PDF file to the user.
        System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
        response.ClearContent();
        Response.ClearHeaders();
        response.ContentType = "application/pdf";
        response.AddHeader("Content-Disposition", "attachment; filename=1094C.pdf;");
        response.WriteFile(tempFileName);
        HttpContext.Current.Response.Flush(); // Sends all currently buffered output to the client.
        DeleteFile(tempFileName); // Call right after flush but before close
        HttpContext.Current.Response.SuppressContent = true;  // Gets or sets a value indicating whether to send HTTP content to the client.
        HttpContext.Current.ApplicationInstance.CompleteRequest(); // Causes ASP.NET to bypass all events and filtering in the HTTP pipeline chain of execution and directly execute the EndRequest event.

最后,這是一個奇特的DeleteFile方法

    private void DeleteFile(string fileName)
    {
        if (File.Exists(fileName))
        {
            try
            {
                File.Delete(fileName);
            }
            catch (Exception ex)
            {
                //Could not delete the file, wait and try again
                try
                {
                    System.GC.Collect();
                    System.GC.WaitForPendingFinalizers();
                    File.Delete(fileName);
                }
                catch
                {
                    //Could not delete the file still
                }
            }
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM