我终于成功地弄清楚了如何使用 iTextSharp 使用我的自定义数据用 XFA 表单填充 PDF。
问题是我丢失了让我将 XFA 设为只读的代码。在将工作版本提交到我的源代码管理之前,我犯了一个可怕的错误,即更改我的代码。现在,在谷歌搜索了一个小时后,我仍然找不到它:(如果有人能提醒我代码,那将不胜感激。
PdfReader.unethicalreading = true;
PdfReader reader = new PdfReader(pdfFileName);
PdfStamper stamper = new PdfStamper(reader, ms);
XfaForm xfa = new XfaForm(reader);
XmlDocument doc = new XmlDocument();
doc.LoadXml(CreateXmaData(XDocument.Parse(xfa.DomDocument.InnerXml)));
xfa.DomDocument = doc;
xfa.Changed = true;
XfaForm.SetXfa(xfa, stamper.Reader, stamper.Writer);
PdfAction action = new PdfAction(PdfAction.PRINTDIALOG);
stamper.Writer.SetOpenAction(action);
// Somewhere here I had the code that made my XFA form read only...
stamper.Writer.CloseStream = false;
stamper.Close();
reader.Close();
byte[] buffer = new byte[ms.Position];
ms.Position = 0;
ms.Read(buffer, 0, buffer.Length);
return buffer;