我正在使用itext7进行PDF操作。我正在尝试通过field.setValue方法填充表单字段。我的_Reader对象在构造函数级别填充,并用于添加和获取表单字段。但是在保存表单字段值时,在关闭PdfDocument对象时会捕获以下异常:
at System.IO.__Error.StreamIsClosed()
at System.IO.MemoryStream.Write(Byte[] buffer, Int32 offset, Int32 count)
at iText.IO.Source.OutputStream`1.Write(Byte[] b, Int32 off, Int32 len)
at iText.IO.Source.OutputStream`1.WriteInteger(Int32 value)
at iText.Kernel.Pdf.PdfWriter.WriteToBody(PdfObject pdfObj)
at iText.Kernel.Pdf.PdfWriter.FlushObject(PdfObject pdfObject, Boolean
canBeInObjStm)
at iText.Kernel.Pdf.PdfDocument.FlushObject(PdfObject pdfObject, Boolean
canBeInObjStm)
at iText.Kernel.Pdf.PdfObject.Flush(Boolean canBeInObjStm)
at iText.Kernel.Pdf.PdfPage.Flush(Boolean flushResourcesContentStreams)
at iText.Kernel.Pdf.PdfPage.Flush()
at iText.Kernel.Pdf.PdfDocument.Close()
代码段如下:
using (var memoryStream = new MemoryStream())
{
PdfDocument document = new PdfDocument(_Reader, new PdfWriter(memoryStream));
PdfAcroForm Form = PdfAcroForm.GetAcroForm(document, true);
foreach (PDFField Field in PDFFields)
{
PdfFormField formField = Form.GetField(Field.Name);
switch (Field.Type)
{
case PDF_FIELD_TYPE.TEXTBOX:
if (!string.IsNullOrEmpty(Field.Value))
formField.SetValue(Field.Value);
else
formField.SetValue(string.Empty);
break;
}
}
document.Close();
byte[] PDFBytes = ((MemoryStream)memoryStream).ToArray();
预先感谢您的帮助。