我使用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。选择此文档开始。
那么,我怎样才能将我提交的内容显示在印刷品中?
编辑:添加了代码
答案 0 :(得分:0)
您应该通过TextField构造函数创建TextField 适用于iTextSharp 4.1.6
private static void InsertTextfield(PdfStamper stamper, int page, float x, float y, float width, float height, string fieldname)
{
var field = new TextField(stamper.Writer, new Rectangle(x, y, x + width, y + height), fieldname);
field.Options = TextField.MULTILINE;
stamper.AddAnnotation(field.GetTextField(), page);
}