Skip to content

Commit efa80d7

Browse files
committed
Add properties to Style and AbstractElement.
1 parent ba50f54 commit efa80d7

File tree

3 files changed

+293
-13
lines changed

3 files changed

+293
-13
lines changed

layout/src/main/java/com/itextpdf/layout/RootElement.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,10 @@ public abstract class RootElement<T extends IPropertyContainer> extends ElementP
9696
* @return this element
9797
* @see BlockElement
9898
*/
99-
public T add(BlockElement element) {
99+
public RootElement<T> add(BlockElement element) {
100100
childElements.add(element);
101101
ensureRootRendererNotNull().addChild(element.createRendererSubTree());
102-
return (T) this;
102+
return this;
103103
}
104104

105105
/**
@@ -108,10 +108,10 @@ public T add(BlockElement element) {
108108
* @return this element
109109
* @see Image
110110
*/
111-
public T add(Image image) {
111+
public RootElement<T> add(Image image) {
112112
childElements.add(image);
113113
ensureRootRendererNotNull().addChild(image.createRendererSubTree());
114-
return (T) this;
114+
return this;
115115
}
116116

117117
@Override
@@ -197,7 +197,7 @@ public RootRenderer getRenderer() {
197197
* @param textAlign horizontal alignment about the specified point
198198
* @return this object
199199
*/
200-
public <T> T showTextAligned(String text, float x, float y, TextAlignment textAlign) {
200+
public RootElement<T> showTextAligned(String text, float x, float y, TextAlignment textAlign) {
201201
return showTextAligned(text, x, y, textAlign, 0);
202202
}
203203

@@ -210,7 +210,7 @@ public <T> T showTextAligned(String text, float x, float y, TextAlignment textAl
210210
* @param angle the angle of rotation applied to the text, in radians
211211
* @return this object
212212
*/
213-
public <T> T showTextAligned(String text, float x, float y, TextAlignment textAlign, float angle) {
213+
public RootElement<T> showTextAligned(String text, float x, float y, TextAlignment textAlign, float angle) {
214214
return showTextAligned(text, x, y, textAlign, VerticalAlignment.BOTTOM, angle);
215215
}
216216

@@ -224,7 +224,7 @@ public <T> T showTextAligned(String text, float x, float y, TextAlignment textAl
224224
* @param angle the angle of rotation applied to the text, in radians
225225
* @return this object
226226
*/
227-
public <T> T showTextAligned(String text, float x, float y, TextAlignment textAlign, VerticalAlignment vertAlign, float angle) {
227+
public RootElement<T> showTextAligned(String text, float x, float y, TextAlignment textAlign, VerticalAlignment vertAlign, float angle) {
228228
Paragraph p = new Paragraph(text);
229229
return showTextAligned(p, x, y, pdfDocument.getNumberOfPages(), textAlign, vertAlign, angle);
230230
}
@@ -239,7 +239,7 @@ public <T> T showTextAligned(String text, float x, float y, TextAlignment textAl
239239
* @param angle the angle of rotation applied to the text, in radians
240240
* @return this object
241241
*/
242-
public <T> T showTextAlignedKerned(String text, float x, float y, TextAlignment textAlign, VerticalAlignment vertAlign, float angle) {
242+
public RootElement<T> showTextAlignedKerned(String text, float x, float y, TextAlignment textAlign, VerticalAlignment vertAlign, float angle) {
243243
Paragraph p = new Paragraph(text).setFontKerning(FontKerning.YES);
244244
return showTextAligned(p, x, y, pdfDocument.getNumberOfPages(), textAlign, vertAlign, angle);
245245
}
@@ -253,7 +253,7 @@ public <T> T showTextAlignedKerned(String text, float x, float y, TextAlignment
253253
* @param textAlign horizontal alignment about the specified point
254254
* @return this object
255255
*/
256-
public <T> T showTextAligned(Paragraph p, float x, float y, TextAlignment textAlign) {
256+
public RootElement<T> showTextAligned(Paragraph p, float x, float y, TextAlignment textAlign) {
257257
return showTextAligned(p, x, y, pdfDocument.getNumberOfPages(), textAlign, VerticalAlignment.BOTTOM, 0);
258258
}
259259

@@ -267,7 +267,7 @@ public <T> T showTextAligned(Paragraph p, float x, float y, TextAlignment textAl
267267
* @param vertAlign vertical alignment about the specified point
268268
* @return this object
269269
*/
270-
public <T> T showTextAligned(Paragraph p, float x, float y, TextAlignment textAlign, VerticalAlignment vertAlign) {
270+
public RootElement<T> showTextAligned(Paragraph p, float x, float y, TextAlignment textAlign, VerticalAlignment vertAlign) {
271271
return showTextAligned(p, x, y, pdfDocument.getNumberOfPages(), textAlign, vertAlign, 0);
272272
}
273273

@@ -283,7 +283,7 @@ public <T> T showTextAligned(Paragraph p, float x, float y, TextAlignment textAl
283283
* @param angle the angle of rotation applied to the text, in radians
284284
* @return this object
285285
*/
286-
public <T> T showTextAligned(Paragraph p, float x, float y, int pageNumber, TextAlignment textAlign, VerticalAlignment vertAlign, float angle) {
286+
public RootElement<T> showTextAligned(Paragraph p, float x, float y, int pageNumber, TextAlignment textAlign, VerticalAlignment vertAlign, float angle) {
287287
Div div = new Div();
288288
div.setTextAlignment(textAlign).setVerticalAlignment(vertAlign);
289289
if (angle != 0) {
@@ -319,7 +319,7 @@ public <T> T showTextAligned(Paragraph p, float x, float y, int pageNumber, Text
319319
div.setRole(PdfName.Artifact);
320320
this.add(div);
321321

322-
return (T) this;
322+
return this;
323323
}
324324

325325
protected abstract RootRenderer ensureRootRendererNotNull();

layout/src/main/java/com/itextpdf/layout/Style.java

Lines changed: 270 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,280 @@ This file is part of the iText (R) project.
4545
package com.itextpdf.layout;
4646

4747
import com.itextpdf.layout.element.AbstractElement;
48+
import com.itextpdf.layout.element.BlockElement;
49+
import com.itextpdf.layout.property.Property;
50+
import com.itextpdf.layout.property.VerticalAlignment;
4851

49-
/** Container object for style properties of an element. A style can be used as
52+
/**
53+
* Container object for style properties of an element. A style can be used as
5054
* an effective way to define multiple equal properties to several elements.
5155
* Used in {@link AbstractElement}
5256
*/
5357
public class Style extends ElementPropertyContainer<Style> {
5458

59+
/**
60+
* Gets the current left margin width of the element.
61+
* @return the left margin width, as a <code>float</code>
62+
*/
63+
public Float getMarginLeft() {
64+
return getProperty(Property.MARGIN_LEFT);
65+
}
66+
67+
/**
68+
* Sets the left margin width of the element.
69+
* @param value the new left margin width
70+
* @return this element
71+
*/
72+
public Style setMarginLeft(float value) {
73+
setProperty(Property.MARGIN_LEFT, value);
74+
return this;
75+
}
76+
77+
/**
78+
* Gets the current right margin width of the element.
79+
* @return the right margin width, as a <code>float</code>
80+
*/
81+
public Float getMarginRight() {
82+
return getProperty(Property.MARGIN_RIGHT);
83+
}
84+
85+
/**
86+
* Sets the right margin width of the element.
87+
* @param value the new right margin width
88+
* @return this element
89+
*/
90+
public Style setMarginRight(float value) {
91+
setProperty(Property.MARGIN_RIGHT, value);
92+
return this;
93+
}
94+
95+
/**
96+
* Gets the current top margin width of the element.
97+
* @return the top margin width, as a <code>float</code>
98+
*/
99+
public Float getMarginTop() {
100+
return getProperty(Property.MARGIN_TOP);
101+
}
102+
103+
/**
104+
* Sets the top margin width of the element.
105+
* @param value the new top margin width
106+
* @return this element
107+
*/
108+
public Style setMarginTop(float value) {
109+
setProperty(Property.MARGIN_TOP, value);
110+
return this;
111+
}
112+
113+
/**
114+
* Gets the current bottom margin width of the element.
115+
* @return the bottom margin width, as a <code>float</code>
116+
*/
117+
public Float getMarginBottom() {
118+
return getProperty(Property.MARGIN_BOTTOM);
119+
}
120+
121+
/**
122+
* Sets the bottom margin width of the element.
123+
* @param value the new bottom margin width
124+
* @return this element
125+
*/
126+
public Style setMarginBottom(float value) {
127+
setProperty(Property.MARGIN_BOTTOM, value);
128+
return this;
129+
}
130+
131+
/**
132+
* Sets all margins around the element to the same width.
133+
*
134+
* @param commonMargin the new margin width
135+
* @return this element
136+
*/
137+
public Style setMargin(float commonMargin) {
138+
return setMargins(commonMargin, commonMargin, commonMargin, commonMargin);
139+
}
140+
141+
/**
142+
* Sets the margins around the element to a series of new widths.
143+
*
144+
* @param marginTop the new margin top width
145+
* @param marginRight the new margin right width
146+
* @param marginBottom the new margin bottom width
147+
* @param marginLeft the new margin left width
148+
* @return this element
149+
*/
150+
public Style setMargins(float marginTop, float marginRight, float marginBottom, float marginLeft) {
151+
setMarginTop(marginTop);
152+
setMarginRight(marginRight);
153+
setMarginBottom(marginBottom);
154+
setMarginLeft(marginLeft);
155+
return this;
156+
}
157+
158+
/**
159+
* Gets the current left padding width of the element.
160+
* @return the left padding width, as a <code>float</code>
161+
*/
162+
public Float getPaddingLeft() {
163+
return getProperty(Property.PADDING_LEFT);
164+
}
165+
166+
/**
167+
* Sets the left padding width of the element.
168+
* @param value the new left padding width
169+
* @return this element
170+
*/
171+
public Style setPaddingLeft(float value) {
172+
setProperty(Property.PADDING_LEFT, value);
173+
return this;
174+
}
175+
176+
/**
177+
* Gets the current right padding width of the element.
178+
* @return the right padding width, as a <code>float</code>
179+
*/
180+
public Float getPaddingRight() {
181+
return getProperty(Property.PADDING_RIGHT);
182+
}
183+
184+
/**
185+
* Sets the right padding width of the element.
186+
* @param value the new right padding width
187+
* @return this element
188+
*/
189+
public Style setPaddingRight(float value) {
190+
setProperty(Property.PADDING_RIGHT, value);
191+
return this;
192+
}
193+
194+
/**
195+
* Gets the current top padding width of the element.
196+
* @return the top padding width, as a <code>float</code>
197+
*/
198+
public Float getPaddingTop() {
199+
return getProperty(Property.PADDING_TOP);
200+
}
201+
202+
/**
203+
* Sets the top padding width of the element.
204+
* @param value the new top padding width
205+
* @return this element
206+
*/
207+
public Style setPaddingTop(float value) {
208+
setProperty(Property.PADDING_TOP, value);
209+
return this;
210+
}
211+
212+
/**
213+
* Gets the current bottom padding width of the element.
214+
* @return the bottom padding width, as a <code>float</code>
215+
*/
216+
public Float getPaddingBottom() {
217+
return getProperty(Property.PADDING_BOTTOM);
218+
}
219+
220+
/**
221+
* Sets the bottom padding width of the element.
222+
* @param value the new bottom padding width
223+
* @return this element
224+
*/
225+
public Style setPaddingBottom(float value) {
226+
setProperty(Property.PADDING_BOTTOM, value);
227+
return this;
228+
}
229+
230+
/**
231+
* Sets all paddings around the element to the same width.
232+
*
233+
* @param commonPadding the new padding width
234+
* @return this element
235+
*/
236+
public Style setPadding(float commonPadding) {
237+
return setPaddings(commonPadding, commonPadding, commonPadding, commonPadding);
238+
}
239+
240+
/**
241+
* Sets the paddings around the element to a series of new widths.
242+
*
243+
* @param paddingTop the new padding top width
244+
* @param paddingRight the new padding right width
245+
* @param paddingBottom the new padding bottom width
246+
* @param paddingLeft the new padding left width
247+
* @return this element
248+
*/
249+
public Style setPaddings(float paddingTop, float paddingRight, float paddingBottom, float paddingLeft) {
250+
setPaddingTop(paddingTop);
251+
setPaddingRight(paddingRight);
252+
setPaddingBottom(paddingBottom);
253+
setPaddingLeft(paddingLeft);
254+
return this;
255+
}
256+
257+
/**
258+
* Sets the vertical alignment of the element.
259+
*
260+
* @param verticalAlignment the vertical alignment setting
261+
* @return this element
262+
*/
263+
public Style setVerticalAlignment(VerticalAlignment verticalAlignment) {
264+
setProperty(Property.VERTICAL_ALIGNMENT, verticalAlignment);
265+
return this;
266+
}
267+
268+
/**
269+
* Sets a ratio which determines in which proportion will word spacing and character spacing
270+
* be applied when horizontal alignment is justified.
271+
* @param ratio the ratio coefficient. It must be between 0 and 1, inclusive.
272+
* It means that <b>ratio</b> part of the free space will
273+
* be compensated by word spacing, and <b>1-ratio</b> part of the free space will
274+
* be compensated by character spacing.
275+
* If <b>ratio</b> is 1, additional character spacing will not be applied.
276+
* If <b>ratio</b> is 0, additional word spacing will not be applied.
277+
*/
278+
public Style setSpacingRatio(float ratio) {
279+
setProperty(Property.SPACING_RATIO, ratio);
280+
return this;
281+
}
282+
283+
/**
284+
* Returns whether the {@link BlockElement} should be kept together as much
285+
* as possible.
286+
* @return the current value of the {@link Property#KEEP_TOGETHER} property
287+
*/
288+
public Boolean isKeepTogether() {
289+
return getProperty(Property.KEEP_TOGETHER);
290+
}
291+
292+
/**
293+
* Sets whether the {@link BlockElement} should be kept together as much
294+
* as possible.
295+
* @param keepTogether the new value of the {@link Property#KEEP_TOGETHER} property
296+
* @return this element
297+
*/
298+
public Style setKeepTogether(boolean keepTogether) {
299+
setProperty(Property.KEEP_TOGETHER, keepTogether);
300+
return this;
301+
}
302+
303+
/**
304+
* Sets the rotation radAngle.
305+
*
306+
* @param radAngle the new rotation radAngle, as a <code>float</code>
307+
* @return this element
308+
*/
309+
public Style setRotationAngle(float radAngle) {
310+
setProperty(Property.ROTATION_ANGLE, radAngle);
311+
return this;
312+
}
313+
314+
/**
315+
* Sets the rotation angle.
316+
*
317+
* @param angle the new rotation angle, as a <code>double</code>
318+
* @return this element
319+
*/
320+
public Style setRotationAngle(double angle) {
321+
setProperty(Property.ROTATION_ANGLE, (float) angle);
322+
return this;
323+
}
55324
}

0 commit comments

Comments
 (0)