ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

CodeGo.net> iTextSharp的System.OutOfMemoryException

2019-11-18 18:17:10  阅读:227  来源: 互联网

标签:itextsharp c


我在尝试创建大型PDF文件时遇到问题.基本上,我有一个字节数组列表,每个字节数组都包含一个字节数组形式的PDF.我想将字节数组合并为一个PDF.这对于较小的文件(2000页以下)非常有用,但是当我尝试创建一个12,00页的文件时遭到轰炸).最初,我使用MemoryStream,但经过一些研究,一个常见的解决方案是使用FileStream.所以我尝试了一种文件流方法,但是得到了类似的结果.该列表包含3800条记录,每条记录包含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);
    }

解决方法:

一些注意事项:

> PdfCopy实现了iDisposable,因此您应该尝试看看使用是否有帮助.
> PdfCopy.FreeReader()将有所帮助.

无论如何,不​​确定是否使用MVC或WebForms,但这是一个简单的工作HTTP handler,它在我的工作站上运行了一个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的大小.这是我的跑步测试输出:

enter image description here

标签:itextsharp,c
来源: https://codeday.me/bug/20191118/2030169.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有