Skip to content

Commit 6c79d11

Browse files
committed
Add a possibility to specify left or right alignment for list symbols explicitly. Default alignment is right
DEVSIX-433
1 parent 8266a46 commit 6c79d11

File tree

5 files changed

+57
-2
lines changed

5 files changed

+57
-2
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public enum Property {
4949
LINE_DRAWER,
5050
LIST_START,
5151
LIST_SYMBOL(true),
52+
LIST_SYMBOL_ALIGNMENT,
5253
LIST_SYMBOL_INDENT,
5354
MARGIN_BOTTOM,
5455
MARGIN_LEFT,
@@ -148,6 +149,20 @@ public enum TextAlignment {
148149
JUSTIFIED_ALL
149150
}
150151

152+
/**
153+
* A specialized enum containing alignment properties for list symbols.
154+
* {@link ListSymbolAlignment#LEFT} means that the items will be aligned as follows:
155+
* 9. Item 9
156+
* 10. Item 10
157+
*
158+
* Whereas {@link ListSymbolAlignment#RIGHT} means the items will be aligned as follows:
159+
* 9. Item 9
160+
* 10. Item 10
161+
*/
162+
public enum ListSymbolAlignment {
163+
RIGHT,
164+
LEFT
165+
}
151166

152167
/**
153168
* A specialized class holding configurable properties related to an {@link

layout/src/main/java/com/itextpdf/layout/element/List.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public List setListSymbol(Image image) {
9999
/**
100100
* Sets the list numbering type to be used. This will create an ordered list,
101101
* i.e. every {@link ListItem} will have a unique prefix.
102-
* @param listNumberingType the {@link Property#ListNumberingType} that will generate appropriate prefixes for the {@link ListItem}s.
102+
* @param listNumberingType the {@link Property.ListNumberingType} that will generate appropriate prefixes for the {@link ListItem}s.
103103
* @return this list.
104104
*/
105105
public List setListSymbol(Property.ListNumberingType listNumberingType) {
@@ -111,6 +111,20 @@ public List setListSymbol(Property.ListNumberingType listNumberingType) {
111111
return setProperty(Property.LIST_SYMBOL, listNumberingType);
112112
}
113113

114+
/**
115+
* A specialized enum containing alignment properties for list symbols.
116+
* {@link Property.ListSymbolAlignment#LEFT} means that the items will be aligned as follows:
117+
* 9. Item 9
118+
* 10. Item 10
119+
*
120+
* Whereas {@link Property.ListSymbolAlignment#RIGHT} means the items will be aligned as follows:
121+
* 9. Item 9
122+
* 10. Item 10
123+
*/
124+
public List setListSymbolAlignment(Property.ListSymbolAlignment alignment) {
125+
return setProperty(Property.LIST_SYMBOL_ALIGNMENT, alignment);
126+
}
127+
114128
/**
115129
* Gets the indent offset of the {@link ListItem} symbols.
116130
*

layout/src/main/java/com/itextpdf/layout/renderer/ListItemRenderer.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,12 @@ public void draw(DrawContext drawContext) {
7373
symbolRenderer.getOccupiedArea().getBBox().getHeight() - symbolRenderer.getOccupiedArea().getBBox().getY());
7474
}
7575

76-
symbolRenderer.move(x + symbolAreaWidth - symbolRenderer.getOccupiedArea().getBBox().getWidth() - symbolRenderer.getOccupiedArea().getBBox().getX(), 0);
76+
Property.ListSymbolAlignment listSymbolAlignment = parent.getProperty(Property.LIST_SYMBOL_ALIGNMENT);
77+
float xPosition = x - symbolRenderer.getOccupiedArea().getBBox().getX();
78+
if (listSymbolAlignment == null || listSymbolAlignment == Property.ListSymbolAlignment.RIGHT) {
79+
xPosition += symbolAreaWidth - symbolRenderer.getOccupiedArea().getBBox().getWidth();
80+
}
81+
symbolRenderer.move(xPosition, 0);
7782

7883
if (isTagged) {
7984
tagStructure.addTag(0, PdfName.Lbl);

layout/src/test/java/com/itextpdf/layout/ListTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,4 +226,25 @@ public void imageInListTest01() throws IOException, InterruptedException {
226226
Assert.assertNull(new CompareTool().compareByContent(outFileName, cmpFileName, destinationFolder, "diff"));
227227
}
228228

229+
@Test
230+
public void listItemAlignmentTest01() throws IOException, InterruptedException {
231+
String outFileName = destinationFolder + "listItemAlignmentTest01.pdf";
232+
String cmpFileName = sourceFolder + "cmp_listItemAlignmentTest01.pdf";
233+
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(new FileOutputStream(outFileName)));
234+
235+
Document document = new Document(pdfDocument);
236+
237+
List list = new List(Property.ListNumberingType.DECIMAL).setListSymbolAlignment(Property.ListSymbolAlignment.LEFT);
238+
239+
for (int i = 1; i <= 30; i++) {
240+
list.add("Item #" + i);
241+
}
242+
243+
document.add(list);
244+
245+
document.close();
246+
247+
Assert.assertNull(new CompareTool().compareByContent(outFileName, cmpFileName, destinationFolder, "diff"));
248+
}
249+
229250
}

0 commit comments

Comments
 (0)