|
| 1 | +package com.itextpdf.layout.element.gridcontainer; |
| 2 | + |
| 3 | +import com.itextpdf.io.image.ImageDataFactory; |
| 4 | +import com.itextpdf.kernel.colors.ColorConstants; |
| 5 | +import com.itextpdf.kernel.geom.Rectangle; |
| 6 | +import com.itextpdf.kernel.pdf.PdfArray; |
| 7 | +import com.itextpdf.kernel.pdf.PdfDocument; |
| 8 | +import com.itextpdf.kernel.pdf.PdfName; |
| 9 | +import com.itextpdf.kernel.pdf.PdfWriter; |
| 10 | +import com.itextpdf.kernel.pdf.xobject.PdfImageXObject; |
| 11 | +import com.itextpdf.kernel.utils.CompareTool; |
| 12 | +import com.itextpdf.layout.Document; |
| 13 | +import com.itextpdf.layout.borders.SolidBorder; |
| 14 | +import com.itextpdf.layout.element.Div; |
| 15 | +import com.itextpdf.layout.element.GridContainer; |
| 16 | +import com.itextpdf.layout.element.Paragraph; |
| 17 | +import com.itextpdf.layout.element.Text; |
| 18 | +import com.itextpdf.layout.properties.BackgroundImage; |
| 19 | +import com.itextpdf.layout.properties.BackgroundRepeat; |
| 20 | +import com.itextpdf.layout.properties.BackgroundRepeat.BackgroundRepeatValue; |
| 21 | +import com.itextpdf.layout.properties.BoxSizingPropertyValue; |
| 22 | +import com.itextpdf.layout.properties.GridValue; |
| 23 | +import com.itextpdf.layout.properties.Property; |
| 24 | +import com.itextpdf.layout.properties.UnitValue; |
| 25 | +import com.itextpdf.test.ExtendedITextTest; |
| 26 | +import com.itextpdf.test.annotations.type.IntegrationTest; |
| 27 | + |
| 28 | +import java.io.IOException; |
| 29 | +import java.util.Arrays; |
| 30 | +import org.junit.Assert; |
| 31 | +import org.junit.Before; |
| 32 | +import org.junit.Test; |
| 33 | +import org.junit.experimental.categories.Category; |
| 34 | + |
| 35 | +@Category(IntegrationTest.class) |
| 36 | +public class GridContainerLayoutTest extends ExtendedITextTest { |
| 37 | + |
| 38 | + public static final String DESTINATION_FOLDER = "./target/test/com/itextpdf/layout/GridContainerTest/"; |
| 39 | + |
| 40 | + public static final String SOURCE_FOLDER = "./src/test/resources/com/itextpdf/layout/GridContainerTest/"; |
| 41 | + |
| 42 | + @Before |
| 43 | + public void setup() { |
| 44 | + createDestinationFolder(DESTINATION_FOLDER); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void simpleBorderBoxSizingTestTest() throws IOException, InterruptedException { |
| 49 | + String fileName = DESTINATION_FOLDER + "border.pdf"; |
| 50 | + PdfDocument pdfDocument = new PdfDocument(new PdfWriter(fileName)); |
| 51 | + Document document = new Document(pdfDocument); |
| 52 | + |
| 53 | + GridContainer gridcontainer0 = createGridBoxWithText(); |
| 54 | + document.add(new Paragraph("BOX_SIZING: BORDER_BOX")); |
| 55 | + gridcontainer0.setProperty(Property.BOX_SIZING, BoxSizingPropertyValue.BORDER_BOX); |
| 56 | + gridcontainer0.setBorder(new SolidBorder(ColorConstants.BLACK, 20)); |
| 57 | + document.add(gridcontainer0); |
| 58 | + |
| 59 | + document.add(new Paragraph("BOX_SIZING: CONTENT_BOX")); |
| 60 | + GridContainer gridcontainer1 = createGridBoxWithText(); |
| 61 | + gridcontainer1.setProperty(Property.BOX_SIZING, BoxSizingPropertyValue.CONTENT_BOX); |
| 62 | + gridcontainer1.setBorder(new SolidBorder(ColorConstants.BLACK, 20)); |
| 63 | + |
| 64 | + document.add(gridcontainer1); |
| 65 | + |
| 66 | + document.close(); |
| 67 | + Assert.assertNull(new CompareTool().compareByContent(fileName, SOURCE_FOLDER + "cmp_border.pdf", |
| 68 | + DESTINATION_FOLDER, "diff")); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void simpleMarginTest() throws IOException, InterruptedException { |
| 73 | + String fileName = DESTINATION_FOLDER + "margin.pdf"; |
| 74 | + PdfDocument pdfDocument = new PdfDocument(new PdfWriter(fileName)); |
| 75 | + Document document = new Document(pdfDocument); |
| 76 | + document.add(new Paragraph("Margin ")); |
| 77 | + GridContainer gridcontainer0 = createGridBoxWithText(); |
| 78 | + gridcontainer0.setMarginTop(50); |
| 79 | + gridcontainer0.setMarginBottom(100); |
| 80 | + gridcontainer0.setMarginLeft(10); |
| 81 | + gridcontainer0.setMarginRight(10); |
| 82 | + document.add(gridcontainer0); |
| 83 | + document.add(new Paragraph("Margin ")); |
| 84 | + |
| 85 | + document.close(); |
| 86 | + |
| 87 | + Assert.assertNull(new CompareTool().compareByContent(fileName, SOURCE_FOLDER + "cmp_margin.pdf", |
| 88 | + DESTINATION_FOLDER, "diff")); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void simplePaddingTest() throws IOException, InterruptedException { |
| 93 | + String fileName = DESTINATION_FOLDER + "padding.pdf"; |
| 94 | + PdfDocument pdfDocument = new PdfDocument(new PdfWriter(fileName)); |
| 95 | + Document document = new Document(pdfDocument); |
| 96 | + |
| 97 | + document.add(new Paragraph("Padding ")); |
| 98 | + GridContainer gridcontainer0 = createGridBoxWithText(); |
| 99 | + gridcontainer0.setPaddingTop(50); |
| 100 | + gridcontainer0.setPaddingBottom(100); |
| 101 | + gridcontainer0.setPaddingLeft(10); |
| 102 | + gridcontainer0.setPaddingRight(10); |
| 103 | + document.add(gridcontainer0); |
| 104 | + document.add(new Paragraph("Padding ")); |
| 105 | + |
| 106 | + document.close(); |
| 107 | + |
| 108 | + Assert.assertNull(new CompareTool().compareByContent(fileName, SOURCE_FOLDER + "cmp_padding.pdf", |
| 109 | + DESTINATION_FOLDER, "diff")); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + public void simpleBackGroundTest() throws IOException, InterruptedException { |
| 114 | + String fileName = DESTINATION_FOLDER + "background.pdf"; |
| 115 | + PdfDocument pdfDocument = new PdfDocument(new PdfWriter(fileName)); |
| 116 | + Document document = new Document(pdfDocument); |
| 117 | + |
| 118 | + document.add(new Paragraph("Background ")); |
| 119 | + GridContainer gridcontainer0 = createGridBoxWithText(); |
| 120 | + gridcontainer0.setBackgroundColor(ColorConstants.RED); |
| 121 | + document.add(gridcontainer0); |
| 122 | + document.add(new Paragraph("Background ")); |
| 123 | + |
| 124 | + document.close(); |
| 125 | + |
| 126 | + Assert.assertNull(new CompareTool().compareByContent(fileName, SOURCE_FOLDER + "cmp_background.pdf", |
| 127 | + DESTINATION_FOLDER, "diff")); |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + public void backgroundWithImageTest() throws IOException, InterruptedException { |
| 132 | + String fileName = DESTINATION_FOLDER + "backgroundWithImage.pdf"; |
| 133 | + PdfDocument pdfDocument = new PdfDocument(new PdfWriter(fileName)); |
| 134 | + Document document = new Document(pdfDocument); |
| 135 | + |
| 136 | + document.add(new Paragraph("Background with image ")); |
| 137 | + GridContainer gridcontainer0 = createGridBoxWithText(); |
| 138 | + |
| 139 | + PdfImageXObject xObject = new PdfImageXObject(ImageDataFactory.create(SOURCE_FOLDER + "rock_texture.jpg")) |
| 140 | + .put(PdfName.BBox, new PdfArray(new Rectangle(70, -15, 500, 750))); |
| 141 | + BackgroundImage image = new BackgroundImage.Builder().setImage(xObject) |
| 142 | + .setBackgroundRepeat(new BackgroundRepeat( |
| 143 | + BackgroundRepeatValue.REPEAT)).build(); |
| 144 | + |
| 145 | + gridcontainer0.setBackgroundImage(image); |
| 146 | + document.add(gridcontainer0); |
| 147 | + document.add(new Paragraph("Background with image ")); |
| 148 | + |
| 149 | + document.close(); |
| 150 | + |
| 151 | + Assert.assertNull(new CompareTool().compareByContent(fileName, SOURCE_FOLDER + "cmp_backgroundWithImage.pdf", |
| 152 | + DESTINATION_FOLDER, "diff")); |
| 153 | + } |
| 154 | + |
| 155 | + @Test |
| 156 | + public void emptyGridContainerTest() throws IOException, InterruptedException { |
| 157 | + String fileName = DESTINATION_FOLDER + "emptyGridContainer.pdf"; |
| 158 | + PdfDocument pdfDocument = new PdfDocument(new PdfWriter(fileName)); |
| 159 | + Document document = new Document(pdfDocument); |
| 160 | + |
| 161 | + GridContainer gridcontainer0 = new GridContainer(); |
| 162 | + |
| 163 | + gridcontainer0.setProperty(Property.COLUMN_GAP_BORDER, null); |
| 164 | + gridcontainer0.setBackgroundColor(ColorConstants.RED); |
| 165 | + gridcontainer0.setProperty(Property.GRID_TEMPLATE_COLUMNS, |
| 166 | + Arrays.asList( |
| 167 | + GridValue.createUnitValue(new UnitValue(1, 150.0f)), |
| 168 | + GridValue.createUnitValue(new UnitValue(1, 150.0f)), |
| 169 | + GridValue.createUnitValue(new UnitValue(1, 150.0f)))); |
| 170 | + gridcontainer0.setProperty(Property.COLUMN_GAP, 12.0f); |
| 171 | + document.add(gridcontainer0); |
| 172 | + |
| 173 | + document.close(); |
| 174 | + |
| 175 | + Assert.assertNull(new CompareTool().compareByContent(fileName, SOURCE_FOLDER + "cmp_emptyGridContainer.pdf", |
| 176 | + DESTINATION_FOLDER, "diff")); |
| 177 | + } |
| 178 | + |
| 179 | + |
| 180 | + private GridContainer createGridBoxWithSizedDiv() { |
| 181 | + GridContainer gridcontainer0 = new GridContainer(); |
| 182 | + |
| 183 | + gridcontainer0.setProperty(Property.COLUMN_GAP_BORDER, null); |
| 184 | + gridcontainer0.setProperty(Property.GRID_TEMPLATE_COLUMNS, |
| 185 | + Arrays.asList(new UnitValue(1, 150.0f), new UnitValue(1, 150.0f), new UnitValue(1, 150.0f))); |
| 186 | + gridcontainer0.setProperty(Property.COLUMN_GAP, 12.0f); |
| 187 | + gridcontainer0.setBackgroundColor(ColorConstants.RED); |
| 188 | + for (int i = 0; i < 4; i++) { |
| 189 | + Div div1 = new Div(); |
| 190 | + div1.setBackgroundColor(ColorConstants.YELLOW); |
| 191 | + div1.setHeight(20); |
| 192 | + div1.setWidth(30); |
| 193 | + div1.setProperty(Property.COLUMN_GAP_BORDER, null); |
| 194 | + div1.setProperty(Property.COLUMN_GAP, 12.0f); |
| 195 | + gridcontainer0.add(div1); |
| 196 | + } |
| 197 | + return gridcontainer0; |
| 198 | + |
| 199 | + } |
| 200 | + |
| 201 | + private GridContainer createGridBoxWithText() { |
| 202 | + GridContainer gridcontainer0 = new GridContainer(); |
| 203 | + |
| 204 | + gridcontainer0.setProperty(Property.COLUMN_GAP_BORDER, null); |
| 205 | + gridcontainer0.setProperty(Property.GRID_TEMPLATE_COLUMNS, |
| 206 | + Arrays.asList(GridValue.createUnitValue(new UnitValue(1, 150.0f)), |
| 207 | + GridValue.createUnitValue(new UnitValue(1, 150.0f)), |
| 208 | + GridValue.createUnitValue(new UnitValue(1, 150.0f)))); |
| 209 | + gridcontainer0.setProperty(Property.COLUMN_GAP, 12.0f); |
| 210 | + Div div1 = new Div(); |
| 211 | + div1.setBackgroundColor(ColorConstants.YELLOW); |
| 212 | + div1.setProperty(Property.COLUMN_GAP_BORDER, null); |
| 213 | + div1.setProperty(Property.COLUMN_GAP, 12.0f); |
| 214 | + Paragraph paragraph2 = new Paragraph(); |
| 215 | + Text text3 = new Text("One"); |
| 216 | + paragraph2.add(text3); |
| 217 | + |
| 218 | + div1.add(paragraph2); |
| 219 | + |
| 220 | + gridcontainer0.add(div1); |
| 221 | + |
| 222 | + Div div4 = new Div(); |
| 223 | + div4.setProperty(Property.COLUMN_GAP_BORDER, null); |
| 224 | + div4.setProperty(Property.COLUMN_GAP, 12.0f); |
| 225 | + Paragraph paragraph5 = new Paragraph(); |
| 226 | + Text text6 = new Text("Two"); |
| 227 | + paragraph5.add(text6); |
| 228 | + |
| 229 | + div4.add(paragraph5); |
| 230 | + |
| 231 | + gridcontainer0.add(div4); |
| 232 | + |
| 233 | + Div div7 = new Div(); |
| 234 | + div7.setBackgroundColor(ColorConstants.GREEN); |
| 235 | + div7.setProperty(Property.COLUMN_GAP_BORDER, null); |
| 236 | + div7.setProperty(Property.COLUMN_GAP, 12.0f); |
| 237 | + Paragraph paragraph8 = new Paragraph(); |
| 238 | + Text text9 = new Text("Three"); |
| 239 | + paragraph8.add(text9); |
| 240 | + |
| 241 | + div7.add(paragraph8); |
| 242 | + |
| 243 | + gridcontainer0.add(div7); |
| 244 | + |
| 245 | + Div div10 = new Div(); |
| 246 | + div10.setBackgroundColor(ColorConstants.CYAN); |
| 247 | + div10.setProperty(Property.COLUMN_GAP_BORDER, null); |
| 248 | + div10.setProperty(Property.COLUMN_GAP, 12.0f); |
| 249 | + Paragraph paragraph11 = new Paragraph(); |
| 250 | + Text text12 = new Text("Four"); |
| 251 | + paragraph11.add(text12); |
| 252 | + |
| 253 | + div10.add(paragraph11); |
| 254 | + |
| 255 | + gridcontainer0.add(div10); |
| 256 | + |
| 257 | + Div div13 = new Div(); |
| 258 | + |
| 259 | + div13.setProperty(Property.COLUMN_GAP_BORDER, null); |
| 260 | + div13.setProperty(Property.COLUMN_GAP, 12.0f); |
| 261 | + Paragraph paragraph14 = new Paragraph(); |
| 262 | + Text text15 = new Text("Five"); |
| 263 | + paragraph14.add(text15); |
| 264 | + |
| 265 | + div13.add(paragraph14); |
| 266 | + |
| 267 | + gridcontainer0.add(div13); |
| 268 | + return gridcontainer0; |
| 269 | + } |
| 270 | + |
| 271 | + |
| 272 | +} |
0 commit comments