Working with iTextSharp Files and Displaying in FileStreamResults

I am trying to open a PDF file using iTextSharp, add data to the file to pre-populate the data, and then save it to the stream so that I can display it to users. I do not want to save it locally to a file. I keep getting the error "Could not access private stream." I can’t understand which thread 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(xml); using (var ms = new MemoryStream(bytes)) { stamper.AcroFields.Xfa.FillXfaForm(ms); } } return new FileStreamResult(outstream, "application/pdf") { FileDownloadName = "file.pdf"; }; } } 
+4
source share
1 answer

PdfStamper has the PdfStamper property in its internal output stream; try setting it to false:

 using (var stamper = new PdfStamper(reader, outstream)) { stamper.Writer.CloseStream = false; 
+5
source

Source: https://habr.com/ru/post/1369022/


All Articles