Skip to content

Commit

Permalink
Fixes #338 Mark radio group as readonly if all are readonly
Browse files Browse the repository at this point in the history
With test.
  • Loading branch information
danfickle committed Dec 23, 2020
1 parent a351828 commit 989e0de
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<html>
<body>
<form>
<input type="radio" name="choice" value="1" readonly=""/>
<input type="radio" name="choice" value="2" readonly="" checked=""/>
</form>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.openhtmltopdf.nonvisualregressiontests;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.instanceOf;
Expand Down Expand Up @@ -29,6 +30,7 @@
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDDocumentOutline;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDOutlineItem;
import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
import org.apache.pdfbox.pdmodel.interactive.form.PDRadioButton;
import org.apache.pdfbox.pdmodel.interactive.form.PDTextField;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.pdfbox.util.Charsets;
Expand Down Expand Up @@ -732,6 +734,17 @@ public void testInputWithoutNameAttribute() throws IOException {
remove("input-without-name-attribute", doc);
}

@Test
public void testIssue338RadioReadOnly() throws IOException {
PDDocument doc = run("issue-338-radio-read-only");
PDAcroForm form = doc.getDocumentCatalog().getAcroForm(null);

PDRadioButton radio = (PDRadioButton) form.getFields().get(0);
assertTrue("radio should be readonly", radio.isReadOnly());

remove("issue-338-radio-read-only", doc);
}

private static float[] getQuadPoints(PDDocument doc, int pg, int linkIndex) throws IOException {
return ((PDAnnotationLink) doc.getPage(pg).getAnnotations().get(linkIndex)).getQuadPoints();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.Map;
import java.util.logging.Level;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import com.openhtmltopdf.util.LogMessageId;
import org.apache.pdfbox.cos.COSArray;
Expand Down Expand Up @@ -627,13 +628,20 @@ private void processRadioButtonGroup(List<Control> group, PDAcroForm acro, int i

Field fObj = allFieldMap.get(groupName);
setPartialNameToField(group.get(0).box.getElement(), fObj, field);
List<String> values = new ArrayList<>(group.size());
for (Control ctrl : group) {
values.add(ctrl.box.getElement().getAttribute("value"));
}

List<String> values =
group.stream()
.map(ctrl -> ctrl.box.getElement().getAttribute("value"))
.collect(Collectors.toList());
field.setExportValues(values);


// We can not make individual members of the group readonly so only make
// all radio buttons in group readonly if they are all marked readonly.
boolean readonly =
group.stream()
.allMatch(ctrl -> ctrl.box.getElement().hasAttribute("readonly"));
field.setReadOnly(readonly);

List<PDAnnotationWidget> widgets = new ArrayList<>(group.size());

int radioCnt = 0;
Expand Down

0 comments on commit 989e0de

Please sign in to comment.