Getting text style from docx using Apache poi

后端 未结 6 1379
清酒与你
清酒与你 2021-01-14 02:41

I\'m trying to get the style information from an MS docx file, I have no problem writing file content with added styles like bold, italic. font size etc, but reading the fil

6条回答
  •  无人及你
    2021-01-14 03:10

    I found a very nice way to copy styles from one document to another. It is not as direct as I would have hoped but it works.

    1. Rename the source word document to type zip
    2. Extract the contents
    3. Copy styles.xml into a string constant or read the file
    4. Copy the styles into your output document with the following code

      public void copyStylesXml(String stylesXmlString) {
         try {
            CTStyles ctStyle = CTStyles.Factory.parse(stylesXmlString);
            XWPFStyles styles = getDoc().createStyles();
            styles.setStyles(ctStyle);
         } catch (Exception e) {
            log.warn(e, e);
         }
      }
      

    The same approach works for copying list formats

提交回复
热议问题