[
https://issues.apache.org/jira/browse/PDFBOX-5720?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17790398#comment-17790398
]
Michael Bädorf commented on PDFBOX-5720:
----------------------------------------
I tried to create a unit test that is really similar to my implemented code.
{code:java}
import org.apache.pdfbox.Loader;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.interactive.form.PDCheckBox;
import org.apache.pdfbox.pdmodel.interactive.form.PDField;
import org.junit.jupiter.api.Test;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import static java.nio.file.Files.newInputStream;
import static java.nio.file.Paths.get;
import static java.util.Objects.nonNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class PDCheckboxTest {
@Test
void isChecked_EarlyAutoClose_WrongCheckedState() throws Exception {
// arrange
byte[] data;
try (InputStream is =
newInputStream(get("src/test/resources/ticket.pdf"))) {
assert nonNull(is);
data = is.readAllBytes();
}
List<PDField> fields = new ArrayList<>();
try (PDDocument document = Loader.loadPDF(data)) {
fields.addAll(document.getDocumentCatalog().getAcroForm().getFields());
}
// act
List<PDCheckBox> actual = filterCheckedCheckboxes(fields);
// assert
Optional<PDCheckBox> checkBox = actual.stream()
.filter(checkbox -> "A".equals(checkbox.getPartialName()))
.findFirst();
assertTrue(checkBox.isEmpty());
}
@Test
void isChecked_LateAutoClose_CorrectCheckedState() throws Exception {
// arrange
byte[] data;
try (InputStream is =
newInputStream(get("src/test/resources/ticket.pdf"))) {
assert nonNull(is);
data = is.readAllBytes();
}
List<PDField> fields = new ArrayList<>();
try (PDDocument document = Loader.loadPDF(data)) {
fields.addAll(document.getDocumentCatalog().getAcroForm().getFields());
// act
List<PDCheckBox> actual = filterCheckedCheckboxes(fields);
// assert
Optional<PDCheckBox> checkBox = actual.stream()
.filter(checkbox -> "A".equals(checkbox.getPartialName()))
.findFirst();
assertTrue(checkBox.isPresent());
} catch (Exception e) {
throw e;
}
}
<T extends PDField> List<T> filterByType(List<PDField> fields, Class<T>
clz) {
return fields.stream()
.filter(clz::isInstance)
.map(clz::cast)
.toList();
}
List<PDCheckBox> filterCheckedCheckboxes(List<PDField> fields) {
return filterByType(fields, PDCheckBox.class)
.stream()
.filter(PDCheckBox::isChecked)
.toList();
}
}
{code}
Result:
Leave the try block open til the end of all steps, the behavior is as expected.
Unfortunatley I havn't found this information in the migration guide. I'm not
sure if this should also mentioned there.
Ticket can be closed.
> Checked state of PDCheckbox
> ---------------------------
>
> Key: PDFBOX-5720
> URL: https://issues.apache.org/jira/browse/PDFBOX-5720
> Project: PDFBox
> Issue Type: Bug
> Components: AcroForm
> Affects Versions: 3.0.0 PDFBox
> Reporter: Michael Bädorf
> Priority: Major
> Attachments: ticket.pdf
>
>
> Before v3.0.0 is was possible to get the selected state simply by using the
> isChecked() function. After updating to v.3.0.0 the function always returns
> false with the same input PDF that works well til v2.0.30.
> There are no migration advices so I think this is a bug.
> This is the code I use for filtering selected checkboxes. but now always
> returns an empty list (in contrast to v2.0.30)
> {{{color:#000080}private {color}List<PDField>
> filterCheckedCheckboxes(List<PDField> fields) {}}
> {{ {color:#000080}return {color}filterByType(fields,
> PDCheckBox.{color:#000080}class{color})}}
> {{ .stream()}}
> {{ .filter(PDCheckBox::isChecked)}}
> {{ .map(PDField.{color:#000080}class{color}::cast)}}
> {{ .toList();}}
> }
>
> I can also find no test for PDCheckBox.isChecked()
--
This message was sent by Atlassian Jira
(v8.20.10#820010)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]