Skip to content

Commit 0a2debb

Browse files
eamonnmoloneyBenoit Lagae
authored andcommitted
Fix for ClassCastException when using PdfA
The test in this commit shows the class cast exception when using background color with PdfA. The change to AbstractRenderer shows a "fix", however I really doubt this is a correct fix for this issue. Test to show problem with background color in PdfA Fix to abstract renderer for class cast exception... Note I do not believe this is the correct fix :)
1 parent 7d49340 commit 0a2debb

File tree

3 files changed

+81
-1
lines changed

3 files changed

+81
-1
lines changed

layout/src/main/java/com/itextpdf/layout/renderer/AbstractRenderer.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,19 @@ public PdfFont getPropertyAsFont(int property) {
294294
* @return a {@link Color}
295295
*/
296296
public Color getPropertyAsColor(int property) {
297-
return this.<Color>getProperty(property);
297+
Object object = getProperty(property);
298+
299+
if(object == null) {
300+
return null;
301+
}
302+
303+
if(object instanceof Color) {
304+
return (Color) object;
305+
} else if (object instanceof Background) {
306+
return ((Background)object).getColor();
307+
}
308+
309+
throw new IllegalStateException("The type of property is unexpected.. property: " + property + ", class: " + object.getClass());
298310
}
299311

300312
/**
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.itextpdf.layout;
2+
3+
import com.itextpdf.kernel.color.Color;
4+
import com.itextpdf.kernel.pdf.PdfDocument;
5+
import com.itextpdf.kernel.pdf.PdfWriter;
6+
import com.itextpdf.kernel.utils.CompareTool;
7+
import com.itextpdf.kernel.xmp.XMPException;
8+
import com.itextpdf.layout.element.Paragraph;
9+
10+
import java.io.FileOutputStream;
11+
import java.io.IOException;
12+
13+
import com.itextpdf.layout.element.Text;
14+
import com.itextpdf.test.ExtendedITextTest;
15+
import com.itextpdf.test.annotations.type.IntegrationTest;
16+
import org.junit.Assert;
17+
import org.junit.BeforeClass;
18+
import org.junit.Rule;
19+
import org.junit.Test;
20+
import org.junit.experimental.categories.Category;
21+
import org.junit.rules.ExpectedException;
22+
23+
24+
@Category(IntegrationTest.class)
25+
public class BackgroundColorTest extends ExtendedITextTest {
26+
27+
public static final String sourceFolder = "./src/test/resources/com/itextpdf/layout/BackgroundColorTest/";
28+
public static final String destinationFolder = "./target/test/com/itextpdf/layout/BackgroundColorTest/";
29+
public static final String cmpPrefix = "cmp_";
30+
31+
String fileName;
32+
String outFileName;
33+
String cmpFileName;
34+
35+
@BeforeClass
36+
public static void beforeClass() {
37+
createDestinationFolder(destinationFolder);
38+
}
39+
40+
@Rule
41+
public ExpectedException thrown = ExpectedException.none();
42+
43+
@Test
44+
public void shouldAddBackgroundColorAttributeToAccessiblityWhenBackgroundColorIsSet() throws IOException, XMPException, InterruptedException {
45+
fileName = "simpleBackgroundColorTest.pdf";
46+
outFileName = destinationFolder + fileName;
47+
cmpFileName = sourceFolder + cmpPrefix + fileName;
48+
49+
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(new FileOutputStream(outFileName)));
50+
pdfDocument.setTagged();
51+
Document doc = new Document(pdfDocument);
52+
53+
Text foo = new Text("foo");
54+
foo.setBackgroundColor(Color.BLUE);
55+
doc.add(new Paragraph(foo));
56+
57+
closeDocumentAndCompareOutputs(doc);
58+
}
59+
60+
private void closeDocumentAndCompareOutputs(Document document) throws IOException, InterruptedException {
61+
document.close();
62+
String compareResult = new CompareTool().compareByContent(outFileName, cmpFileName, destinationFolder, "diff");
63+
if (compareResult != null) {
64+
Assert.fail(compareResult);
65+
}
66+
}
67+
}
68+

0 commit comments

Comments
 (0)