Getting text style from docx using Apache poi

后端 未结 6 1380
清酒与你
清酒与你 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:30

    Here is a very good way to copy styles from another document. A little background; a docx file is really a zip file of a number of xml files including styles.xml. In the following code sample I read numberin.xml, parse it into a CTStyles object then set it in the current document. Here is most of the code. You can use the same approach to copy numbering.xml for your Word numbering.

    // copy an existing style.xml document into this document to get styles
    public void copyStylesFromDocument(String documentFileName) {
        log.debug("fileName " + documentFileName);
        try {
            InputStream is = CertificationReportHelper.getInputStreamFromZipFile(documentFileName, FILE_NAME_STYLES);
            CTStyles ctStyle = CTStyles.Factory.parse(is);
            XWPFStyles styles = getDoc().createStyles();
            styles.setStyles(ctStyle);
            log.info("Styles copied from file " + FILE_NAME_STYLES + " in document" + documentFileName);
        } catch (Exception e) {
            String msg = "Error copying styles from file " + FILE_NAME_STYLES + " in document" + documentFileName;
            addErrorMessage(msg, e);
            log.debug(e, e);
        }
        @SuppressWarnings("resource") // closing stream causes input stream to close and operation fails
    public static InputStream getInputStreamFromZipFile(String zipFileName, String containedFile) {
        InputStream is = null;
        ZipFile zfile = null;
        try {
            zfile = new ZipFile(zipFileName);
            ZipEntry entry = zfile.getEntry(containedFile);
            log.trace(entry);
            if (entry != null) {
                is = zfile.getInputStream(entry);
                log.trace("created input stream  for file " + containedFile + " from zip file" + zipFileName);
            } else {
                String msg = "Error getting input stream for file " + containedFile + " from zip file " + zipFileName;
                // closing stream causes input stream to close and operation fails
                throw new ApplicationRuntimeException(msg);
            }
        } catch (Exception e) {
            String msg = "Error getting input stream for file " + containedFile + " from zip file " + zipFileName + "  Message:"
                    + e.getMessage();
            log.warn("*** Throwing exception " + msg);
            throw new ApplicationRuntimeException(msg, e);
        } finally {
            // closing stream causes input stream to close and operation fails
            // try {
            // zfile.close();
            // } catch (IOException e) {
            // log.warn("Catching exception "+e+" closing zip file "+zipFileName);
            // }
        }
        return is;
    

提交回复
热议问题