0

I have an application built on Spring MVC that reads a MSWord DOCX with Apache POI and returns it to a HTML Thymeleaf web page. But I am not able to maintain the text formatting styles like bold, italic, font-color, font-size, etc.

The Spring Controller method returns a ModelAndView with a variable called docDetail loaded with a XWPFDocument object from loadResource().

[...]
@GetMapping("/document")
public ModelAndView document() {
    [...]

    modelAndView.addObject("docDetail", fileService.loadResource());

    return modelAndView;
}
[...]

The HTML Thymeleaf fills a fragment while iterate over the paragraphs from the document.

[...]
<div th:fragment="doc-detail">
        <div th:each="par : ${docDetail.paragraphs}">
            <p th:text="${par.text}"></p>
        </div>
</div>
[...]

And the result appears as plain text. I have not tried Apache Tika yet. So, How can I keep the styles from the document to the web page? Thanks in advance.

1
  • 1
    Can you post the code for instantiating fileService in the controller? Specifically, the POI parts? Commented Mar 17, 2019 at 5:34

1 Answer 1

0

I think I see your issue. You are using a XWPFDocument and scanning through the text without regard to style. In POI, font and style is encoded in XWPFRun types which are one level down from paragraph. So you're going to need an extra th:each to loop through the runs and apply the style for each. You code will look something like this:

<div th:fragment="doc-detail">
    <div th:each="par : ${docDetail.paragraphs}">
        <div th:each="run : ${par.runs}">                  
            <span th:if="run.bold"><b th:value="run.text"></b></span>
            <span th:unless="run.italic"><i th:value="run.text"></i></span>                                    
            <span th:unless="1==1" th:value="run.text"></span>                                    
        </div>
    </div>
</div>
Sign up to request clarification or add additional context in comments.

1 Comment

Greetings, now I see the paragraph properties at the "run" atribute. I think each formatting need its own step, I'll try to search some more. Thank you for the insight.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.