Howdy,
I have encounter an issue with textbox text alignments and wondered if there has been a change to the way it used to work. Originally this textbox was fine in previous versions but now is doing something different. The text was being aligned correctly but now, the second line doesn’t align to the centre and vertical alignment is set to middle centred, meaning if you changed the horizontal alignment to left or right, it doesn’t go to the edge of the textbox. Here is the following code:
<pre style=“background-color: rgb(255, 255, 255); font-family: “Courier New”; font-size: 9pt;”>final Workbook wb = new Workbook();
final Worksheet ws = wb.getWorksheets().get(0);
TextBox t = ws.getTextBoxes().get(
ws.getTextBoxes().add(
3,
3,
100,
100));
t.setText(“Line 1\nLine2”);
t.setTextVerticalAlignment(TextAlignmentType.CENTER);
t.setTextHorizontalAlignment(TextAlignmentType.CENTER);
wb.save(“C:/export/Test Report.xlsx”);
Desktop.getDesktop().open(new File(“C:/export/Test Report.xlsx”));
Howdy,
I’ve opened the output you have linked and it shows the same problem. I have attached a report that has the result textbox on the left and a textbox on the right that it should be showing. Also note the Textbox vertical alignment options under Format Shape for each one.
To note, the first version I was using where the textbox was working was 8.7.0. I updated the project to 16.11.0 since its the most current version for a Maven project and this is where the issue came up. I’ve checked 16.11.4 as an external library and the results are the same as 16.11.0.
Hi James,
Thank you for sharing the comparison. Please try the following piece of code which produces the expected results (attached).
Java
final Workbook wb = new Workbook();
final Worksheet ws = wb.getWorksheets().get(0);
TextBox shape = ws.getTextBoxes().get(
ws.getTextBoxes().add(
3,
3,
100,
100));
shape.setText(“Line 1\nLine2”);
//Access the first paragraph and set its horizontal alignment to center.
TextParagraph p = shape.getTextBody().getTextParagraphs().get(0);
p.setAlignmentType(TextAlignmentType.CENTER);
//Access the second paragraph and set its horizontal alignment to center.
p = shape.getTextBody().getTextParagraphs().get(1);
p.setAlignmentType(TextAlignmentType.CENTER);
//Set vertical alignment of all contents.
shape.setTextVerticalAlignment(TextAlignmentType.CENTER);
wb.save(dir + “Test Report.xlsx”);
Thank you Babar, it appears to be working correctly.
Hi,
Good to know that the suggested code provided by Babar figures out your issue now. Feel free to write us back if you have further queries or issue, we will be happy to assist you soon.
Thank you.