我正在尝试使用iText7在PDF上选中一个复选框。但是,不仅检查一个字段,还检查所有字段
我需要的:
我得到的是:
编辑时的PDF:
我认为出口价值与之有关。但是我不知道该怎么办。
我的代码:
private static void CreatePdf(string output)
{
using var _pdfDoc = new PdfDocument(new PdfReader("CheckTest.pdf"), new PdfWriter(output));
var form = PdfAcroForm.GetAcroForm(_pdfDoc, true);
var check = form.GetField("Check");
check.SetValue("01");
}
PDF:链接
有人知道如何正确检查吗?
谢谢!
首先,PDF实质上将PDF AcroForm复选框字段误用作单选按钮,而不是使用真正的PDF AcroForm单选按钮字段。
The PDF specification does not clearly specify what a PDF viewer should do in such a case (it's mis-use after all) but the developers of the PDF form generator in question probably have experimented and determined that in the most widely used PDF viewer, Adobe Acrobat Reader, this mis-use works just as they want.
As this use is beyond specification, though, other PDF processors processing such PDFs may produce completely different results without doing anything wrong.
That being said, there is a way to fill the form using iText and achieve results similar to those generated by Adobe Reader.
The problem at hand is that iText by default for all form field types except actual AcroForm radio button fields generates new appearances in a way appropriate for the field type when setting the field value. In your document there are three check box field objects with the same name. Thus, they are considered a single check box with three widgets representing the same value, and so the appearances are generated as you observe.
But you can tell iText to not generate new appearances, using another SetValue
overload accepting an additional boolean value, simply replace
check.SetValue("01");
by
check.SetValue("01", false);
Now iText makes do with the existing appearances, so only the field becomes checked that has an appearance for that "01"
value.
当心,仅在这种情况下才能阻止iText生成外观。例如,在文本字段的情况下,即使内部字段值已更改,不更新外观也会导致具有旧字段内容的旧外观继续显示。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句