@@ -44,22 +44,32 @@ This file is part of the iText (R) project.
44
44
45
45
import com .itextpdf .kernel .colors .ColorConstants ;
46
46
import com .itextpdf .kernel .colors .DeviceRgb ;
47
+ import com .itextpdf .kernel .geom .Rectangle ;
47
48
import com .itextpdf .kernel .pdf .PdfDocument ;
48
49
import com .itextpdf .kernel .pdf .PdfWriter ;
49
50
import com .itextpdf .kernel .pdf .canvas .PdfCanvas ;
50
51
import com .itextpdf .kernel .utils .CompareTool ;
51
52
import com .itextpdf .layout .element .Div ;
52
53
import com .itextpdf .layout .element .Paragraph ;
53
54
import com .itextpdf .layout .element .Text ;
55
+ import com .itextpdf .layout .layout .LayoutArea ;
56
+ import com .itextpdf .layout .layout .LayoutContext ;
57
+ import com .itextpdf .layout .layout .LayoutResult ;
54
58
import com .itextpdf .layout .property .Property ;
59
+ import com .itextpdf .layout .renderer .DivRenderer ;
60
+ import com .itextpdf .layout .renderer .IRenderer ;
55
61
import com .itextpdf .test .ExtendedITextTest ;
56
62
import com .itextpdf .test .annotations .type .IntegrationTest ;
57
63
import org .junit .Assert ;
58
64
import org .junit .BeforeClass ;
65
+ import org .junit .Rule ;
59
66
import org .junit .Test ;
60
67
import org .junit .experimental .categories .Category ;
68
+ import org .junit .rules .ExpectedException ;
61
69
62
70
import java .io .IOException ;
71
+ import java .util .ArrayList ;
72
+ import java .util .List ;
63
73
64
74
@ Category (IntegrationTest .class )
65
75
public class CollapsingMarginsTest extends ExtendedITextTest {
@@ -71,6 +81,9 @@ public static void beforeClass() {
71
81
createOrClearDestinationFolder (destinationFolder );
72
82
}
73
83
84
+ @ Rule
85
+ public ExpectedException junitExpectedException = ExpectedException .none ();
86
+
74
87
@ Test
75
88
public void collapsingMarginsTest01 () throws IOException , InterruptedException {
76
89
String outFileName = destinationFolder + "collapsingMarginsTest01.pdf" ;
@@ -104,14 +117,14 @@ public void collapsingMarginsTest01() throws IOException, InterruptedException {
104
117
Div div1 = new Div ();
105
118
Div div2 = new Div ();
106
119
107
- div1 .add (p ).setBackgroundColor (new DeviceRgb (65 ,151 ,29 ));
108
- div2 .add (p ).setBackgroundColor (new DeviceRgb (209 ,247 ,29 ));
120
+ div1 .add (p ).setBackgroundColor (new DeviceRgb (65 , 151 , 29 ));
121
+ div2 .add (p ).setBackgroundColor (new DeviceRgb (209 , 247 , 29 ));
109
122
110
123
div1 .setMarginBottom (20 );
111
124
div2 .setMarginTop (150 );
112
125
div2 .setMarginBottom (150 );
113
126
114
- Div div = new Div ().setMarginTop (20 ).setMarginBottom (10 ).setBackgroundColor (new DeviceRgb (78 ,151 ,205 ));
127
+ Div div = new Div ().setMarginTop (20 ).setMarginBottom (10 ).setBackgroundColor (new DeviceRgb (78 , 151 , 205 ));
115
128
div .add (div1 );
116
129
div .add (div2 );
117
130
doc .add (div );
@@ -160,14 +173,14 @@ public void collapsingMarginsTest02() throws IOException, InterruptedException {
160
173
Div div1 = new Div ();
161
174
Div div2 = new Div ();
162
175
163
- div1 .add (p ).setBackgroundColor (new DeviceRgb (65 ,151 ,29 ));
164
- div2 .add (p ).setBackgroundColor (new DeviceRgb (209 ,247 ,29 ));
176
+ div1 .add (p ).setBackgroundColor (new DeviceRgb (65 , 151 , 29 ));
177
+ div2 .add (p ).setBackgroundColor (new DeviceRgb (209 , 247 , 29 ));
165
178
166
179
div1 .setMarginBottom (40 );
167
180
div2 .setMarginTop (20 );
168
181
div2 .setMarginBottom (150 );
169
182
170
- Div div = new Div ().setMarginTop (20 ).setMarginBottom (10 ).setBackgroundColor (new DeviceRgb (78 ,151 ,205 ));
183
+ Div div = new Div ().setMarginTop (20 ).setMarginBottom (10 ).setBackgroundColor (new DeviceRgb (78 , 151 , 205 ));
171
184
div .add (div1 );
172
185
div .add (div2 );
173
186
doc .add (div );
@@ -215,8 +228,8 @@ public void collapsingMarginsTest03() throws IOException, InterruptedException {
215
228
Div div1 = new Div ();
216
229
Div div2 = new Div ();
217
230
218
- div1 .add (p ).setBackgroundColor (new DeviceRgb (65 ,151 ,29 ));
219
- div2 .add (p ).setBackgroundColor (new DeviceRgb (209 ,247 ,29 ));
231
+ div1 .add (p ).setBackgroundColor (new DeviceRgb (65 , 151 , 29 ));
232
+ div2 .add (p ).setBackgroundColor (new DeviceRgb (209 , 247 , 29 ));
220
233
221
234
div1 .setMarginBottom (80 );
222
235
div2 .setMarginTop (80 );
@@ -267,14 +280,14 @@ public void collapsingMarginsTest04() throws IOException, InterruptedException {
267
280
p .add (new Text ("small text" ).setFontSize (5.1f ));
268
281
p .add (
269
282
"\n And is always as nobly requited;\n " +
270
- "Then battle for Freedom wherever you can,\n " +
271
- "And, if not shot or hanged, you'll get knighted." );
283
+ "Then battle for Freedom wherever you can,\n " +
284
+ "And, if not shot or hanged, you'll get knighted." );
272
285
273
286
Div div1 = new Div ();
274
287
Div div2 = new Div ();
275
288
276
- div1 .add (p ).setBackgroundColor (new DeviceRgb (65 ,151 ,29 ));
277
- div2 .add (p ).setBackgroundColor (new DeviceRgb (209 ,247 ,29 ));
289
+ div1 .add (p ).setBackgroundColor (new DeviceRgb (65 , 151 , 29 ));
290
+ div2 .add (p ).setBackgroundColor (new DeviceRgb (209 , 247 , 29 ));
278
291
279
292
div1 .setMarginBottom (80 );
280
293
div2 .setMarginTop (80 );
@@ -320,7 +333,7 @@ public void collapsingMarginsTest05() throws IOException, InterruptedException {
320
333
p .setMarginTop (80 );
321
334
Div div = new Div ();
322
335
323
- div .add (p ).setBackgroundColor (new DeviceRgb (65 ,151 ,29 ));
336
+ div .add (p ).setBackgroundColor (new DeviceRgb (65 , 151 , 29 ));
324
337
325
338
doc .add (div );
326
339
@@ -342,4 +355,71 @@ private void drawPageBorders(PdfDocument pdfDocument, int pageNum) {
342
355
canvas .restoreState ();
343
356
}
344
357
}
358
+
359
+ @ Test
360
+ // TODO DEVSIX-2901 the exception should not be thrown
361
+ public void columnRendererTest () throws IOException , InterruptedException {
362
+ junitExpectedException .expect (ArrayIndexOutOfBoundsException .class );
363
+ String outFileName = destinationFolder + "columnRendererTest.pdf" ;
364
+ String cmpFileName = sourceFolder + "cmp_columnRendererTest.pdf" ;
365
+ PdfDocument pdfDocument = new PdfDocument (new PdfWriter (outFileName ));
366
+
367
+ String textByron =
368
+ "When a man hath no freedom to fight for at home,\n " +
369
+ " Let him combat for that of his neighbours;\n " +
370
+ "Let him think of the glories of Greece and of Rome,\n " +
371
+ " And get knocked on the head for his labours.\n " +
372
+ "\n " +
373
+ "To do good to Mankind is the chivalrous plan,\n " +
374
+ " And is always as nobly requited;\n " +
375
+ "Then battle for Freedom wherever you can,\n " +
376
+ " And, if not shot or hanged, you'll get knighted." ;
377
+
378
+ Document doc = new Document (pdfDocument );
379
+ doc .setProperty (Property .COLLAPSING_MARGINS , true );
380
+
381
+
382
+ Paragraph p = new Paragraph ();
383
+ for (int i = 0 ; i < 10 ; i ++) {
384
+ p .add (textByron );
385
+ }
386
+
387
+ Div div = new Div ().add (p );
388
+ List <Rectangle > areas = new ArrayList <>();
389
+ areas .add (new Rectangle (30 , 30 , 150 , 600 ));
390
+ areas .add (new Rectangle (200 , 30 , 150 , 600 ));
391
+ areas .add (new Rectangle (370 , 30 , 150 , 600 ));
392
+ div .setNextRenderer (new CustomColumnDocumentRenderer (div , areas ));
393
+
394
+ doc .add (div );
395
+
396
+ doc .close ();
397
+
398
+ Assert .assertNull (new CompareTool ().compareByContent (outFileName , cmpFileName , destinationFolder , "diff" ));
399
+ }
400
+
401
+ private static class CustomColumnDocumentRenderer extends DivRenderer {
402
+ private List <Rectangle > areas ;
403
+
404
+ public CustomColumnDocumentRenderer (Div modelElement , List <Rectangle > areas ) {
405
+ super (modelElement );
406
+ this .areas = areas ;
407
+ }
408
+
409
+ @ Override
410
+ public LayoutResult layout (LayoutContext layoutContext ) {
411
+ LayoutResult result = super .layout (layoutContext );
412
+ return result ;
413
+ }
414
+
415
+ @ Override
416
+ public List <Rectangle > initElementAreas (LayoutArea area ) {
417
+ return areas ;
418
+ }
419
+
420
+ @ Override
421
+ public IRenderer getNextRenderer () {
422
+ return new CustomColumnDocumentRenderer ((Div ) modelElement , areas );
423
+ }
424
+ }
345
425
}
0 commit comments