I am unable to find a way to insert underline text using docx4j. The code is given below.
WordprocessingMLPackage wordPackage = WordprocessingMLPackage.createPackage();
MainDocumentPart mainDocumentPart = wordPackage.getMainDocumentPart();
P p = factory.createP();
R r = factory.createR();
Text t = factory.createText();
t.setValue("Test");
r.getContent().add(t);
p.getContent().add(r);
RPr rpr = factory.createRPr();
U u = factory.createU();
rpr.setU(u);
r.setRPr(rpr)
mainDocumentPart.getContent().add(p);
File exportFile = new File("welcome.docx");
wordPackage.save(exportFile);
I believe that you are missing
u.setVal(UnderlineEnumeration.SINGLE);
in your code.
Kindly try this code snippet
WordprocessingMLPackage wordPackage = WordprocessingMLPackage.createPackage();
MainDocumentPart mainDocumentPart = wordPackage.getMainDocumentPart();
P p = factory.createP();
R r = factory.createR();
Text t = factory.createText();
t.setValue("Test");
r.getContent().add(t);
p.getContent().add(r);
RPr rpr = factory.createRPr();
U u = factory.createU();
//This line here will do the trick for you.
u.setVal(UnderlineEnumeration.SINGLE);
rpr.setU(u);
r.setRPr(rpr)
mainDocumentPart.getContent().add(p);
File exportFile = new File("welcome.docx");
wordPackage.save(exportFile);