admin管理员组文章数量:1356456
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);
}
}
本文标签: Downloading multiple PDFs and combining into one without saving the files (CNET Core)Stack Overflow
版权声明:本文标题:Downloading multiple PDFs and combining into one without saving the files (C#.NET Core) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744017260a2576558.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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