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
|
1 Answer
Reset to default 0I 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);
}
}
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