1
+ package com .itextpdf .layout ;
2
+
3
+ import com .itextpdf .io .image .ImageDataFactory ;
4
+ import com .itextpdf .io .util .MessageFormatUtil ;
5
+ import com .itextpdf .kernel .colors .ColorConstants ;
6
+ import com .itextpdf .kernel .pdf .PdfDocument ;
7
+ import com .itextpdf .kernel .pdf .PdfWriter ;
8
+ import com .itextpdf .kernel .utils .CompareTool ;
9
+ import com .itextpdf .layout .borders .DashedBorder ;
10
+ import com .itextpdf .layout .borders .SolidBorder ;
11
+ import com .itextpdf .layout .element .Div ;
12
+ import com .itextpdf .layout .element .Image ;
13
+ import com .itextpdf .layout .element .Paragraph ;
14
+ import com .itextpdf .layout .property .ClearPropertyValue ;
15
+ import com .itextpdf .layout .property .FloatPropertyValue ;
16
+ import com .itextpdf .layout .property .HorizontalAlignment ;
17
+ import com .itextpdf .layout .property .Property ;
18
+ import com .itextpdf .layout .property .UnitValue ;
19
+ import com .itextpdf .test .ExtendedITextTest ;
20
+ import com .itextpdf .test .annotations .type .IntegrationTest ;
21
+
22
+ import java .io .IOException ;
23
+ import java .net .MalformedURLException ;
24
+ import java .util .ArrayList ;
25
+ import java .util .List ;
26
+ import javax .xml .parsers .ParserConfigurationException ;
27
+ import org .junit .Assert ;
28
+ import org .junit .BeforeClass ;
29
+ import org .junit .Test ;
30
+ import org .junit .experimental .categories .Category ;
31
+ import org .xml .sax .SAXException ;
32
+
33
+ @ Category (IntegrationTest .class )
34
+ public class FloatBlockTest extends ExtendedITextTest {
35
+ private static final String sourceFolder = "./src/test/resources/com/itextpdf/layout/FloatBlockTest/" ;
36
+ private static final String destinationFolder = "./target/test/com/itextpdf/layout/FloatBlockTest/" ;
37
+
38
+ @ BeforeClass
39
+ public static void beforeClass () {
40
+ createOrClearDestinationFolder (destinationFolder );
41
+ }
42
+
43
+ @ Test
44
+ public void floatImageInDivClearNoneTest () throws IOException , InterruptedException ,
45
+ ParserConfigurationException , SAXException {
46
+ String dest = destinationFolder + "floatImageInDivClearNone.pdf" ;
47
+
48
+ PdfDocument pdf = new PdfDocument (new PdfWriter (dest ));
49
+ Document document = new Document (pdf );
50
+ pdf .setTagged ();
51
+
52
+ addFloatingImagesInDivs (document , new UnitValue (UnitValue .POINT , 200f ), ClearPropertyValue .NONE );
53
+
54
+ document .close ();
55
+
56
+ Assert .assertNull (new CompareTool ()
57
+ .compareByContent (dest , sourceFolder + "cmp_floatImageInDivClearNone.pdf" , destinationFolder ));
58
+ Assert .assertNull (
59
+ new CompareTool ().compareTagStructures (dest , sourceFolder + "cmp_floatImageInDivClearNone.pdf" ));
60
+ }
61
+
62
+ @ Test
63
+ public void floatImageInDivClearBothTest () throws IOException , InterruptedException ,
64
+ ParserConfigurationException , SAXException {
65
+ String dest = destinationFolder + "floatImageInDivClearBoth.pdf" ;
66
+
67
+ PdfDocument pdf = new PdfDocument (new PdfWriter (dest ));
68
+ Document document = new Document (pdf );
69
+ pdf .setTagged ();
70
+
71
+ addFloatingImagesInDivs (document , new UnitValue (UnitValue .POINT , 200f ), ClearPropertyValue .BOTH );
72
+
73
+ document .close ();
74
+
75
+ Assert .assertNull (new CompareTool ()
76
+ .compareByContent (dest , sourceFolder + "cmp_floatImageInDivClearBoth.pdf" , destinationFolder ));
77
+ Assert .assertNull (
78
+ new CompareTool ().compareTagStructures (dest , sourceFolder + "cmp_floatImageInDivClearBoth.pdf" ));
79
+ }
80
+
81
+ @ Test
82
+ public void floatImageDifferentSizeInDivTest ()
83
+ throws IOException , InterruptedException , ParserConfigurationException , SAXException {
84
+ String cmpFileName = sourceFolder + "cmp_floatImageDifferentSizeInDiv.pdf" ;
85
+ String outFile = destinationFolder + "floatImageInDiv.pdf" ;
86
+
87
+ PdfDocument pdfDoc = new PdfDocument (new PdfWriter (outFile ));
88
+ Document doc = new Document (pdfDoc );
89
+ pdfDoc .setTagged ();
90
+
91
+ UnitValue width = new UnitValue (UnitValue .PERCENT , 33f );
92
+
93
+ Image image = new Image (ImageDataFactory .create (sourceFolder + "5.png" ));
94
+ image
95
+ .setBorder (new SolidBorder (1f ))
96
+ .setWidth (width )
97
+ .setProperty (Property .FLOAT , FloatPropertyValue .RIGHT );
98
+
99
+ Div div = new Div ();
100
+ div .setBorder (new DashedBorder (ColorConstants .LIGHT_GRAY , 1 ));
101
+ div .setProperty (Property .CLEAR , ClearPropertyValue .BOTH );
102
+ div .setProperty (Property .FLOAT , FloatPropertyValue .RIGHT );
103
+
104
+ div .add (image );
105
+ doc .add (div );
106
+
107
+ Image image1 = new Image (ImageDataFactory .create (sourceFolder + "4.png" ));
108
+ image1
109
+ .setBorder (new SolidBorder (1f ))
110
+ .setWidth (width )
111
+ .setProperty (Property .FLOAT , FloatPropertyValue .RIGHT );
112
+
113
+ Div div1 = new Div ();
114
+ div1 .setBorder (new DashedBorder (ColorConstants .LIGHT_GRAY , 1 ));
115
+ div1 .setProperty (Property .CLEAR , ClearPropertyValue .BOTH );
116
+ div1 .setProperty (Property .FLOAT , FloatPropertyValue .RIGHT );
117
+
118
+ div1 .add (image1 );
119
+ doc .add (div1 );
120
+
121
+ doc .close ();
122
+
123
+ Assert .assertNull (new CompareTool ().compareByContent (outFile , cmpFileName , destinationFolder ));
124
+ Assert .assertNull (new CompareTool ().compareTagStructures (outFile , cmpFileName ));
125
+ }
126
+
127
+ private static void addFloatingImagesInDivs (Document document , UnitValue width , ClearPropertyValue clearValue )
128
+ throws MalformedURLException {
129
+ List <ImageProperties > imagePropertiesList = new ArrayList <>();
130
+ imagePropertiesList .add (new ImageProperties (FloatPropertyValue .LEFT , clearValue , null , width ));
131
+ imagePropertiesList .add (new ImageProperties (FloatPropertyValue .RIGHT , clearValue , null , width ));
132
+ imagePropertiesList
133
+ .add (new ImageProperties (FloatPropertyValue .NONE , clearValue , HorizontalAlignment .CENTER , width ));
134
+ imagePropertiesList .add (new ImageProperties (FloatPropertyValue .LEFT , clearValue , null , width ));
135
+
136
+ document .add (new Paragraph (
137
+ "Four images followed by two paragraphs. All images are wrapped in Divs.\n " +
138
+ "All images specify WIDTH = " + width ));
139
+
140
+ for (int i = 0 ; i < imagePropertiesList .size (); i ++) {
141
+ document .add (new Paragraph ("Image " + (i + 1 ) + ": " + imagePropertiesList .get (i )));
142
+ }
143
+
144
+ for (int i = 0 ; i < imagePropertiesList .size (); i ++) {
145
+ Image image = new Image (ImageDataFactory .create (MessageFormatUtil .format (sourceFolder + "{0}.png" , i + 1 )));
146
+ image .setBorder (new SolidBorder (1f ));
147
+ image .setWidth (width );
148
+
149
+ Div div = new Div ();
150
+ div .setProperty (Property .CLEAR , clearValue );
151
+ div .setProperty (Property .FLOAT , imagePropertiesList .get (i ).floatPropertyValue );
152
+ div .add (image );
153
+ document .add (div );
154
+ }
155
+
156
+ document .add (new Paragraph (
157
+ "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor..." ));
158
+ document .add (new Paragraph (
159
+ "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod "
160
+ + "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim "
161
+ + "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex "
162
+ + "ea commodo consequat. Duis aute irure dolor in reprehenderit in "
163
+ + "voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur"
164
+ + " sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt"
165
+ + " mollit anim id est laborum.\n "
166
+ + "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod"
167
+ + " tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim "
168
+ + "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea "
169
+ + "commodo consequat. Duis aute irure dolor in ......" ));
170
+ }
171
+
172
+ private static class ImageProperties {
173
+
174
+ FloatPropertyValue floatPropertyValue ;
175
+ ClearPropertyValue clearPropertyValue ;
176
+ HorizontalAlignment horizontalAlignment ;
177
+ UnitValue width ;
178
+
179
+ public ImageProperties (FloatPropertyValue floatPropertyValue , ClearPropertyValue clearPropertyValue ,
180
+ HorizontalAlignment horizontalAlignment , UnitValue width ) {
181
+ this .floatPropertyValue = floatPropertyValue ;
182
+ this .clearPropertyValue = clearPropertyValue ;
183
+ this .horizontalAlignment = horizontalAlignment ;
184
+ this .width = width ;
185
+ }
186
+
187
+ public String toString () {
188
+ String hAlignString ;
189
+ if (horizontalAlignment == null ) {
190
+ hAlignString = "null" ;
191
+ } else {
192
+ hAlignString = horizontalAlignment .toString ();
193
+ }
194
+ return MessageFormatUtil
195
+ .format ("float={0} clear={1} horiz_align={2}" , floatPropertyValue ,
196
+ clearPropertyValue , hAlignString );
197
+ }
198
+ }
199
+ }
0 commit comments