Have a PDF on disk that is 498kb. I'm reading this into a MemoryStream which reports the size as 508978 when viewed in the debugger. Then using a reader and stamper, the output MemoryStream has a size of 15. When the stream is output to disk the PDF is unreadable.
MemoryStream inputStream = new MemoryStream();
MemoryStream outputStream = new MemoryStream();
FileStream fs = File.OpenRead(@"e:\O1.pdf");
inputStream.SetLength(fs.Length);
fs.Read(inputStream.GetBuffer(), 0, (int)fs.Length);
inputStream.Seek(0, SeekOrigin.Begin);
PdfReader reader = new PdfReader(inputStream);
PdfStamper stamper = new PdfStamper(reader, outputStream);
stamper.Writer.CloseStream = false;
AcroFields fields = reader.AcroFields;
foreach (String key in fields.Fields.Keys)
{
if (String.Equals(key, @"ReportID")) { stamper.AcroFields.SetField(@"ReportID", "Fred Bloggs"); }
}
stamper.Close();
Byte[] bytes = (Byte[])outputStream.ToArray();
File.WriteAllBytes(@"e:\O2.pdf", bytes);
ConvertFileToStream()is missing. 2) You compute the length of the output stream before doing anything with the stamper; look at the length after stamping and closing the stamper; you may needstamper.CloseStream = false. - mklstamper.Close()call or ausingconstruct implicitly causing Close to be called. As you lateron try to useoutputStreamas a stream, you need to setstamper.CloseStream = falsebefore that. - mklstamper.Writer.CloseStream = false... - mkl