У меня проблема с попыткой создать большой PDF-файл. В основном у меня есть список байтовых массивов, каждый из которых содержит PDF-файл в виде байтового массива. Я хотел объединить байтовые массивы в один PDF-файл. Это отлично работает для файлов меньшего размера (до 2000 страниц), но когда я попытался создать файл размером 12,00 страниц, он провалился). Первоначально я использовал MemoryStream, но после некоторых исследований общим решением было использовать вместо этого FileStream. Поэтому я попробовал использовать файловый поток, но получил аналогичные результаты. Список содержит 3800 записей, каждая по 4 страницы. MemoryStream срывается примерно после 570. FileStream примерно после 680 записей. Текущий размер файла после сбоя кода составлял 60 МБ. Что я делаю неправильно? Вот код, который у меня есть, и он вылетает на "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);
    }
3
Lukas 5 Май 2016 в 20:08

2 ответа

Лучший ответ

Пара замечаний:

  1. PdfCopy реализует iDisposable, поэтому вам следует попробовать и посмотреть, поможет ли using.
  2. PdfCopy.FreeReader() поможет.

В любом случае, не уверен, используете ли вы MVC или WebForms, но вот простой рабочий Обработчик HTTP протестирован с тестовым файлом 15 страниц 125 КБ , который работает на моей рабочей станции:

<%@ 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

3
kuujinbo 6 Май 2016 в 02:25

Итак, после долгой возни я понял, что другого пути нет. Однако мне удалось найти обходной путь. Вместо того, чтобы возвращать массив байтов, я возвращаю путь к временному файлу, который затем передаю и удаляю оттуда.

    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
                }
            }
        }
    }
0
Lukas 5 Май 2016 в 23:27