I'm reading one string in that some li tags have spans with styles so I'm trying to add styles for a particular word in the li tag (not for the whole line), but for every styles change it is going for new bullet point instead of appending to the same li,
String:
"<p><span style=\"font-weight: bold;\">Title</span></p> <ul> <li> <span style=\"font-weight: bold;\"> </span><span>Laser cutting of sheet </span><span style=\"font-weight: bold;\">metal</span><span> generates a continuous heat source which can led to high levels of thermal stress </span> </li> <li><span>Thermal Stress can deform the material at the micron level and the slightest deformation reduces the effectiveness of the sheet metal components</span></li> <br /> </ul> <p><span> </span><span style=\"font-weight: bold;\">Mass Level Production</span></p> <ul> <li><span>Laser cutting cannot produce more component more than one part at a time</span></li> <li><span>When production is required at a lower level , laser cutting may be apt but inefficient for mass level production</span></li> </ul> <p><span style=\"font-weight: bold;\">Metal etching with aluminum</span></p> <p><span style=\"font-weight: bold;\"> </span></p> <ul> <li><span>Aluminum is a reflective material that is reactive to heat in the manufacturing process and therefore, makes it less suitable for laser cutting and wire EDM processes</span></li> <li> <span>As the use of aluminum is growing in many industries, there is a requirement for a suitable manufacturing process for thin </span><span style=\"font-weight: bold;\">aluminum</span><span>components</span> </li> </ul>"
Can anyone help me, like where I'm doing the mistake?
Code:
public static void advancedUlLiConvert(XSLFTextShape textShape, String str) {
System.out.println("Recived String: "+str);
Document doc = null;
try {
doc = Jsoup.parse(str);
Element element = doc.body();
Elements lstElements = element.children();
System.out.println("bodyChildEle : " +lstElements);
for (Element ele: lstElements) {
if (ele.tagName() == "p") {
System.out.println("para" + ele);
String para = ele.children().toString();
System.out.println("String para" + para);
//if any <p> tags are there then I'm calling this method to take care of that which is working fine,
addAllStyledText(textShape,para, false);
}
//I'm facing problems in these below part
if (ele.tagName() == "ul") {
Elements lstLi = ele.children();
System.out.println("lstsi" + lstLi);
for (Element li: lstLi) {
Elements spans = li.children();
XSLFTextParagraph paragraph = textShape.addNewTextParagraph();
paragraph.setBullet(true);
XSLFTextRun textRun = paragraph.addNewTextRun();
System.out.println("allspans: " + spans);
for (Element span: spans) {
System.out.println("loopspan" + span);
if (!(span.hasText())) {
span.remove();
System.out.println("Removed Element: "+ span);
} else if (span.hasAttr("style")) {
System.out.println("Styled Span: " + span);
String strSpan = span.toString();
boolean isBold = false;
boolean isItalic = false;
boolean isUnderline = false;
boolean isLineThrough = false;
if (strSpan.contains(": bold;"))
isBold = true;
if (strSpan.contains(": italic;"))
isItalic = true;
if (strSpan.contains(": underline;"))
isUnderline = true;
if (strSpan.contains(": line-through;"))
isLineThrough = true;
String text = span.text();
textRun = textShape.appendText(text+" ", false);
if (isBold) {
textRun.setBold(true);
//textRun.setFontColor(rgb);
}
if (isItalic) {
textRun.setItalic(true);
}
if (isUnderline) {
textRun.setUnderlined(true);
}
if (isLineThrough) {
textRun.setStrikethrough(true);
}
//textRun.setText(text);
} else {
System.out.println("Span without Style: " + span);
String text = span.text();
//if I make this as false than whole code braking not sure why
textRun = textShape.appendText(text + " ", true);
//textRun.setText(text);
//paragraph.setBullet(false);
//paragraph.setBullet(false);
//paragraph.addNewTextRun().setText(text);
}
XSLFTextRun dummyTextRun = textShape.appendText("", false);
dummyTextRun.setBold(false);
dummyTextRun.setItalic(false);
dummyTextRun.setUnderlined(false);
dummyTextRun.setStrikethrough(false);
}
paragraph.setBullet(false);
}
}
}
} catch (Exception e) {
log.error("Exception in advancedUlLiConvert: " , e);
}
}

