在磁盘上有一个 498kb 的 PDF。我正在将其读入 MemoryStream 中,当在调试器中查看时,它报告的大小为 508978。然后使用阅读器和压模,输出 MemoryStream 的大小为 15。当流输出到磁盘时,PDF 是不可读的。
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);