简体   繁体   English

docx4j添加样式到段破坏文档

[英]docx4j adding style to paragraph destroys document

I'm trying to add a paragraph containing a headline (with style) and some plain unformatted text. 我正在尝试添加一个包含标题(带样式)和一些简单无格式文本的段落。 The following code destroys the document. 以下代码销毁文档。

Edit: After executing the following code and trying to open the document in word I get an error message "Unspecified Error" Location: Part /word/document.xml Line 1 Column 0 编辑:执行以下代码并尝试在单词中打开文档后,我收到错误消息“未指定的错误”位置:部分/word/document.xml第1行第0列

        ObjectFactory factory = new ObjectFactory();
        P complete = factory.createP();

        org.docx4j.wml.P headline=factory.createP();
        R hrun = factory.createR();
        Text htxt = new Text();
        hrun.getContent().add(htxt);
        htxt.setValue(View_Beta.this.falseAlarmChoice.getSelectedItem().toString());

        headline.getContent().add(hrun);        



    org.docx4j.wml.PPr  pPr = factory.createPPr();
        headline.setPPr(pPr);
        org.docx4j.wml.PPrBase.PStyle pStyle = factory.createPPrBasePStyle();
        pPr.setPStyle(pStyle);
        pStyle.setVal("Title"); 
        complete.getContent().add(headline);

        P ptext = factory.createP();
        R rtext = factory.createR();
        Text ttext = new Text();
        rtext.getContent().add(ttext);
        ptext.getContent().add(rtext);

        ttext.setValue(falseAlarmChoice.getSelectedItem()
                + falseAlarmDsc.getText());
        complete.getContent().add(ptext);

        //add to document context
        View_Beta.this.c.insertAtPos(complete,
                paragraphlst.getSelectedIndex());

In your code, 在你的代码中,

complete.getContent().add(headline) 

adds a paragraph inside a paragraph, which is invalid per the Open XML spec. 在段落中添加一个段落,根据Open XML规范,该段落无效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM