我正在使用 itextSharp 4.1.6 将 AcroFields 添加到现有 PDF,然后在第二步中用内容填充这些字段。当我在 Adobe Reader 中打开这些文档时,我可以看到内容。当我尝试打印文档时,所有填写的字段都是空的。
这发生在可编辑和拼合的 PDF 中。'GenerateAppearances' 设置为 True。由于当前的许可证概念,我无法升级到另一个版本。我的代码:
var dlg = new OpenFileDialog
{
FileName = "",
Filter = @"pdf|*.pdf"
};
if (dlg.ShowDialog() != DialogResult.OK) return;
// first step: create field
var outputpath1 = Path.Combine(Path.GetTempPath(), string.Format("{0}.pdf", Guid.NewGuid().ToString("N")));
var pdfReader1 = new PdfReader(dlg.FileName);
var output1 = new FileStream(outputpath1, FileMode.Create, FileAccess.Write);
var stamper1 = new PdfStamper(pdfReader1, output1);
InsertTextfield(stamper1, 1, 20, 500, 150, 100, "test");
stamper1.Close();
pdfReader1.Close();
// second step: insert some text
var outputpath2 = Path.Combine(Path.GetTempPath(), string.Format("{0}.pdf", Guid.NewGuid().ToString("N")));
var pdfReader2 = new PdfReader(outputpath1);
var output2 = new FileStream(outputpath2, FileMode.Create, FileAccess.Write);
var stamper2 = new PdfStamper(pdfReader2, output2);
stamper2.AcroFields.GenerateAppearances = true;
var formData = stamper2.AcroFields;
formData.SetField("test", "Lorem ipsum dolor sit amet, consectetur adipiscing elit. ");
stamper2.Close();
pdfReader2.Close();
Process.Start(outputpath2);
和InsertTextField
:
private static void InsertTextfield(PdfStamper stamper, int page, float x, float y, float width, float height, string fieldname)
{
var field = PdfFormField.CreateTextField(stamper.Writer, true, false, 500);
field.SetWidget(new Rectangle(x, y, x + width, y + height), PdfAnnotation.HIGHLIGHT_INVERT);
field.SetFieldFlags(PdfAnnotation.FLAGS_PRINT);
field.FieldName = fieldname;
stamper.AddAnnotation(field, page);
}
对于文档,只需创建一个新的 word 文档,添加一些文本并保存为 PDF。选择此文档开始。
那么,我怎样才能让我的归档内容出现在打印件中?
编辑:添加代码