Skip to content

Commit a6abdef

Browse files
Eugene BochiloiText-CI
authored andcommitted
Add test to be sure that PdfXFormObject and PdfImageObject work correctly
DEVSIX-1948 Autoported commit. Original commit hash: [5871c9d9f]
1 parent a04e575 commit a6abdef

File tree

4 files changed

+52
-11
lines changed

4 files changed

+52
-11
lines changed

itext.tests/itext.layout.tests/itext/layout/BackgroundImageTest.cs

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ public virtual void BackgroundXObject() {
228228
using (PdfDocument pdfDocument = new PdfDocument(new PdfWriter(new FileStream(outFileName, FileMode.Create
229229
)))) {
230230
iText.Layout.Properties.BackgroundImage backgroundImage = new BackgroundImage.Builder().SetImage(CreateFormXObject
231-
(pdfDocument)).Build();
231+
(pdfDocument, "itis.jpg")).Build();
232232
NUnit.Framework.Assert.IsTrue(backgroundImage.IsRepeatX());
233233
NUnit.Framework.Assert.IsTrue(backgroundImage.IsRepeatY());
234234
BackgroundXObjectGenericTest(filename, backgroundImage, pdfDocument);
@@ -243,7 +243,7 @@ public virtual void BackgroundXObjectWithoutRepeatX() {
243243
using (PdfDocument pdfDocument = new PdfDocument(new PdfWriter(new FileStream(outFileName, FileMode.Create
244244
)))) {
245245
iText.Layout.Properties.BackgroundImage backgroundImage = new BackgroundImage.Builder().SetImage(CreateFormXObject
246-
(pdfDocument)).SetBackgroundRepeat(new BackgroundRepeat(false, true)).Build();
246+
(pdfDocument, "itis.jpg")).SetBackgroundRepeat(new BackgroundRepeat(false, true)).Build();
247247
NUnit.Framework.Assert.IsFalse(backgroundImage.IsRepeatX());
248248
NUnit.Framework.Assert.IsTrue(backgroundImage.IsRepeatY());
249249
BackgroundXObjectGenericTest(filename, backgroundImage, pdfDocument);
@@ -258,7 +258,7 @@ public virtual void BackgroundXObjectWithoutRepeatY() {
258258
using (PdfDocument pdfDocument = new PdfDocument(new PdfWriter(new FileStream(outFileName, FileMode.Create
259259
)))) {
260260
iText.Layout.Properties.BackgroundImage backgroundImage = new BackgroundImage.Builder().SetImage(CreateFormXObject
261-
(pdfDocument)).SetBackgroundRepeat(new BackgroundRepeat(true, false)).Build();
261+
(pdfDocument, "itis.jpg")).SetBackgroundRepeat(new BackgroundRepeat(true, false)).Build();
262262
NUnit.Framework.Assert.IsTrue(backgroundImage.IsRepeatX());
263263
NUnit.Framework.Assert.IsFalse(backgroundImage.IsRepeatY());
264264
BackgroundXObjectGenericTest(filename, backgroundImage, pdfDocument);
@@ -273,7 +273,7 @@ public virtual void BackgroundXObjectWithoutRepeatXY() {
273273
using (PdfDocument pdfDocument = new PdfDocument(new PdfWriter(new FileStream(outFileName, FileMode.Create
274274
)))) {
275275
iText.Layout.Properties.BackgroundImage backgroundImage = new BackgroundImage.Builder().SetImage(CreateFormXObject
276-
(pdfDocument)).SetBackgroundRepeat(new BackgroundRepeat(false, false)).Build();
276+
(pdfDocument, "itis.jpg")).SetBackgroundRepeat(new BackgroundRepeat(false, false)).Build();
277277
NUnit.Framework.Assert.IsFalse(backgroundImage.IsRepeatX());
278278
NUnit.Framework.Assert.IsFalse(backgroundImage.IsRepeatY());
279279
BackgroundXObjectGenericTest(filename, backgroundImage, pdfDocument);
@@ -297,19 +297,55 @@ public virtual void BackgroundXObjectAndImageTest() {
297297
PdfImageXObject imageXObject = new PdfImageXObject(ImageDataFactory.Create(SOURCE_FOLDER + "itis.jpg"));
298298
iText.Layout.Properties.BackgroundImage backgroundImage = new BackgroundImage.Builder().SetImage(imageXObject
299299
).Build();
300-
div.SetProperty(Property.BACKGROUND_IMAGE, backgroundImage);
300+
div.SetBackgroundImage(backgroundImage);
301301
doc.Add(div);
302302
iText.Layout.Properties.BackgroundImage backgroundFormXObject = new BackgroundImage.Builder().SetImage(CreateFormXObject
303-
(pdfDocument)).Build();
303+
(pdfDocument, "itis.jpg")).Build();
304304
div = new Div().Add(new Paragraph(text + text + text));
305-
div.SetProperty(Property.BACKGROUND_IMAGE, backgroundFormXObject);
305+
div.SetBackgroundImage(backgroundFormXObject);
306306
doc.Add(div);
307307
pdfDocument.Close();
308308
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, DESTINATION_FOLDER
309309
, "diff"));
310310
}
311311
}
312312

