Skip to content

Commit 77ea061

Browse files
committed
Assign Form role to widget annotation only if it's not a part of Form role struct element already
It allows to create widget annotations with labels. DEVSIX-7965
1 parent cd02c8e commit 77ea061

File tree

5 files changed

+47
-6
lines changed

5 files changed

+47
-6
lines changed

forms/src/main/java/com/itextpdf/forms/fields/PdfFormAnnotationUtil.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,9 @@ public static void addWidgetAnnotationToPage(PdfPage page, PdfAnnotation annotat
101101
if (tagged) {
102102
tagPointer = document.getTagStructureContext().getAutoTaggingPointer();
103103
//TODO DEVSIX-4117 PrintField attributes
104-
tagPointer.addTag(StandardRoles.FORM);
104+
if (!StandardRoles.FORM.equals(tagPointer.getRole())) {
105+
tagPointer.addTag(StandardRoles.FORM);
106+
}
105107
}
106108

107109
page.addAnnotation(index, annotation, true);

forms/src/test/java/com/itextpdf/forms/FormFieldsTaggingTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,37 @@ public void formFieldTaggingTest10() throws IOException, InterruptedException, P
274274
compareOutput(outFileName, cmpFileName);
275275
}
276276

277+
@Test
278+
public void formFieldTaggingTest11() throws IOException, InterruptedException, ParserConfigurationException, SAXException {
279+
String outFileName = destinationFolder + "taggedPdfWithForms11.pdf";
280+
String cmpFileName = sourceFolder + "cmp_taggedPdfWithForms11.pdf";
281+
282+
PdfWriter writer = new PdfWriter(outFileName);
283+
PdfReader reader = new PdfReader(sourceFolder + "taggedDocWithFields.pdf");
284+
PdfDocument pdfDoc = new PdfDocument(reader, writer);
285+
pdfDoc.setTagged();
286+
287+
PdfAcroForm acroForm = PdfFormCreator.getAcroForm(pdfDoc, true);
288+
289+
PdfButtonFormField pushButton = new PushButtonFormFieldBuilder(pdfDoc, "push")
290+
.setWidgetRectangle(new Rectangle(36, 650, 40, 20)).setCaption("Button 1").createPushButton();
291+
pushButton.setFontSize(12f);
292+
293+
PdfButtonFormField pushButton2 = new PushButtonFormFieldBuilder(pdfDoc, "push 2")
294+
.setWidgetRectangle(new Rectangle(36, 600, 40, 20)).setCaption("Button 2").createPushButton();
295+
pushButton.setFontSize(12f);
296+
297+
TagTreePointer tagPointer = pdfDoc.getTagStructureContext().getAutoTaggingPointer();
298+
tagPointer.moveToKid(StandardRoles.DIV);
299+
acroForm.addField(pushButton);
300+
tagPointer.moveToKid(StandardRoles.FORM);
301+
acroForm.addField(pushButton2);
302+
303+
pdfDoc.close();
304+
305+
compareOutput(outFileName, cmpFileName);
306+
}
307+
277308
private void addFormFieldsToDocument(PdfDocument pdfDoc, PdfAcroForm acroForm) {
278309
Rectangle rect = new Rectangle(36, 700, 20, 20);
279310
Rectangle rect1 = new Rectangle(36, 680, 20, 20);

forms/src/test/java/com/itextpdf/forms/PdfUA2FormTest.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ This file is part of the iText (R) project.
5050
import com.itextpdf.kernel.xmp.XMPMetaFactory;
5151
import com.itextpdf.layout.Document;
5252
import com.itextpdf.layout.borders.SolidBorder;
53+
import com.itextpdf.layout.element.Div;
54+
import com.itextpdf.layout.element.Paragraph;
5355
import com.itextpdf.test.ExtendedITextTest;
5456
import com.itextpdf.test.annotations.type.IntegrationTest;
5557
import com.itextpdf.test.pdfa.VeraPdfValidator; // Android-Conversion-Skip-Line (TODO DEVSIX-7377 introduce pdf\a validation on Android)
@@ -100,7 +102,7 @@ public void checkFormFieldTest() throws IOException, XMPException, InterruptedEx
100102
}
101103

102104
@Test
103-
public void checkTextAreaTest() throws IOException, XMPException, InterruptedException {
105+
public void checkTextAreaWithLabelTest() throws IOException, XMPException, InterruptedException {
104106
String outFile = DESTINATION_FOLDER + "textAreaTest.pdf";
105107
String cmpFile = SOURCE_FOLDER + "cmp_textAreaTest.pdf";
106108

@@ -112,13 +114,19 @@ public void checkTextAreaTest() throws IOException, XMPException, InterruptedExc
112114
document.setFont(font);
113115
createSimplePdfUA2Document(pdfDocument);
114116

115-
TextArea formTextArea = new TextArea("form text area");
117+
Paragraph paragraph = new Paragraph("Widget label").setFont(font);
118+
paragraph.getAccessibilityProperties().setRole(StandardRoles.LBL);
119+
120+
TextArea formTextArea = new TextArea("form text1");
116121
formTextArea.setProperty(FormProperty.FORM_FIELD_FLATTEN, false);
117122
formTextArea.setProperty(FormProperty.FORM_FIELD_VALUE, "form\ntext\narea");
118123

119-
document.add(formTextArea);
120-
PdfAcroForm form = PdfFormCreator.getAcroForm(pdfDocument, true);
121-
form.getField("form text area").getPdfObject().put(PdfName.Contents, new PdfString("Description"));
124+
Div div = new Div();
125+
div.getAccessibilityProperties().setRole(StandardRoles.FORM);
126+
div.add(paragraph);
127+
div.add(formTextArea);
128+
129+
document.add(div);
122130
}
123131
compareAndValidate(outFile, cmpFile);
124132
}
Binary file not shown.

0 commit comments

Comments
 (0)