I am trying top open a PDF file using iTextSharp, add a dataset to the file to prepopulate data, then save it to a stream so that I can display it to the users. I don't want to save it locally to a file. I keep getting the error "Cannot access a closed Stream." I can't figure out which stream is wrong.
Here is my code:
public FileStreamResult PushDataIntoPDFStream(string filename)
{
var reader = new PdfReader(Path.Combine(Server.MapPath(path), filename));
var xml = @"<?xml version=""1.0"" encoding=""UTF-8""?>
<form1>
<firstName>test</firstName>
<lastName>user</lastName>
<driveCar>0</driveCar>
<gender>1</gender>
<birthdate>2011-08-12</birthdate>
<numPets>4</numPets>
</form1>";
using (var outstream = new MemoryStream())
{
using (var stamper = new PdfStamper(reader, outstream))
{
var bytes = System.Text.Encoding.UTF8.GetBytes开发者_JAVA技巧(xml);
using (var ms = new MemoryStream(bytes))
{
stamper.AcroFields.Xfa.FillXfaForm(ms);
}
}
return new FileStreamResult(outstream, "application/pdf")
{
FileDownloadName = "file.pdf";
};
}
}
The PdfStamper
has a .CloseStream
property on its internal output stream; try setting it to false:
using (var stamper = new PdfStamper(reader, outstream))
{
stamper.Writer.CloseStream = false;
精彩评论