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 2026/10/08 03:08:10 UTC

svn commit: r1938893 - in pdfbox/trunk/pdfbox/src: main/java/org/apache/pdfbox/pdmodel/graphics test/java/org/apache/pdfbox/pdmodel/graphics/form

Author: tilman
Date: Thu Oct  8 03:08:10 2026
New Revision: 1938893

Log:
PDFBOX-6271: don't let the xobject get dirty, as suggested by Jakob Heher and ChatGPT

Added:
   pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/graphics/form/
   pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/graphics/form/PDFormXObjectTest.java   (contents, props changed)
Modified:
   pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/PDXObject.java

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/PDXObject.java
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/PDXObject.java	Thu Oct  8 03:08:06 2026	(r1938892)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/PDXObject.java	Thu Oct  8 03:08:10 2026	(r1938893)
@@ -98,9 +98,7 @@ public class PDXObject implements COSObj
     protected PDXObject(COSStream stream, COSName subtype)
     {
         this.stream = new PDStream(stream);
-        // could be used for writing:
-        stream.setName(COSName.TYPE, COSName.XOBJECT.getName());
-        stream.setName(COSName.SUBTYPE, subtype.getName());
+        maybeInitNames(stream, subtype);
     }
 
     /**
@@ -112,9 +110,7 @@ public class PDXObject implements COSObj
     protected PDXObject(PDStream stream, COSName subtype)
     {
         this.stream = stream;
-        // could be used for writing:
-        stream.getCOSObject().setName(COSName.TYPE, COSName.XOBJECT.getName());
-        stream.getCOSObject().setName(COSName.SUBTYPE, subtype.getName());
+        maybeInitNames(stream.getCOSObject(), subtype);
     }
 
     /**
@@ -125,8 +121,19 @@ public class PDXObject implements COSObj
     protected PDXObject(PDDocument document, COSName subtype)
     {
         stream = new PDStream(document);
-        stream.getCOSObject().setName(COSName.TYPE, COSName.XOBJECT.getName());
-        stream.getCOSObject().setName(COSName.SUBTYPE, subtype.getName());
+        maybeInitNames(stream.getCOSObject(), subtype);
+    }
+
+    private void maybeInitNames(COSStream strm, COSName subtype)
+    {
+        if (!COSName.XOBJECT.equals(strm.getItem(COSName.TYPE)))
+        {
+            strm.setName(COSName.TYPE, COSName.XOBJECT.getName());
+        }
+        if (!subtype.equals(strm.getItem(COSName.SUBTYPE)))
+        {
+            strm.setName(COSName.SUBTYPE, subtype.getName());
+        }
     }
 
     /**

Added: pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/graphics/form/PDFormXObjectTest.java
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/graphics/form/PDFormXObjectTest.java	Thu Oct  8 03:08:10 2026	(r1938893)
@@ -0,0 +1,51 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pdfbox.pdmodel.graphics.form;
+
+import org.apache.pdfbox.cos.COSDocumentState;
+import org.apache.pdfbox.cos.COSName;
+import org.apache.pdfbox.cos.COSStream;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import org.junit.jupiter.api.Test;
+
+public class PDFormXObjectTest
+{
+
+    /**
+     * PDFBOX-6271: check that a correct form xobject doesn't get dirty.
+     */
+    @Test
+    void testDontDirtyOnConstruction()
+    {
+        // setup - build a cosstream that will accept update flags:
+        COSStream s = new COSStream();
+        COSDocumentState state = new COSDocumentState();
+        state.setParsing(false);
+        s.getUpdateState().setOriginDocumentState(state);
+
+        // set type/subtype correctly for an XObject:
+        // (this stands in for an XObject that was parsed from an existing document)
+        s.setItem(COSName.TYPE, COSName.XOBJECT);
+        s.setItem(COSName.SUBTYPE, COSName.FORM);
+        s.setNeedToBeUpdated(false);
+
+        // construct a PDFormXObject wrapper around it:
+        new PDFormXObject(s);
+
+        assertFalse(s.isNeedToBeUpdated());
+    }
+}