如何在docx4j中添加下划线文本?

时间:2018-02-15 09:52:22

标签: docx4j

我无法找到使用docx4j插入下划线文本的方法。代码如下。

    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);

1 个答案:

答案 0 :(得分:1)

我相信你错过了

  

u.setVal(UnderlineEnumeration.SINGLE);

代码。

请尝试使用此代码段

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);