将可见性设置为隐藏时,PdfFormField 根本不会隐藏
PdfFormField simply does not hide when setting the visibility to HIDDEN
我使用带参数 PdfFormField.HIDDEN
的 setVisibility()
方法将 PdfFormField 的可见性设置为隐藏。尽管如此,在展平它时,该场并没有隐藏。下面是执行此操作的代码。
File file = new File("path to PDF file");
baos = new ByteArrayOutputStream();
pdfDoc = new PdfDocument(new PdfReader(file.getAbsolutePath()), new PdfWriter("path to flattened PDF file"));
//This function removes all the permissions.
removePdfPermissions();
form = PdfAcroForm.getAcroForm(pdfDoc, true);
fields = form.getFormFields();
fields.get("HumanSubjectsText").setVisibility(PdfFormField.HIDDEN);
fields.get("HumanSubjects").setVisibility(PdfFormField.HIDDEN);
form.flattenFields();
pdfDoc.close();
将字段的值设置为空字符串 ""
是一种解决方法,但不是正确的方法。
字段 "HumanSubjects"
和 "HumanSubjectsText"
对应于第 2 页上的复选框及其对应文本。(人类受试者)
无法使用 iText 库隐藏字段。与 Bruno 的评论相反,iText 库中的一种方法效果很好,setValue()
。我不太清楚为什么 setVisibility()
会失败。
但是,我可以使用 Master PDF editor 工具隐藏该字段。我附上了此工具的屏幕截图,您可以在其中从 General
选项卡更改表单字段的可见性。
一个观察:在com.itextpdf.forms.fields.PdfFormField
中定义的一些常量如下:
public static final int HIDDEN = 1;
public static final int VISIBLE_BUT_DOES_NOT_PRINT = 2;
public static final int HIDDEN_BUT_PRINTABLE = 3;
public static final int VISIBLE = 4;
Master PDF editor 也有类似的选项来隐藏字段。请参考附件截图。
注意:我的要求只是简单地隐藏该字段。它可以通过使用像 iText
这样的库或一些 PDF 编辑器工具以编程方式实现。
我使用带参数 PdfFormField.HIDDEN
的 setVisibility()
方法将 PdfFormField 的可见性设置为隐藏。尽管如此,在展平它时,该场并没有隐藏。下面是执行此操作的代码。
File file = new File("path to PDF file");
baos = new ByteArrayOutputStream();
pdfDoc = new PdfDocument(new PdfReader(file.getAbsolutePath()), new PdfWriter("path to flattened PDF file"));
//This function removes all the permissions.
removePdfPermissions();
form = PdfAcroForm.getAcroForm(pdfDoc, true);
fields = form.getFormFields();
fields.get("HumanSubjectsText").setVisibility(PdfFormField.HIDDEN);
fields.get("HumanSubjects").setVisibility(PdfFormField.HIDDEN);
form.flattenFields();
pdfDoc.close();
将字段的值设置为空字符串 ""
是一种解决方法,但不是正确的方法。
字段 "HumanSubjects"
和 "HumanSubjectsText"
对应于第 2 页上的复选框及其对应文本。(人类受试者)
无法使用 iText 库隐藏字段。与 Bruno 的评论相反,iText 库中的一种方法效果很好,setValue()
。我不太清楚为什么 setVisibility()
会失败。
但是,我可以使用 Master PDF editor 工具隐藏该字段。我附上了此工具的屏幕截图,您可以在其中从 General
选项卡更改表单字段的可见性。
一个观察:在com.itextpdf.forms.fields.PdfFormField
中定义的一些常量如下:
public static final int HIDDEN = 1;
public static final int VISIBLE_BUT_DOES_NOT_PRINT = 2;
public static final int HIDDEN_BUT_PRINTABLE = 3;
public static final int VISIBLE = 4;
Master PDF editor 也有类似的选项来隐藏字段。请参考附件截图。
注意:我的要求只是简单地隐藏该字段。它可以通过使用像 iText
这样的库或一些 PDF 编辑器工具以编程方式实现。