Skip to content

Commit 5871c9d

Browse files
author
Eugene Bochilo
committed
Add test to be sure that PdfXFormObject and PdfImageObject work correctly
DEVSIX-1948
1 parent 3ce2c4b commit 5871c9d

File tree

3 files changed

+59
-13
lines changed

3 files changed

+59
-13
lines changed

layout/src/test/java/com/itextpdf/layout/BackgroundImageTest.java

Lines changed: 59 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,10 @@ This file is part of the iText (R) project.
5454
import com.itextpdf.kernel.pdf.PdfArray;
5555
import com.itextpdf.kernel.pdf.PdfDocument;
5656
import com.itextpdf.kernel.pdf.PdfName;
57-
import com.itextpdf.kernel.pdf.PdfString;
5857
import com.itextpdf.kernel.pdf.PdfWriter;
5958
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
6059
import com.itextpdf.kernel.pdf.xobject.PdfFormXObject;
6160
import com.itextpdf.kernel.pdf.xobject.PdfImageXObject;
62-
import com.itextpdf.kernel.pdf.xobject.PdfTransparencyGroup;
6361
import com.itextpdf.kernel.utils.CompareTool;
6462
import com.itextpdf.layout.element.Div;
6563
import com.itextpdf.layout.element.Paragraph;
@@ -68,7 +66,6 @@ This file is part of the iText (R) project.
6866
import com.itextpdf.layout.property.BackgroundPosition;
6967
import com.itextpdf.layout.property.BackgroundRepeat;
7068
import com.itextpdf.layout.property.BlendMode;
71-
import com.itextpdf.layout.property.Property;
7269
import com.itextpdf.layout.property.UnitValue;
7370
import com.itextpdf.test.ExtendedITextTest;
7471
import com.itextpdf.test.LogLevelConstants;
@@ -271,7 +268,7 @@ public void backgroundXObject() throws IOException, InterruptedException {
271268
String outFileName = DESTINATION_FOLDER + fileName;
272269

273270
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();
275272

276273
Assert.assertTrue(backgroundImage.isRepeatX());
277274
Assert.assertTrue(backgroundImage.isRepeatY());
@@ -289,7 +286,7 @@ public void backgroundXObjectWithoutRepeatX() throws IOException, InterruptedExc
289286

290287
try (PdfDocument pdfDocument = new PdfDocument(new PdfWriter(new FileOutputStream(outFileName)))) {
291288
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();
293290

294291
Assert.assertFalse(backgroundImage.isRepeatX());
295292
Assert.assertTrue(backgroundImage.isRepeatY());
@@ -307,7 +304,7 @@ public void backgroundXObjectWithoutRepeatY() throws IOException, InterruptedExc
307304

308305
try (PdfDocument pdfDocument = new PdfDocument(new PdfWriter(new FileOutputStream(outFileName)))) {
309306
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();
311308

312309
Assert.assertTrue(backgroundImage.isRepeatX());
313310
Assert.assertFalse(backgroundImage.isRepeatY());
@@ -325,7 +322,7 @@ public void backgroundXObjectWithoutRepeatXY() throws IOException, InterruptedEx
325322

326323
try (PdfDocument pdfDocument = new PdfDocument(new PdfWriter(new FileOutputStream(outFileName)))) {
327324
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();
329326

330327
Assert.assertFalse(backgroundImage.isRepeatX());
331328
Assert.assertFalse(backgroundImage.isRepeatY());
@@ -359,13 +356,13 @@ public void backgroundXObjectAndImageTest() throws IOException, InterruptedExcep
359356
PdfImageXObject imageXObject = new PdfImageXObject(ImageDataFactory.create(SOURCE_FOLDER + "itis.jpg"));
360357
BackgroundImage backgroundImage = new BackgroundImage.Builder().setImage(imageXObject).build();
361358

362-
div.setProperty(Property.BACKGROUND_IMAGE, backgroundImage);
359+
div.setBackgroundImage(backgroundImage);
363360
doc.add(div);
364361

365362

366-
BackgroundImage backgroundFormXObject = new BackgroundImage.Builder().setImage(createFormXObject(pdfDocument)).build();
363+
BackgroundImage backgroundFormXObject = new BackgroundImage.Builder().setImage(createFormXObject(pdfDocument, "itis.jpg")).build();
367364
div = new Div().add(new Paragraph(text + text + text));
368-
div.setProperty(Property.BACKGROUND_IMAGE, backgroundFormXObject);
365+
div.setBackgroundImage(backgroundFormXObject);
369366
doc.add(div);
370367

371368
pdfDocument.close();
@@ -375,6 +372,51 @@ public void backgroundXObjectAndImageTest() throws IOException, InterruptedExcep
375372
}
376373
}
377374

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+
378420
@Test
379421
public void backgroundImageWithLinearGradientAndNormalBlendModeTest() throws IOException, InterruptedException {
380422
blendModeTest(BlendMode.NORMAL);
@@ -471,8 +513,8 @@ private void blendModeTest(BlendMode blendMode) throws IOException, InterruptedE
471513
+ blendMode.getPdfRepresentation().getValue(), Arrays.asList(topBackgroundImage, backgroundImage));
472514
}
473515

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);
476518
PdfFormXObject template = new PdfFormXObject(new Rectangle(image.getWidth(), image.getHeight()));
477519
PdfCanvas canvas = new PdfCanvas(template, pdfDocument);
478520
canvas.addImage(image, 0, 0, image.getWidth(), false).flush();
@@ -516,7 +558,11 @@ private void backgroundImageGenericTest(String filename, Object backgroundImage,
516558
if (angle != null) {
517559
div.setRotationAngle(angle.doubleValue());
518560
}
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+
}
520566
doc.add(div);
521567

522568
pdfDocument.close();

0 commit comments

Comments
 (0)