最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Downloading multiple PDFs and combining into one without saving the files (C#.NET Core) - Stack Overflow

programmeradmin2浏览0评论

I'm trying to download 2-10 PDFs from URLs, combine them into one PDF and serve that PDF to the user. I'm getting the following error:

ObjectDisposedException: Cannot access a closed Stream. > System.IO.MemoryStream.Read(byte[] buffer, int offset, int count)

Does anyone see where I'm going wrong here?

Here's the code snippet that's trying to combine the files:

MemoryStream finalStream = new MemoryStream();
PdfCopyFields copy = new PdfCopyFields(finalStream);

var ms1 = new MemoryStream();
ConvertToStream(url1, ms1);
ms1.Position = 0;
copy.AddDocument(new PdfReader(ms1));
ms1.Dispose();

var ms2 = new MemoryStream();
ConvertToStream(url2, ms2);
ms2.Position = 0;
copy.AddDocument(new PdfReader(ms2));
ms2.Dispose();

foreach(string s in psURLs)
{
    var ms3 = new MemoryStream();
    ConvertToStream(s, ms3);
    ms3.Position = 0;
    copy.AddDocument(new PdfReader(ms3));
    ms3.Dispose();
}

copy.Close();

return finalStream;

And here's the function that downloads a PDF and copies it to a stream:

private void ConvertToStream(string fileUrl, Stream stream)
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(fileUrl);
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();

    try
    {
        Stream response_stream = response.GetResponseStream();

        response_stream.CopyTo(stream, 4096);
    }
    finally
    {
        response.Close();
    }
}

I'm trying to download 2-10 PDFs from URLs, combine them into one PDF and serve that PDF to the user. I'm getting the following error:

ObjectDisposedException: Cannot access a closed Stream. > System.IO.MemoryStream.Read(byte[] buffer, int offset, int count)

Does anyone see where I'm going wrong here?

Here's the code snippet that's trying to combine the files:

MemoryStream finalStream = new MemoryStream();
PdfCopyFields copy = new PdfCopyFields(finalStream);

var ms1 = new MemoryStream();
ConvertToStream(url1, ms1);
ms1.Position = 0;
copy.AddDocument(new PdfReader(ms1));
ms1.Dispose();

var ms2 = new MemoryStream();
ConvertToStream(url2, ms2);
ms2.Position = 0;
copy.AddDocument(new PdfReader(ms2));
ms2.Dispose();

foreach(string s in psURLs)
{
    var ms3 = new MemoryStream();
    ConvertToStream(s, ms3);
    ms3.Position = 0;
    copy.AddDocument(new PdfReader(ms3));
    ms3.Dispose();
}

copy.Close();

return finalStream;

And here's the function that downloads a PDF and copies it to a stream:

private void ConvertToStream(string fileUrl, Stream stream)
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(fileUrl);
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();

    try
    {
        Stream response_stream = response.GetResponseStream();

        response_stream.CopyTo(stream, 4096);
    }
    finally
    {
        response.Close();
    }
}
Share Improve this question edited Mar 29 at 6:44 marc_s 756k184 gold badges1.4k silver badges1.5k bronze badges asked Mar 28 at 19:24 ChronosChronos 1191 gold badge2 silver badges10 bronze badges 5
  • Thanks for the rec. I'd thought about that but I'm running the app in a containerized service on Azure and don't think I can save to the file system. – Chronos Commented Mar 28 at 22:11
  • Thanks for the added info. If I comment out all of the disposing and PdfCopyFields.Close() it gives me the same error :/ – Chronos Commented Mar 28 at 22:57
  • Which line of code throws the exception? – mjwills Commented Mar 28 at 23:50
  • Remove the MemoryStream.Dispose lines of code - they don't do anything useful anyway. If you remove them, does the problem go away? – mjwills Commented Mar 28 at 23:51
  • What Nuget package are you using for "PdfReader/PdfCopyFields"? Also, what is happening in "PdfCopyFields"? – Morten Bork Commented Mar 29 at 9:04
Add a comment  | 

1 Answer 1

Reset to default 0

I tried this:

And it compiled, and didn't throw an exception:
Could you please replicate your issue, as a unit test case?

using iTextSharp.text.pdf;
using System.Net;

public class PdfStreamsHandler
{
    private string url1 = "https://www.w3./WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf";
    private string url2 = "https://www.w3./WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf";

    List<string> psURLs = new List<string>() {
    "https://www.w3./WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf",
    "https://www.w3./WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf",
    "https://www.w3./WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf"
    };

    public MemoryStream MergePdfs()
    {
        MemoryStream finalStream = new MemoryStream();
        PdfCopyFields copy = new PdfCopyFields(finalStream);

        var ms1 = new MemoryStream();
        ConvertToStream(url1, ms1);
        ms1.Position = 0;
        copy.AddDocument(new PdfReader(ms1));
        ms1.Dispose();

        var ms2 = new MemoryStream();
        ConvertToStream(url2, ms2);
        ms2.Position = 0;
        copy.AddDocument(new PdfReader(ms2));
        ms2.Dispose();

        foreach (string s in psURLs)
        {
            var ms3 = new MemoryStream();
            ConvertToStream(s, ms3);
            ms3.Position = 0;
            copy.AddDocument(new PdfReader(ms3));
            ms3.Dispose();
        }

        copy.Close();

        return finalStream;
    }

    private void ConvertToStream(string fileUrl, Stream stream)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(fileUrl);
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();

        try
        {
            Stream response_stream = response.GetResponseStream();

            response_stream.CopyTo(stream, 4096);
        }
        finally
        {
            response.Close();
        }
    }
}
public class UnitTest
{
    [Fact]
    public void TestAnswer2()
    {
        PdfStreamsHandler pdfStreamHandler = new PdfStreamsHandler();
        MemoryStream actualResult = pdfStreamHandler.MergePdfs();
        Assert.NotNull(actualResult);
    }
}
发布评论

评论列表(0)

  1. 暂无评论