通过样式 ID 在 XWPFRun 上设置样式

我正在尝试将命名样式应用于 XWPFDocument 中的单个运行,但我看到了奇怪的结果。

XWPFRunjavadoc 描述了 setStyle 方法,但该样式似乎未应用于最终文档。我说会出现,因为在 Finder 的 QuickLook 预览中,样式确实会按预期出现在运行中。在下面的示例中,我将命名样式应用于超链接,它在右侧的预览中按预期显示,但在左侧的 Word 中没有显示。

http://img3.mukewang.com/61b2f9980001ded810910874.jpg

很明显 POI 实际上是在做一些事情来应用样式,但 Word 没有渲染样式。我尝试了其他几个 .docx 阅读器,所有这些都产生了类似的结果。


所以我开始剥离样式并将属性单独应用到运行中,这在 Word中确实有效。这是其中一件似乎我必须错过的事情。我当然可以编写一个可以读取现有样式的例程并将其应用于这样的运行,但我宁愿不这样做。我已经搜索了答案,但这部分 POI 似乎正在进行中。


那么我是否只是遗漏了一些明显的东西,或者我只是不得不接受它并以痛苦的方式做到这一点?


//This does not work.

run.setStyle(styleId);


if(docStyles.styleExist(styleId))

{


    /*

        In order to set the style on the run, we need to manually

        determine the properties of the style, and set them on the

        run individually.


        This makes no sense.

     */

    XWPFStyle style = docStyles.getStyle(styleId);


    CTStyle ctStyle = style.getCTStyle();

    CTRPr ctRpr = ctStyle.getRPr();


    if (ctRpr.isSetB())

    {

        CTOnOff onOff = ctRpr.getB();

        STOnOff.Enum stOnOff = onOff.getVal();


        boolean bold = (stOnOff == STOnOff.TRUE);


        run.setBold(bold);

    }

    if(ctRpr.isSetU())

    {

        CTUnderline underline = ctRpr.getU();

        STUnderline.Enum val = underline.getVal();


        UnderlinePatterns underlinePattern = UnderlinePatterns.valueOf(val.intValue());


        run.setUnderline(underlinePattern);

    }

    // ... //

}

else

{

    System.out.println("404: Style not found");

}


守候你守候我
浏览 983回答 1
1回答

摇曳的蔷薇

如果XWPfDocument是从模板创建的,则该模板必须已经包含命名样式“超链接”。这意味着,它必须包含/word/styles.xml在潜在样式的条目中...<w:latentStyles......&nbsp;<w:lsdException w:name="Hyperlink" w:qFormat="1"/>...以及样式定义...<w:style w:type="character" w:styleId="Hyperlink">&nbsp;<w:name w:val="Hyperlink"/>&nbsp;<w:basedOn w:val="..."/>&nbsp;<w:uiPriority w:val="99"/>&nbsp;<w:unhideWhenUsed/>&nbsp;<w:qFormat/>&nbsp;<w:rsid w:val="00072FE4"/>&nbsp;<w:rPr>&nbsp; <w:color w:val="0000FF" w:themeColor="hyperlink"/>&nbsp; <w:u w:val="single"/>&nbsp;</w:rPr></w:style>...如果这是真的,那么以下代码适用于我使用apache poi 4.0.0:import java.io.FileInputStream;import java.io.FileOutputStream;import org.apache.poi.xwpf.usermodel.*;import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHyperlink;public class CreateWordStyledHyperlinkRunFromTemplate {&nbsp;static XWPFHyperlinkRun createHyperlinkRun(XWPFParagraph paragraph, String uri) throws Exception {&nbsp; String rId = paragraph.getPart().getPackagePart().addExternalRelationship(&nbsp; &nbsp; uri,&nbsp;&nbsp; &nbsp; XWPFRelation.HYPERLINK.getRelation()&nbsp; &nbsp;).getId();&nbsp; CTHyperlink cthyperLink=paragraph.getCTP().addNewHyperlink();&nbsp; cthyperLink.setId(rId);&nbsp; cthyperLink.addNewR();&nbsp; return new XWPFHyperlinkRun(&nbsp; &nbsp; cthyperLink,&nbsp; &nbsp; cthyperLink.getRArray(0),&nbsp; &nbsp; paragraph&nbsp; &nbsp;);&nbsp;}&nbsp;public static void main(String[] args) throws Exception {&nbsp; XWPFDocument document = new XWPFDocument(new FileInputStream("Template.docx"));&nbsp; XWPFParagraph paragraph = document.createParagraph();&nbsp; XWPFRun run = paragraph.createRun();&nbsp; run.setText("This is a text paragraph having a link to Google ");&nbsp; XWPFHyperlinkRun hyperlinkrun = createHyperlinkRun(paragraph, "https://www.google.de");&nbsp; hyperlinkrun.setText("https://www.google.de");&nbsp; XWPFStyles styles = document.getStyles();&nbsp; if (styles.styleExist("Hyperlink")) {&nbsp; &nbsp;System.out.println("Style Hyperlink exists."); //Template must contain named style "Hyperlink" already&nbsp; &nbsp;hyperlinkrun.setStyle("Hyperlink");&nbsp; } else {&nbsp; &nbsp;hyperlinkrun.setColor("0000FF");&nbsp; &nbsp;hyperlinkrun.setUnderline(UnderlinePatterns.SINGLE);&nbsp; }&nbsp; run = paragraph.createRun();&nbsp; run.setText(" in it.");&nbsp; FileOutputStream out = new FileOutputStream("CreateWordStyledHyperlinkRunFromTemplate.docx");&nbsp; document.write(out);&nbsp; out.close();&nbsp; document.close();&nbsp;}}请注意,除了使用低级类之外,不可能创建XWPFHyperlinkRunorg.openxmlformats.schemas.wordprocessingml.x2006.main.CTHyperlink。它产生:
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java