0

So I've been trying to make use of some heading styles in Apache POI. These are loaded from a template with the following code:

val document = XWPFDocument(template.inputStream)
val par = document.createParagraph()
par.style = "Heading 1"

Here, template is a Spring Resource.

The styles I am using are confirmed to exist in the template, they are named 'Heading 1', 'Heading 2', etc.

When I apply one of the styles to a paragraph, I am met with disappointing results. The paragraphs with these styles lack the styles whatsoever, even though the styles are still present in the document. The worst part is, that this occurs only in Microsoft Word, LibreOffice has no issues with using the template styles. Regrettably, I am required to use Microsoft Word.

I've found out, that when the template has some text using these styles present, the problem does not occur. These 'style holders' however, cannot be present in the final document, so I cannot use that.

Is there any way to make Microsoft behave properly and use these styles properly?

Result when opened in Microsoft Word

Result when the same file is opened in LibreOffice

I can define my own styles in accordance with solutions to this question: How can I use predefined formats in DOCX with POI? but I have no idea how to create a style with the numbering included, so that is why I've been trying to use the template styles.

Olaf Kock
  • 46,930
  • 8
  • 59
  • 90

1 Answers1

0

I suppose this isn't an issue many people face, but I wanted to offer a workaround I ended up using.

So I've found, that you do not need to have all the styles used in the template, only one of them was sufficient for word to not delete any of them. So I placed the largest header into the document template, and then used docx4j stamper to make the header have the desired text.

The result works fine, but I suppose it is not a generic solution to this issue.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 05 '22 at 10:48