313+
[NUnit.Framework.Test]
314+
public virtual void BackgroundXFormObjectWithBboxTest() {
315+
// There shall be rock texture picture at the left top corner with 30pt width and 60pt height
316+
String filename = "backgroundComplicatedXFormObjectTest";
317+
String fileName = filename + ".pdf";
318+
String outFileName = DESTINATION_FOLDER + fileName;
319+
String cmpFileName = SOURCE_FOLDER + "cmp_" + filename + ".pdf";
320+
using (PdfDocument pdfDocument = new PdfDocument(new PdfWriter(new FileStream(outFileName, FileMode.Create
321+
)))) {
322+
Document doc = new Document(pdfDocument);
323+
String text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, " + "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. "
324+
+ "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi " + "ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit "
325+
+ "in voluptate velit esse cillum dolore eu fugiat nulla pariatur. " + "Excepteur sint occaecat cupidatat non proident, sunt in culpa qui "
326+
+ "officia deserunt mollit anim id est laborum. ";
327+
iText.Layout.Properties.BackgroundImage backgroundFormXObject = new BackgroundImage.Builder().SetBackgroundRepeat
328+
(new BackgroundRepeat(false, false)).SetImage(CreateFormXObject(pdfDocument, "rock_texture.jpg").SetBBox
329+
(new PdfArray(new Rectangle(70, -15, 50, 75)))).Build();
330+
Div div = new Div().Add(new Paragraph(text + text + text));
331+
div.SetBackgroundImage(backgroundFormXObject);
332+
doc.Add(div);
333+
pdfDocument.Close();
334+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, DESTINATION_FOLDER
335+
, "diff"));
336+
}
337+
}
338+
339+
[NUnit.Framework.Test]
340+
public virtual void BackgroundImageWithBboxTest() {
341+
// There shall be default rock texture picture with 100pt width and height at the left top corner. BBox shall not do any differences.
342+
PdfImageXObject xObject = new PdfImageXObject(ImageDataFactory.Create(SOURCE_FOLDER + "rock_texture.jpg"))
343+
.Put(PdfName.BBox, new PdfArray(new Rectangle(70, -15, 500, 750)));
344+
iText.Layout.Properties.BackgroundImage image = new BackgroundImage.Builder().SetImage(xObject).SetBackgroundRepeat
345+
(new BackgroundRepeat(false, false)).Build();
346+
BackgroundImageGenericTest("backgroundImageWithBbox", image);
347+
}
348+
313349
[NUnit.Framework.Test]
314350
public virtual void BackgroundImageWithLinearGradientAndNormalBlendModeTest() {
315351
BlendModeTest(BlendMode.NORMAL);
@@ -406,8 +442,8 @@ private void BlendModeTest(BlendMode blendMode) {
406442
().GetValue(), JavaUtil.ArraysAsList(topBackgroundImage, backgroundImage));
407443
}
408444

409-
private PdfFormXObject CreateFormXObject(PdfDocument pdfDocument) {
410-
ImageData image = ImageDataFactory.Create(SOURCE_FOLDER + "itis.jpg");
445+
private PdfFormXObject CreateFormXObject(PdfDocument pdfDocument, String pictureName) {
446+
ImageData image = ImageDataFactory.Create(SOURCE_FOLDER + pictureName);
411447
PdfFormXObject template = new PdfFormXObject(new Rectangle(image.GetWidth(), image.GetHeight()));
412448
PdfCanvas canvas = new PdfCanvas(template, pdfDocument);
413449
canvas.AddImage(image, 0, 0, image.GetWidth(), false).Flush();
@@ -443,7 +479,12 @@ private void BackgroundImageGenericTest(String filename, Object backgroundImage,
443479
if (angle != null) {
444480
div.SetRotationAngle(angle.Value);
445481
}
446-
div.SetProperty(Property.BACKGROUND_IMAGE, backgroundImage);
482+
if (backgroundImage is iText.Layout.Properties.BackgroundImage) {
483+
div.SetBackgroundImage((iText.Layout.Properties.BackgroundImage)backgroundImage);
484+
}
485+
else {
486+
div.SetBackgroundImage((IList<iText.Layout.Properties.BackgroundImage>)backgroundImage);
487+
}
447488
doc.Add(div);
448489
pdfDocument.Close();
449490
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, DESTINATION_FOLDER

port-hash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3ce2c4b328665253b6ecdac0797b6f5764a023bd
1+
5871c9d9fda8779ddfa5a95a2165ea5f431b00b5

0 commit comments

Comments
 (0)