@@ -44,6 +44,9 @@ This file is part of the iText (R) project.
44
44
45
45
import com .itextpdf .io .LogMessageConstant ;
46
46
import com .itextpdf .io .source .ByteArrayOutputStream ;
47
+ import com .itextpdf .kernel .KernelLogMessageConstant ;
48
+ import com .itextpdf .kernel .PdfException ;
49
+ import com .itextpdf .kernel .colors .Color ;
47
50
import com .itextpdf .kernel .pdf .PdfDocument ;
48
51
import com .itextpdf .kernel .pdf .PdfPage ;
49
52
import com .itextpdf .kernel .pdf .PdfReader ;
@@ -55,6 +58,8 @@ This file is part of the iText (R) project.
55
58
import com .itextpdf .kernel .pdf .canvas .parser .data .TextRenderInfo ;
56
59
import com .itextpdf .kernel .pdf .canvas .parser .listener .IEventListener ;
57
60
import com .itextpdf .kernel .pdf .canvas .parser .listener .LocationTextExtractionStrategy ;
61
+ import com .itextpdf .kernel .pdf .colorspace .PdfColorSpace ;
62
+ import com .itextpdf .kernel .pdf .colorspace .PdfSpecialCs ;
58
63
import com .itextpdf .test .ExtendedITextTest ;
59
64
import com .itextpdf .test .annotations .LogMessage ;
60
65
import com .itextpdf .test .annotations .LogMessages ;
@@ -63,6 +68,8 @@ This file is part of the iText (R) project.
63
68
import java .nio .charset .StandardCharsets ;
64
69
import java .nio .file .Files ;
65
70
import java .nio .file .Paths ;
71
+ import java .util .ArrayList ;
72
+ import java .util .List ;
66
73
import org .junit .Assert ;
67
74
import org .junit .Ignore ;
68
75
import org .junit .Rule ;
@@ -143,6 +150,82 @@ public void parseCircularReferencesInResourcesTest() throws IOException {
143
150
pdfDocument .close ();
144
151
}
145
152
153
+ @ Test
154
+ @ LogMessages (messages = @ LogMessage (messageTemplate = KernelLogMessageConstant .UNABLE_TO_PARSE_COLOR_WITHIN_COLORSPACE ))
155
+ public void patternColorParsingNotValidPdfTest () throws IOException {
156
+ String inputFile = sourceFolder + "patternColorParsingNotValidPdfTest.pdf" ;
157
+ PdfDocument pdfDocument = new PdfDocument (new PdfReader (inputFile ));
158
+
159
+ for (int i = 1 ; i <= pdfDocument .getNumberOfPages (); ++i ) {
160
+ PdfPage page = pdfDocument .getPage (i );
161
+
162
+ ColorParsingEventListener colorParsingEventListener = new ColorParsingEventListener ();
163
+
164
+ PdfCanvasProcessor processor = new PdfCanvasProcessor (colorParsingEventListener );
165
+ processor .processPageContent (page );
166
+
167
+ Color renderInfo = colorParsingEventListener .getEncounteredPath ().getFillColor ();
168
+
169
+ Assert .assertNull (renderInfo );
170
+ }
171
+ }
172
+
173
+ @ Test
174
+ public void patternColorParsingValidPdfTest () throws IOException {
175
+ String inputFile = sourceFolder + "patternColorParsingValidPdfTest.pdf" ;
176
+ PdfDocument pdfDocument = new PdfDocument (new PdfReader (inputFile ));
177
+
178
+ for (int i = 1 ; i <= pdfDocument .getNumberOfPages (); ++i ) {
179
+ PdfPage page = pdfDocument .getPage (i );
180
+
181
+ ColorParsingEventListener colorParsingEventListener = new ColorParsingEventListener ();
182
+
183
+ PdfCanvasProcessor processor = new PdfCanvasProcessor (colorParsingEventListener );
184
+ processor .processPageContent (page );
185
+
186
+ PathRenderInfo renderInfo = colorParsingEventListener .getEncounteredPath ();
187
+ PdfColorSpace colorSpace = renderInfo .getGraphicsState ().getFillColor ().getColorSpace ();
188
+
189
+ Assert .assertTrue (colorSpace instanceof PdfSpecialCs .Pattern );
190
+ }
191
+ }
192
+
193
+ private static class ColorParsingEventListener implements IEventListener {
194
+ private List <IEventData > content = new ArrayList <>();
195
+ private static final String pathDataExpected = "Path data expected." ;
196
+
197
+ public void eventOccurred (IEventData data , EventType type ) {
198
+ if (type .equals (EventType .RENDER_PATH )) {
199
+ PathRenderInfo pathRenderInfo = (PathRenderInfo ) data ;
200
+ pathRenderInfo .preserveGraphicsState ();
201
+ content .add (data );
202
+ }
203
+ }
204
+
205
+ /**
206
+ * Get the last encountered PathRenderInfo, then clears the internal buffer
207
+ *
208
+ * @return the PathRenderInfo object that was encountered when processing the last path rendering operation
209
+ */
210
+ PathRenderInfo getEncounteredPath () {
211
+ if (content .size () == 0 ) {
212
+ return null ;
213
+ }
214
+
215
+ IEventData eventData = content .get (0 );
216
+ if (!(eventData instanceof PathRenderInfo )) {
217
+ throw new PdfException (pathDataExpected );
218
+ }
219
+ content .clear ();
220
+
221
+ return (PathRenderInfo ) eventData ;
222
+ }
223
+
224
+ public Set <EventType > getSupportedEvents () {
225
+ return null ;
226
+ }
227
+ }
228
+
146
229
private static class NoOpEventListener implements IEventListener {
147
230
@ Override
148
231
public void eventOccurred (IEventData data , EventType type ) {
0 commit comments