You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ti...@apache.org on 2024/11/22 13:49:21 UTC
svn commit: r1922019 - in /pdfbox/trunk/pdfbox/src: main/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroForm.java test/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroFormFlattenTest.java
Author: tilman
Date: Fri Nov 22 13:49:20 2024
New Revision: 1922019
URL: http://svn.apache.org/viewvc?rev=1922019&view=rev
Log:
PDFBOX-5225: consider the field list parameter when there are widgets with missing page references; adjust / add test
Modified:
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroForm.java
pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroFormFlattenTest.java
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroForm.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroForm.java?rev=1922019&r1=1922018&r2=1922019&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroForm.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroForm.java Fri Nov 22 13:49:20 2024
@@ -759,6 +759,13 @@ public final class PDAcroForm implements
return transformedAppearanceBox.getBounds2D();
}
+ /**
+ * Build a map of pages => widgets
+ * @param fields a list of fields to be flattened
+ * @param pages the page tree
+ * @return
+ * @throws IOException
+ */
private Map<COSDictionary,Set<COSDictionary>> buildPagesWidgetsMap(
List<PDField> fields, PDPageTree pages) throws IOException
{
@@ -777,6 +784,7 @@ public final class PDAcroForm implements
}
else
{
+ LOG.warn("missing /P entry (page reference) in a widget for field: " + field);
hasMissingPageRef = true;
}
}
@@ -790,11 +798,21 @@ public final class PDAcroForm implements
// If there is a widget with a missing page reference we need to build the map reverse i.e.
// from the annotations to the widget.
LOG.warn("There has been a widget with a missing page reference, will check all page annotations");
+ Set<COSDictionary> widgetDictionarySet = new HashSet<>();
+ for (PDField field : fields)
+ {
+ List<PDAnnotationWidget> widgets = field.getWidgets();
+ for (PDAnnotationWidget widget : widgets)
+ {
+ widgetDictionarySet.add(widget.getCOSObject());
+ }
+ }
+
for (PDPage page : pages)
{
for (PDAnnotation annotation : page.getAnnotations())
{
- if (annotation instanceof PDAnnotationWidget)
+ if (widgetDictionarySet.contains(annotation.getCOSObject()))
{
fillPagesAnnotationMap(pagesAnnotationsMap, page, (PDAnnotationWidget) annotation);
}
Modified: pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroFormFlattenTest.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroFormFlattenTest.java?rev=1922019&r1=1922018&r2=1922019&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroFormFlattenTest.java (original)
+++ pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroFormFlattenTest.java Fri Nov 22 13:49:20 2024
@@ -30,6 +30,7 @@ import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
+import java.util.Iterator;
import java.util.List;
import java.util.stream.Stream;
@@ -37,7 +38,6 @@ import javax.imageio.ImageIO;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.Loader;
-import org.apache.pdfbox.pdfwriter.compress.CompressParameters;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.apache.pdfbox.rendering.TestPDFToImage;
import org.junit.jupiter.api.BeforeAll;
@@ -130,9 +130,6 @@ class PDAcroFormFlattenTest
// annotations.
"https://issues.apache.org/jira/secure/attachment/12994791/flatten.pdf,PDFBOX-4788.pdf",
- // PDFBOX-4889: appearance streams with empty /BBox.
- "https://issues.apache.org/jira/secure/attachment/13005793/f1040sb%20test.pdf,PDFBOX-4889.pdf",
-
// PDFBOX-4955: appearance streams with forms that are not used.
"https://issues.apache.org/jira/secure/attachment/13011410/PDFBOX-4955.pdf,PDFBOX-4955.pdf",
@@ -183,8 +180,9 @@ class PDAcroFormFlattenTest
{
testPdf.getDocumentCatalog().getAcroForm().flatten();
testPdf.setAllSecurityToBeRemoved(true);
- assertTrue(testPdf.getDocumentCatalog().getAcroForm().getFields().isEmpty());
- testPdf.save(outputFile, CompressParameters.NO_COMPRESSION);
+ testPdf.save(outputFile);
+ assertTrue(testPdf.getDocumentCatalog().getAcroForm(null).getFields().isEmpty());
+ assertEquals(72, testPdf.getPage(0).getAnnotations().size());
}
// compare rendering
@@ -195,6 +193,61 @@ class PDAcroFormFlattenTest
+ " failed or is not identical to expected rendering in " + IN_DIR
+ " directory");
}
+ else
+ {
+ // cleanup input and output directory for matching files.
+ removeAllRenditions(inputFile);
+ inputFile.delete();
+ outputFile.delete();
+ }
+ }
+
+ /**
+ * Check that only VN_Name is removed in the field tree and in the annotations list. That field
+ * has an "orphan" widget that belongs to no page.
+ *
+ * @throws IOException
+ * @throws URISyntaxException
+ */
+ @Test
+ void flattenTestPDFBOX5225() throws IOException, URISyntaxException
+ {
+ String sourceUrl = "https://issues.apache.org/jira/secure/attachment/13027311/SourceFailure.pdf";
+ String targetFileName = "PDFBOX-5225.pdf";
+
+ generateSamples(sourceUrl, targetFileName);
+
+ File inputFile = new File(IN_DIR, targetFileName);
+ File outputFile = new File(OUT_DIR, targetFileName);
+
+ try (PDDocument testPdf = Loader.loadPDF(inputFile))
+ {
+ PDAcroForm acroForm = testPdf.getDocumentCatalog().getAcroForm();
+ List<PDField> list = new ArrayList<>();
+ list.add(acroForm.getField("VN_NAME"));
+ acroForm.flatten(list, false);
+ testPdf.setAllSecurityToBeRemoved(true);
+ testPdf.save(outputFile);
+ int count = 0;
+ Iterator<PDField> iterator = acroForm.getFieldTree().iterator();
+ while (iterator.hasNext())
+ {
+ iterator.next();
+ ++count;
+ }
+ assertEquals(76, count);
+ assertEquals(59, testPdf.getPage(0).getAnnotations().size());
+ }
+
+ // compare rendering
+ if (!TestPDFToImage.doTestFile(outputFile, IN_DIR.getAbsolutePath(),
+ OUT_DIR.getAbsolutePath()))
+ {
+ // check manually
+ System.err.println("Rendering of " + outputFile
+ + " failed or is not identical to expected rendering in " + IN_DIR
+ + " directory");
+ }
else
{
// cleanup input and output directory for matching files.