-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
close #429: Form field missing from merged result if annotation widge…
…t has invalid /P entry
- Loading branch information
1 parent
2f41684
commit 84f004c
Showing
4 changed files
with
55 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,10 +32,7 @@ | |
import org.sejda.sambox.cos.COSName; | ||
import org.sejda.sambox.pdmodel.PDDocument; | ||
import org.sejda.sambox.pdmodel.PDPage; | ||
import org.sejda.sambox.pdmodel.interactive.annotation.PDAnnotation; | ||
import org.sejda.sambox.pdmodel.interactive.annotation.PDAnnotationLink; | ||
import org.sejda.sambox.pdmodel.interactive.annotation.PDAnnotationMarkup; | ||
import org.sejda.sambox.pdmodel.interactive.annotation.PDAnnotationPopup; | ||
import org.sejda.sambox.pdmodel.interactive.annotation.*; | ||
import org.sejda.sambox.pdmodel.interactive.documentnavigation.destination.PDDestination; | ||
import org.sejda.sambox.pdmodel.interactive.documentnavigation.destination.PDPageDestination; | ||
import org.slf4j.Logger; | ||
|
@@ -80,6 +77,16 @@ public LookupTable<PDAnnotation> retainRelevantAnnotations(LookupTable<PDPage> r | |
if (nonNull(mapped)) { | ||
keptAnnotations.add(mapped); | ||
} else { | ||
// handle the scenario where the annotation page != actual render page | ||
// TODO: for safety, applying this only to form widgets for now | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
torakiki
Owner
|
||
if(annotation instanceof PDAnnotationWidget) { | ||
PDPage annotationPage = annotation.getPage(); | ||
if (annotationPage != null && !annotationPage.equals(page)) { | ||
// inconsistent annotation, this creates problems | ||
annotation.setPage(page); | ||
} | ||
} | ||
|
||
if (annotation instanceof PDAnnotationLink) { | ||
processLinkAnnotation(relevantPages, keptAnnotations, (PDAnnotationLink) annotation); | ||
} else { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,16 +30,13 @@ | |
import org.sejda.sambox.pdmodel.PDPage; | ||
import org.sejda.sambox.pdmodel.interactive.action.PDActionGoTo; | ||
import org.sejda.sambox.pdmodel.interactive.action.PDActionJavaScript; | ||
import org.sejda.sambox.pdmodel.interactive.annotation.PDAnnotation; | ||
import org.sejda.sambox.pdmodel.interactive.annotation.PDAnnotationLine; | ||
import org.sejda.sambox.pdmodel.interactive.annotation.PDAnnotationLink; | ||
import org.sejda.sambox.pdmodel.interactive.annotation.PDAnnotationMarkup; | ||
import org.sejda.sambox.pdmodel.interactive.annotation.PDAnnotationPopup; | ||
import org.sejda.sambox.pdmodel.interactive.annotation.PDAnnotationSquareCircle; | ||
import org.sejda.sambox.pdmodel.interactive.annotation.PDAnnotationText; | ||
import org.sejda.sambox.pdmodel.interactive.annotation.*; | ||
import org.sejda.sambox.pdmodel.interactive.documentnavigation.destination.PDNamedDestination; | ||
import org.sejda.sambox.pdmodel.interactive.documentnavigation.destination.PDPageDestination; | ||
import org.sejda.sambox.pdmodel.interactive.documentnavigation.destination.PDPageFitDestination; | ||
import org.sejda.sambox.pdmodel.interactive.form.PDAcroForm; | ||
import org.sejda.sambox.pdmodel.interactive.form.PDField; | ||
import org.sejda.sambox.pdmodel.interactive.form.PDTextField; | ||
|
||
import java.io.IOException; | ||
import java.util.Arrays; | ||
|
@@ -138,6 +135,8 @@ public void noLinks_PageNotRelevant() { | |
PDDocument doc = new PDDocument(); | ||
doc.addPage(oldPage); | ||
LookupTable<PDAnnotation> annotationsLookup = new AnnotationsDistiller(doc).retainRelevantAnnotations(lookup); | ||
|
||
// TODO: review this test; the annotation should be present in the merged result, even if /P is inconsistent | ||
This comment has been minimized.
Sorry, something went wrong.
ediweissmann
Author
Collaborator
|
||
assertEquals(0, newPage.getAnnotations().size()); | ||
assertTrue(annotationsLookup.isEmpty()); | ||
} | ||
|
@@ -154,6 +153,8 @@ public void noLinks_OnePageNotRelevantOneRelevant() { | |
PDDocument doc = new PDDocument(); | ||
doc.addPage(oldPage); | ||
LookupTable<PDAnnotation> annotationsLookup = new AnnotationsDistiller(doc).retainRelevantAnnotations(lookup); | ||
|
||
// TODO: review this test; the fist annotation should be present in the merged result, even if /P is inconsistent | ||
assertEquals(annotationsLookup.lookup(annotation2), newPage.getAnnotations().get(0)); | ||
} | ||
|
||
|
@@ -398,4 +399,22 @@ public void pagesHaveTheSameAnnotations() { | |
assertEquals(oldPage.getAnnotations().size(), newPage.getAnnotations().size()); | ||
assertEquals(secondOld.getAnnotations().size(), secondNew.getAnnotations().size()); | ||
} | ||
|
||
@Test | ||
public void dontRemoveFormFieldsWhenMergingIfWidgetPageDifferentThanActualDisplayPage() { | ||
PDDocument doc = new PDDocument(); | ||
PDAcroForm form = new PDAcroForm(doc); | ||
PDField field = new PDTextField(form); | ||
field.setPartialName("first_name"); | ||
PDAnnotationWidget annotation = new PDAnnotationWidget(field.getCOSObject()); | ||
// widget page is different than the actual parent page (where the widget is displayed) | ||
annotation.setPage(new PDPage()); | ||
List<PDAnnotation> annotations = List.of(annotation); | ||
oldPage.setAnnotations(annotations); | ||
|
||
doc.addPage(oldPage); | ||
LookupTable<PDAnnotation> annotationsLookup = new AnnotationsDistiller(doc).retainRelevantAnnotations(lookup); | ||
assertFalse(annotationsLookup.isEmpty()); | ||
assertEquals(1, newPage.getAnnotations().size()); | ||
} | ||
} |
Binary file added
BIN
+3.55 KB
sejda-tests/src/main/resources/pdf/sample_form_invalid_widget_page.pdf
Binary file not shown.
I think this should be applied to all annotations, not just widgets (form fields), but there are some test failures in AnnotationsDistillerTest related to non-link annotations.