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 2022/02/28 18:13:36 UTC

svn commit: r1898485 - /pdfbox/branches/2.0/examples/src/main/java/org/apache/pdfbox/examples/signature/SigUtils.java

Author: tilman
Date: Mon Feb 28 18:13:35 2022
New Revision: 1898485

URL: http://svn.apache.org/viewvc?rev=1898485&view=rev
Log:
PDFBOX-5382: add method to detect gaps in cross reference table

Modified:
    pdfbox/branches/2.0/examples/src/main/java/org/apache/pdfbox/examples/signature/SigUtils.java

Modified: pdfbox/branches/2.0/examples/src/main/java/org/apache/pdfbox/examples/signature/SigUtils.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/examples/src/main/java/org/apache/pdfbox/examples/signature/SigUtils.java?rev=1898485&r1=1898484&r2=1898485&view=diff
==============================================================================
--- pdfbox/branches/2.0/examples/src/main/java/org/apache/pdfbox/examples/signature/SigUtils.java (original)
+++ pdfbox/branches/2.0/examples/src/main/java/org/apache/pdfbox/examples/signature/SigUtils.java Mon Feb 28 18:13:35 2022
@@ -27,12 +27,14 @@ import java.util.List;
 import java.util.Set;
 import java.util.SortedMap;
 import java.util.TreeMap;
+import java.util.TreeSet;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.pdfbox.cos.COSArray;
 import org.apache.pdfbox.cos.COSBase;
 import org.apache.pdfbox.cos.COSDictionary;
 import org.apache.pdfbox.cos.COSName;
+import org.apache.pdfbox.cos.COSObjectKey;
 import org.apache.pdfbox.examples.signature.cert.CertificateVerificationException;
 import org.apache.pdfbox.examples.signature.cert.CertificateVerifier;
 import org.apache.pdfbox.pdmodel.PDDocument;
@@ -344,4 +346,29 @@ public class SigUtils
         // https://ec.europa.eu/digital-single-market/en/eu-trusted-lists-trust-service-providers
         // ( getRootCertificates() is not helpful because these are SSL certificates)
     }
+
+    /**
+     * Look for gaps in the cross reference table and display warnings if any found. See also
+     * <a href="https://stackoverflow.com/questions/71267471/">here</a>.
+     *
+     * @param doc document.
+     */
+    public void checkCrossReferenceTable(PDDocument doc)
+    {
+        TreeSet<COSObjectKey> set = new TreeSet<COSObjectKey>(doc.getDocument().getXrefTable().keySet());
+        if (set.size() != set.last().getNumber())
+        {
+            long n = 0;
+            for (COSObjectKey key : set)
+            {
+                ++n;
+                while (n < key.getNumber())
+                {
+                    LOG.warn("Object " + n + " missing, signature verification may fail in " +
+                             "Adobe Reader, see https://stackoverflow.com/questions/71267471/");
+                    ++n;
+                }
+            }
+        }
+    }
 }