@@ -54,12 +54,10 @@ This file is part of the iText (R) project.
54
54
import com .itextpdf .kernel .pdf .PdfArray ;
55
55
import com .itextpdf .kernel .pdf .PdfDocument ;
56
56
import com .itextpdf .kernel .pdf .PdfName ;
57
- import com .itextpdf .kernel .pdf .PdfString ;
58
57
import com .itextpdf .kernel .pdf .PdfWriter ;
59
58
import com .itextpdf .kernel .pdf .canvas .PdfCanvas ;
60
59
import com .itextpdf .kernel .pdf .xobject .PdfFormXObject ;
61
60
import com .itextpdf .kernel .pdf .xobject .PdfImageXObject ;
62
- import com .itextpdf .kernel .pdf .xobject .PdfTransparencyGroup ;
63
61
import com .itextpdf .kernel .utils .CompareTool ;
64
62
import com .itextpdf .layout .element .Div ;
65
63
import com .itextpdf .layout .element .Paragraph ;
@@ -68,7 +66,6 @@ This file is part of the iText (R) project.
68
66
import com .itextpdf .layout .property .BackgroundPosition ;
69
67
import com .itextpdf .layout .property .BackgroundRepeat ;
70
68
import com .itextpdf .layout .property .BlendMode ;
71
- import com .itextpdf .layout .property .Property ;
72
69
import com .itextpdf .layout .property .UnitValue ;
73
70
import com .itextpdf .test .ExtendedITextTest ;
74
71
import com .itextpdf .test .LogLevelConstants ;
@@ -271,7 +268,7 @@ public void backgroundXObject() throws IOException, InterruptedException {
271
268
String outFileName = DESTINATION_FOLDER + fileName ;
272
269
273
270
try (PdfDocument pdfDocument = new PdfDocument (new PdfWriter (new FileOutputStream (outFileName )))) {
274
- BackgroundImage backgroundImage = new BackgroundImage .Builder ().setImage (createFormXObject (pdfDocument )).build ();
271
+ BackgroundImage backgroundImage = new BackgroundImage .Builder ().setImage (createFormXObject (pdfDocument , "itis.jpg" )).build ();
275
272
276
273
Assert .assertTrue (backgroundImage .isRepeatX ());
277
274
Assert .assertTrue (backgroundImage .isRepeatY ());
@@ -289,7 +286,7 @@ public void backgroundXObjectWithoutRepeatX() throws IOException, InterruptedExc
289
286
290
287
try (PdfDocument pdfDocument = new PdfDocument (new PdfWriter (new FileOutputStream (outFileName )))) {
291
288
BackgroundImage backgroundImage = new BackgroundImage .Builder ()
292
- .setImage (createFormXObject (pdfDocument )).setBackgroundRepeat (new BackgroundRepeat (false , true )).build ();
289
+ .setImage (createFormXObject (pdfDocument , "itis.jpg" )).setBackgroundRepeat (new BackgroundRepeat (false , true )).build ();
293
290
294
291
Assert .assertFalse (backgroundImage .isRepeatX ());
295
292
Assert .assertTrue (backgroundImage .isRepeatY ());
@@ -307,7 +304,7 @@ public void backgroundXObjectWithoutRepeatY() throws IOException, InterruptedExc
307
304
308
305
try (PdfDocument pdfDocument = new PdfDocument (new PdfWriter (new FileOutputStream (outFileName )))) {
309
306
BackgroundImage backgroundImage = new BackgroundImage
310
- .Builder ().setImage (createFormXObject (pdfDocument )).setBackgroundRepeat (new BackgroundRepeat (true , false )).build ();
307
+ .Builder ().setImage (createFormXObject (pdfDocument , "itis.jpg" )).setBackgroundRepeat (new BackgroundRepeat (true , false )).build ();
311
308
312
309
Assert .assertTrue (backgroundImage .isRepeatX ());
313
310
Assert .assertFalse (backgroundImage .isRepeatY ());
@@ -325,7 +322,7 @@ public void backgroundXObjectWithoutRepeatXY() throws IOException, InterruptedEx
325
322
326
323
try (PdfDocument pdfDocument = new PdfDocument (new PdfWriter (new FileOutputStream (outFileName )))) {
327
324
BackgroundImage backgroundImage = new BackgroundImage
328
- .Builder ().setImage (createFormXObject (pdfDocument )).setBackgroundRepeat (new BackgroundRepeat (false , false )).build ();
325
+ .Builder ().setImage (createFormXObject (pdfDocument , "itis.jpg" )).setBackgroundRepeat (new BackgroundRepeat (false , false )).build ();
329
326
330
327
Assert .assertFalse (backgroundImage .isRepeatX ());
331
328
Assert .assertFalse (backgroundImage .isRepeatY ());
@@ -359,13 +356,13 @@ public void backgroundXObjectAndImageTest() throws IOException, InterruptedExcep
359
356
PdfImageXObject imageXObject = new PdfImageXObject (ImageDataFactory .create (SOURCE_FOLDER + "itis.jpg" ));
360
357
BackgroundImage backgroundImage = new BackgroundImage .Builder ().setImage (imageXObject ).build ();
361
358
362
- div .setProperty ( Property . BACKGROUND_IMAGE , backgroundImage );
359
+ div .setBackgroundImage ( backgroundImage );
363
360
doc .add (div );
364
361
365
362
366
- BackgroundImage backgroundFormXObject = new BackgroundImage .Builder ().setImage (createFormXObject (pdfDocument )).build ();
363
+ BackgroundImage backgroundFormXObject = new BackgroundImage .Builder ().setImage (createFormXObject (pdfDocument , "itis.jpg" )).build ();
367
364
div = new Div ().add (new Paragraph (text + text + text ));
368
- div .setProperty ( Property . BACKGROUND_IMAGE , backgroundFormXObject );
365
+ div .setBackgroundImage ( backgroundFormXObject );
369
366
doc .add (div );
370
367
371
368
pdfDocument .close ();
@@ -375,6 +372,51 @@ public void backgroundXObjectAndImageTest() throws IOException, InterruptedExcep
375
372
}
376
373
}
377
374
375
+ @ Test
376
+ // There shall be rock texture picture at the left top corner with 30pt width and 60pt height
377
+ public void backgroundXFormObjectWithBboxTest () throws IOException , InterruptedException {
378
+ String filename = "backgroundComplicatedXFormObjectTest" ;
379
+
380
+ String fileName = filename + ".pdf" ;
381
+ String outFileName = DESTINATION_FOLDER + fileName ;
382
+ String cmpFileName = SOURCE_FOLDER + "cmp_" + filename + ".pdf" ;
383
+
384
+ try (PdfDocument pdfDocument = new PdfDocument (new PdfWriter (new FileOutputStream (outFileName )))) {
385
+
386
+ Document doc = new Document (pdfDocument );
387
+
388
+ String text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, " +
389
+ "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. " +
390
+ "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi " +
391
+ "ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit " +
392
+ "in voluptate velit esse cillum dolore eu fugiat nulla pariatur. " +
393
+ "Excepteur sint occaecat cupidatat non proident, sunt in culpa qui " +
394
+ "officia deserunt mollit anim id est laborum. " ;
395
+
396
+ BackgroundImage backgroundFormXObject = new BackgroundImage .Builder ().setBackgroundRepeat (new BackgroundRepeat (false , false ))
397
+ .setImage (createFormXObject (pdfDocument , "rock_texture.jpg" )
398
+ .setBBox (new PdfArray (new Rectangle (70 , -15 , 50 , 75 )))).build ();
399
+ Div div = new Div ().add (new Paragraph (text + text + text ));
400
+ div .setBackgroundImage (backgroundFormXObject );
401
+ doc .add (div );
402
+
403
+ pdfDocument .close ();
404
+
405
+ Assert .assertNull (new CompareTool ().compareByContent (outFileName , cmpFileName , DESTINATION_FOLDER , "diff" ));
406
+
407
+ }
408
+ }
409
+
410
+ @ Test
411
+ // There shall be default rock texture picture with 100pt width and height at the left top corner. BBox shall not do any differences.
412
+ public void backgroundImageWithBboxTest () throws IOException , InterruptedException {
413
+ PdfImageXObject xObject = new PdfImageXObject (ImageDataFactory .create (SOURCE_FOLDER + "rock_texture.jpg" ))
414
+ .put (PdfName .BBox , new PdfArray (new Rectangle (70 , -15 , 500 , 750 )));
415
+ BackgroundImage image = new BackgroundImage .Builder ().setImage (xObject ).setBackgroundRepeat (new BackgroundRepeat (false , false )).build ();
416
+
417
+ backgroundImageGenericTest ("backgroundImageWithBbox" , image );
418
+ }
419
+
378
420
@ Test
379
421
public void backgroundImageWithLinearGradientAndNormalBlendModeTest () throws IOException , InterruptedException {
380
422
blendModeTest (BlendMode .NORMAL );
@@ -471,8 +513,8 @@ private void blendModeTest(BlendMode blendMode) throws IOException, InterruptedE
471
513
+ blendMode .getPdfRepresentation ().getValue (), Arrays .asList (topBackgroundImage , backgroundImage ));
472
514
}
473
515
474
- private PdfFormXObject createFormXObject (PdfDocument pdfDocument ) throws MalformedURLException {
475
- ImageData image = ImageDataFactory .create (SOURCE_FOLDER + "itis.jpg" );
516
+ private PdfFormXObject createFormXObject (PdfDocument pdfDocument , String pictureName ) throws MalformedURLException {
517
+ ImageData image = ImageDataFactory .create (SOURCE_FOLDER + pictureName );
476
518
PdfFormXObject template = new PdfFormXObject (new Rectangle (image .getWidth (), image .getHeight ()));
477
519
PdfCanvas canvas = new PdfCanvas (template , pdfDocument );
478
520
canvas .addImage (image , 0 , 0 , image .getWidth (), false ).flush ();
@@ -516,7 +558,11 @@ private void backgroundImageGenericTest(String filename, Object backgroundImage,
516
558
if (angle != null ) {
517
559
div .setRotationAngle (angle .doubleValue ());
518
560
}
519
- div .setProperty (Property .BACKGROUND_IMAGE , backgroundImage );
561
+ if (backgroundImage instanceof BackgroundImage ) {
562
+ div .setBackgroundImage ((BackgroundImage ) backgroundImage );
563
+ } else {
564
+ div .setBackgroundImage ((List <BackgroundImage >) backgroundImage );
565
+ }
520
566
doc .add (div );
521
567
522
568
pdfDocument .close ();
0 commit comments