|
| 1 | +/* |
| 2 | + This file is part of the iText (R) project. |
| 3 | + Copyright (c) 1998-2019 iText Group NV |
| 4 | + Authors: iText Software. |
| 5 | +
|
| 6 | + This program is free software; you can redistribute it and/or modify |
| 7 | + it under the terms of the GNU Affero General Public License version 3 |
| 8 | + as published by the Free Software Foundation with the addition of the |
| 9 | + following permission added to Section 15 as permitted in Section 7(a): |
| 10 | + FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY |
| 11 | + ITEXT GROUP. ITEXT GROUP DISCLAIMS THE WARRANTY OF NON INFRINGEMENT |
| 12 | + OF THIRD PARTY RIGHTS |
| 13 | +
|
| 14 | + This program is distributed in the hope that it will be useful, but |
| 15 | + WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 16 | + or FITNESS FOR A PARTICULAR PURPOSE. |
| 17 | + See the GNU Affero General Public License for more details. |
| 18 | + You should have received a copy of the GNU Affero General Public License |
| 19 | + along with this program; if not, see http://www.gnu.org/licenses or write to |
| 20 | + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 21 | + Boston, MA, 02110-1301 USA, or download the license from the following URL: |
| 22 | + http://itextpdf.com/terms-of-use/ |
| 23 | +
|
| 24 | + The interactive user interfaces in modified source and object code versions |
| 25 | + of this program must display Appropriate Legal Notices, as required under |
| 26 | + Section 5 of the GNU Affero General Public License. |
| 27 | +
|
| 28 | + In accordance with Section 7(b) of the GNU Affero General Public License, |
| 29 | + a covered work must retain the producer line in every PDF that is created |
| 30 | + or manipulated using iText. |
| 31 | +
|
| 32 | + You can be released from the requirements of the license by purchasing |
| 33 | + a commercial license. Buying such a license is mandatory as soon as you |
| 34 | + develop commercial activities involving the iText software without |
| 35 | + disclosing the source code of your own applications. |
| 36 | + These activities include: offering paid services to customers as an ASP, |
| 37 | + serving PDFs on the fly in a web application, shipping iText with a closed |
| 38 | + source product. |
| 39 | +
|
| 40 | + For more information, please contact iText Software Corp. at this |
| 41 | + |
| 42 | + */ |
| 43 | +package com.itextpdf.layout; |
| 44 | + |
| 45 | +import com.itextpdf.io.image.ImageData; |
| 46 | +import com.itextpdf.io.image.ImageDataFactory; |
| 47 | +import com.itextpdf.kernel.geom.Rectangle; |
| 48 | +import com.itextpdf.kernel.pdf.PdfDocument; |
| 49 | +import com.itextpdf.kernel.pdf.PdfWriter; |
| 50 | +import com.itextpdf.kernel.pdf.canvas.PdfCanvas; |
| 51 | +import com.itextpdf.kernel.pdf.xobject.PdfFormXObject; |
| 52 | +import com.itextpdf.kernel.pdf.xobject.PdfImageXObject; |
| 53 | +import com.itextpdf.kernel.utils.CompareTool; |
| 54 | +import com.itextpdf.layout.element.Div; |
| 55 | +import com.itextpdf.layout.element.Paragraph; |
| 56 | +import com.itextpdf.layout.property.BackgroundImage; |
| 57 | +import com.itextpdf.layout.property.Property; |
| 58 | +import com.itextpdf.test.ExtendedITextTest; |
| 59 | +import com.itextpdf.test.annotations.type.IntegrationTest; |
| 60 | +import org.junit.Assert; |
| 61 | +import org.junit.BeforeClass; |
| 62 | +import org.junit.Test; |
| 63 | +import org.junit.experimental.categories.Category; |
| 64 | + |
| 65 | +import java.io.FileOutputStream; |
| 66 | +import java.io.IOException; |
| 67 | +import java.net.MalformedURLException; |
| 68 | + |
| 69 | +@Category(IntegrationTest.class) |
| 70 | +public class BackgroundImageTest extends ExtendedITextTest { |
| 71 | + |
| 72 | + public static final String sourceFolder = "./src/test/resources/com/itextpdf/layout/BackgroundImageTest/"; |
| 73 | + public static final String destinationFolder = "./target/test/com/itextpdf/layout/BackgroundImageTest/"; |
| 74 | + |
| 75 | + @BeforeClass |
| 76 | + public static void beforeClass() { |
| 77 | + createDestinationFolder(destinationFolder); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void backgroundImage() throws IOException, InterruptedException { |
| 82 | + PdfImageXObject xObject = new PdfImageXObject(ImageDataFactory.create(sourceFolder + "itis.jpg")); |
| 83 | + BackgroundImage backgroundImage = new BackgroundImage(xObject); |
| 84 | + |
| 85 | + Assert.assertTrue(backgroundImage.isRepeatX()); |
| 86 | + Assert.assertTrue(backgroundImage.isRepeatY()); |
| 87 | + |
| 88 | + backgroundImageGenericTest("backgroundImage", backgroundImage); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void backgroundImageWithoutRepeatX() throws IOException, InterruptedException { |
| 93 | + PdfImageXObject xObject = new PdfImageXObject(ImageDataFactory.create(sourceFolder + "itis.jpg")); |
| 94 | + BackgroundImage backgroundImage = new BackgroundImage(xObject, false, true); |
| 95 | + |
| 96 | + Assert.assertFalse(backgroundImage.isRepeatX()); |
| 97 | + Assert.assertTrue(backgroundImage.isRepeatY()); |
| 98 | + |
| 99 | + backgroundImageGenericTest("backgroundImageWithoutRepeatX", backgroundImage); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void backgroundImageWithoutRepeatY() throws IOException, InterruptedException { |
| 104 | + PdfImageXObject xObject = new PdfImageXObject(ImageDataFactory.create(sourceFolder + "itis.jpg")); |
| 105 | + BackgroundImage backgroundImage = new BackgroundImage(xObject, true, false); |
| 106 | + |
| 107 | + Assert.assertTrue(backgroundImage.isRepeatX()); |
| 108 | + Assert.assertFalse(backgroundImage.isRepeatY()); |
| 109 | + |
| 110 | + backgroundImageGenericTest("backgroundImageWithoutRepeatY", backgroundImage); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + public void backgroundImageWithoutRepeatXY() throws IOException, InterruptedException { |
| 115 | + PdfImageXObject xObject = new PdfImageXObject(ImageDataFactory.create(sourceFolder + "itis.jpg")); |
| 116 | + BackgroundImage backgroundImage = new BackgroundImage(xObject, false, false); |
| 117 | + |
| 118 | + Assert.assertFalse(backgroundImage.isRepeatX()); |
| 119 | + Assert.assertFalse(backgroundImage.isRepeatY()); |
| 120 | + |
| 121 | + backgroundImageGenericTest("backgroundImageWithoutRepeatXY", backgroundImage); |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + //TODO DEVSIX-3108 |
| 126 | + public void backgroundXObject() throws IOException, InterruptedException { |
| 127 | + String filename = "backgroundXObject"; |
| 128 | + |
| 129 | + String fileName = filename + ".pdf"; |
| 130 | + String outFileName = destinationFolder + fileName; |
| 131 | + |
| 132 | + try (PdfDocument pdfDocument = new PdfDocument(new PdfWriter(new FileOutputStream(outFileName)))) { |
| 133 | + BackgroundImage backgroundImage = new BackgroundImage(createFormXObject(pdfDocument)); |
| 134 | + |
| 135 | + Assert.assertTrue(backgroundImage.isRepeatX()); |
| 136 | + Assert.assertTrue(backgroundImage.isRepeatY()); |
| 137 | + |
| 138 | + backgroundXObjectGenericTest(filename, backgroundImage, pdfDocument); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + @Test |
| 143 | + //TODO DEVSIX-3108 |
| 144 | + public void backgroundXObjectWithoutRepeatX() throws IOException, InterruptedException { |
| 145 | + String filename = "backgroundXObjectWithoutRepeatX"; |
| 146 | + |
| 147 | + String fileName = filename + ".pdf"; |
| 148 | + String outFileName = destinationFolder + fileName; |
| 149 | + |
| 150 | + try (PdfDocument pdfDocument = new PdfDocument(new PdfWriter(new FileOutputStream(outFileName)))) { |
| 151 | + BackgroundImage backgroundImage = new BackgroundImage(createFormXObject(pdfDocument), false, true); |
| 152 | + |
| 153 | + Assert.assertFalse(backgroundImage.isRepeatX()); |
| 154 | + Assert.assertTrue(backgroundImage.isRepeatY()); |
| 155 | + |
| 156 | + backgroundXObjectGenericTest(filename, backgroundImage, pdfDocument); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + @Test |
| 161 | + //TODO DEVSIX-3108 |
| 162 | + public void backgroundXObjectWithoutRepeatY() throws IOException, InterruptedException { |
| 163 | + String filename = "backgroundXObjectWithoutRepeatY"; |
| 164 | + |
| 165 | + String fileName = filename + ".pdf"; |
| 166 | + String outFileName = destinationFolder + fileName; |
| 167 | + |
| 168 | + try (PdfDocument pdfDocument = new PdfDocument(new PdfWriter(new FileOutputStream(outFileName)))) { |
| 169 | + BackgroundImage backgroundImage = new BackgroundImage(createFormXObject(pdfDocument), true, false); |
| 170 | + |
| 171 | + Assert.assertTrue(backgroundImage.isRepeatX()); |
| 172 | + Assert.assertFalse(backgroundImage.isRepeatY()); |
| 173 | + |
| 174 | + backgroundXObjectGenericTest(filename, backgroundImage, pdfDocument); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + @Test |
| 179 | + //TODO DEVSIX-3108 |
| 180 | + public void backgroundXObjectWithoutRepeatXY() throws IOException, InterruptedException { |
| 181 | + String filename = "backgroundXObjectWithoutRepeatXY"; |
| 182 | + |
| 183 | + String fileName = filename + ".pdf"; |
| 184 | + String outFileName = destinationFolder + fileName; |
| 185 | + |
| 186 | + try (PdfDocument pdfDocument = new PdfDocument(new PdfWriter(new FileOutputStream(outFileName)))) { |
| 187 | + BackgroundImage backgroundImage = new BackgroundImage(createFormXObject(pdfDocument), false, false); |
| 188 | + |
| 189 | + Assert.assertFalse(backgroundImage.isRepeatX()); |
| 190 | + Assert.assertFalse(backgroundImage.isRepeatY()); |
| 191 | + |
| 192 | + backgroundXObjectGenericTest(filename, backgroundImage, pdfDocument); |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + @Test |
| 197 | + //TODO DEVSIX-3108 |
| 198 | + public void backgroundXObjectAndImage() throws IOException, InterruptedException { |
| 199 | + String filename = "backgroundXObjectAndImage"; |
| 200 | + |
| 201 | + String fileName = filename + ".pdf"; |
| 202 | + String outFileName = destinationFolder + fileName; |
| 203 | + String cmpFileName = sourceFolder + "cmp_" + filename + ".pdf"; |
| 204 | + |
| 205 | + try (PdfDocument pdfDocument = new PdfDocument(new PdfWriter(new FileOutputStream(outFileName)))) { |
| 206 | + |
| 207 | + Document doc = new Document(pdfDocument); |
| 208 | + |
| 209 | + String text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, " + |
| 210 | + "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. " + |
| 211 | + "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi " + |
| 212 | + "ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit " + |
| 213 | + "in voluptate velit esse cillum dolore eu fugiat nulla pariatur. " + |
| 214 | + "Excepteur sint occaecat cupidatat non proident, sunt in culpa qui " + |
| 215 | + "officia deserunt mollit anim id est laborum. "; |
| 216 | + |
| 217 | + |
| 218 | + Div div = new Div().add(new Paragraph(text + text + text)); |
| 219 | + PdfImageXObject imageXObject = new PdfImageXObject(ImageDataFactory.create(sourceFolder + "itis.jpg")); |
| 220 | + BackgroundImage backgroundImage = new BackgroundImage(imageXObject); |
| 221 | + |
| 222 | + div.setProperty(Property.BACKGROUND_IMAGE, backgroundImage); |
| 223 | + doc.add(div); |
| 224 | + |
| 225 | + |
| 226 | + BackgroundImage backgroundFormXObject = new BackgroundImage(createFormXObject(pdfDocument)); |
| 227 | + div = new Div().add(new Paragraph(text + text + text)); |
| 228 | + div.setProperty(Property.BACKGROUND_IMAGE, backgroundFormXObject); |
| 229 | + doc.add(div); |
| 230 | + |
| 231 | + pdfDocument.close(); |
| 232 | + |
| 233 | + Assert.assertNull(new CompareTool().compareByContent(outFileName, cmpFileName, destinationFolder, "diff")); |
| 234 | + |
| 235 | + } |
| 236 | + } |
| 237 | + |
| 238 | + private PdfFormXObject createFormXObject(PdfDocument pdfDocument) throws MalformedURLException { |
| 239 | + ImageData image = ImageDataFactory.create(sourceFolder + "itis.jpg"); |
| 240 | + PdfFormXObject template = new PdfFormXObject(new Rectangle(image.getWidth(), image.getHeight())); |
| 241 | + PdfCanvas canvas = new PdfCanvas(template, pdfDocument); |
| 242 | + canvas.addImage(image, 0, 0, image.getWidth(), false).flush(); |
| 243 | + canvas.release(); |
| 244 | + template.flush(); |
| 245 | + |
| 246 | + return template; |
| 247 | + } |
| 248 | + |
| 249 | + |
| 250 | + private void backgroundImageGenericTest(String filename, BackgroundImage backgroundImage) throws IOException, InterruptedException { |
| 251 | + Assert.assertTrue(backgroundImage.isBackgroundSpecified()); |
| 252 | + |
| 253 | + String outFileName = destinationFolder + filename + ".pdf"; |
| 254 | + String cmpFileName = sourceFolder + "cmp_" + filename + ".pdf"; |
| 255 | + |
| 256 | + PdfDocument pdfDocument = new PdfDocument(new PdfWriter(new FileOutputStream(outFileName))); |
| 257 | + Document doc = new Document(pdfDocument); |
| 258 | + |
| 259 | + String text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, " + |
| 260 | + "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. " + |
| 261 | + "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi " + |
| 262 | + "ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit " + |
| 263 | + "in voluptate velit esse cillum dolore eu fugiat nulla pariatur. " + |
| 264 | + "Excepteur sint occaecat cupidatat non proident, sunt in culpa qui " + |
| 265 | + "officia deserunt mollit anim id est laborum. "; |
| 266 | + |
| 267 | + |
| 268 | + Div div = new Div().add(new Paragraph(text + text + text)); |
| 269 | + div.setProperty(Property.BACKGROUND_IMAGE, backgroundImage); |
| 270 | + doc.add(div); |
| 271 | + |
| 272 | + pdfDocument.close(); |
| 273 | + |
| 274 | + Assert.assertNull(new CompareTool().compareByContent(outFileName, cmpFileName, destinationFolder, "diff")); |
| 275 | + } |
| 276 | + |
| 277 | + private void backgroundXObjectGenericTest(String filename, BackgroundImage backgroundImage, PdfDocument pdfDocument) throws IOException, InterruptedException { |
| 278 | + Assert.assertTrue(backgroundImage.isBackgroundSpecified()); |
| 279 | + |
| 280 | + String outFileName = destinationFolder + filename + ".pdf"; |
| 281 | + String cmpFileName = sourceFolder + "cmp_" + filename + ".pdf"; |
| 282 | + Document doc = new Document(pdfDocument); |
| 283 | + |
| 284 | + String text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, " + |
| 285 | + "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. " + |
| 286 | + "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi " + |
| 287 | + "ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit " + |
| 288 | + "in voluptate velit esse cillum dolore eu fugiat nulla pariatur. " + |
| 289 | + "Excepteur sint occaecat cupidatat non proident, sunt in culpa qui " + |
| 290 | + "officia deserunt mollit anim id est laborum. "; |
| 291 | + |
| 292 | + |
| 293 | + Div div = new Div().add(new Paragraph(text + text + text)); |
| 294 | + div.setProperty(Property.BACKGROUND_IMAGE, backgroundImage); |
| 295 | + doc.add(div); |
| 296 | + |
| 297 | + pdfDocument.close(); |
| 298 | + |
| 299 | + Assert.assertNull(new CompareTool().compareByContent(outFileName, cmpFileName, destinationFolder, "diff")); |
| 300 | + } |
| 301 | +} |
| 302 | + |
0 commit comments