Skip to content

Commit 3f3df3c

Browse files
committed
PDFBOX-5980: fix BDC processor handling of properties, adjust test and rendering
git-svn-id: https://svn.apache.org/repos/asf/pdfbox/trunk@1924747 13f79535-47bb-0310-9956-ffa450edef68
1 parent de92705 commit 3f3df3c

File tree

3 files changed

+49
-20
lines changed

3 files changed

+49
-20
lines changed

pdfbox/src/main/java/org/apache/pdfbox/contentstream/operator/markedcontent/BeginMarkedContentSequenceWithProperties.java

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@
1919
import java.io.IOException;
2020
import java.util.List;
2121

22-
import org.apache.pdfbox.cos.COSBase;
23-
import org.apache.pdfbox.cos.COSDictionary;
24-
import org.apache.pdfbox.cos.COSName;
2522
import org.apache.pdfbox.contentstream.PDFStreamEngine;
23+
import org.apache.pdfbox.contentstream.operator.MissingOperandException;
2624
import org.apache.pdfbox.contentstream.operator.Operator;
2725
import org.apache.pdfbox.contentstream.operator.OperatorName;
2826
import org.apache.pdfbox.contentstream.operator.OperatorProcessor;
27+
import org.apache.pdfbox.cos.COSBase;
28+
import org.apache.pdfbox.cos.COSDictionary;
29+
import org.apache.pdfbox.cos.COSName;
30+
import org.apache.pdfbox.pdmodel.documentinterchange.markedcontent.PDPropertyList;
2931

3032
/**
3133
* BDC : Begins a marked-content sequence with property list.
@@ -40,22 +42,39 @@ public BeginMarkedContentSequenceWithProperties(PDFStreamEngine context)
4042
}
4143

4244
@Override
43-
public void process(Operator operator, List<COSBase> arguments) throws IOException
45+
public void process(Operator operator, List<COSBase> operands) throws IOException
4446
{
45-
COSName tag = null;
46-
COSDictionary properties = null;
47-
for (COSBase argument : arguments)
47+
if (operands.size() < 2)
4848
{
49-
if (argument instanceof COSName)
50-
{
51-
tag = (COSName) argument;
52-
}
53-
else if (argument instanceof COSDictionary)
49+
throw new MissingOperandException(operator, operands);
50+
}
51+
if (!(operands.get(0) instanceof COSName))
52+
{
53+
return;
54+
}
55+
PDFStreamEngine context = getContext();
56+
COSName tag = (COSName) operands.get(0);
57+
COSBase op1 = operands.get(1);
58+
COSDictionary propDict = null;
59+
if (op1 instanceof COSName)
60+
{
61+
// PDFBOX-5980 and SO79549651
62+
PDPropertyList prop = context.getResources().getProperties((COSName) op1);
63+
if (prop != null)
5464
{
55-
properties = (COSDictionary) argument;
65+
propDict = prop.getCOSObject();
5666
}
5767
}
58-
getContext().beginMarkedContentSequence(tag, properties);
68+
else if (op1 instanceof COSDictionary)
69+
{
70+
propDict = (COSDictionary) op1;
71+
}
72+
if (propDict == null)
73+
{
74+
// wrong type or property not found
75+
return;
76+
}
77+
context.beginMarkedContentSequence(tag, propDict);
5978
}
6079

6180
@Override

pdfbox/src/main/java/org/apache/pdfbox/rendering/PageDrawer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2093,11 +2093,11 @@ public void beginMarkedContentSequence(COSName tag, COSDictionary properties)
20932093
nestedHiddenOCGCount++;
20942094
return;
20952095
}
2096-
if (tag == null || getResources() == null)
2096+
if (properties == null)
20972097
{
20982098
return;
20992099
}
2100-
if (isHiddenOCG(getResources().getProperties(tag)))
2100+
if (isHiddenOCG(PDPropertyList.create(properties)))
21012101
{
21022102
nestedHiddenOCGCount = 1;
21032103
}

pdfbox/src/test/java/org/apache/pdfbox/pdmodel/graphics/optionalcontent/TestOptionalContentGroups.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919

2020
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
21-
import static org.junit.jupiter.api.Assertions.assertEquals;
2221
import static org.junit.jupiter.api.Assertions.assertFalse;
2322
import static org.junit.jupiter.api.Assertions.assertNotNull;
2423
import static org.junit.jupiter.api.Assertions.assertNull;
@@ -47,13 +46,15 @@
4746
import org.apache.pdfbox.pdmodel.PDPageContentStream.AppendMode;
4847
import org.apache.pdfbox.pdmodel.PageMode;
4948
import org.apache.pdfbox.pdmodel.documentinterchange.markedcontent.PDMarkedContent;
49+
import org.apache.pdfbox.pdmodel.documentinterchange.markedcontent.PDPropertyList;
5050
import org.apache.pdfbox.pdmodel.font.PDFont;
5151
import org.apache.pdfbox.pdmodel.font.PDType1Font;
5252
import org.apache.pdfbox.pdmodel.font.Standard14Fonts.FontName;
5353
import org.apache.pdfbox.pdmodel.graphics.optionalcontent.PDOptionalContentProperties.BaseState;
5454
import org.apache.pdfbox.rendering.PDFRenderer;
5555
import org.apache.pdfbox.text.PDFMarkedContentExtractor;
5656
import org.apache.pdfbox.text.TextPosition;
57+
import static org.junit.jupiter.api.Assertions.assertEquals;
5758
import org.junit.jupiter.api.BeforeAll;
5859
import org.junit.jupiter.api.Test;
5960

@@ -215,14 +216,23 @@ void testOCGConsumption() throws Exception
215216
PDFMarkedContentExtractor extractor = new PDFMarkedContentExtractor();
216217
extractor.processPage(page);
217218
List<PDMarkedContent> markedContents = extractor.getMarkedContents();
218-
assertEquals("oc1", markedContents.get(0).getTag());
219+
assertEquals("OC", markedContents.get(0).getTag());
220+
PDOptionalContentGroup ocg1 =
221+
(PDOptionalContentGroup) PDPropertyList.create(markedContents.get(0).getProperties());
222+
assertEquals("background", ocg1.getName());
219223
assertEquals("PDF 1.5: Optional Content Groups"
220224
+ "You should see a green textline, but no red text line.",
221225
textPositionListToString(markedContents.get(0).getContents()));
222-
assertEquals("oc2", markedContents.get(1).getTag());
226+
assertEquals("OC", markedContents.get(1).getTag());
227+
PDOptionalContentGroup ocg2 =
228+
(PDOptionalContentGroup) PDPropertyList.create(markedContents.get(1).getProperties());
229+
assertEquals("enabled", ocg2.getName());
223230
assertEquals("This is from an enabled layer. If you see this, that's good.",
224231
textPositionListToString(markedContents.get(1).getContents()));
225-
assertEquals("oc3", markedContents.get(2).getTag());
232+
assertEquals("OC", markedContents.get(2).getTag());
233+
PDOptionalContentGroup ocg3 =
234+
(PDOptionalContentGroup) PDPropertyList.create(markedContents.get(2).getProperties());
235+
assertEquals("disabled", ocg3.getName());
226236
assertEquals("This is from a disabled layer. If you see this, that's NOT good!",
227237
textPositionListToString(markedContents.get(2).getContents()));
228238
}

0 commit comments

Comments
 (0)