How to center text in docx4j

I have a paragraph of text that I would like to appear in the center of the document. How can I do this in docx4j? I am currently using:

PPr paragraphProperties = factory.createPPr(); //creating the alignment TextAlignment align = new TextAlignment(); align.setVal("center"); paragraphProperties.setTextAlignment(align); //centering the paragraph paragraph.setPPr(paragraphProperties); 

but it does not work.

+6
source share
1 answer

You are almost there. Instead of setting this with a TextAlignment object, use a Jc instance (excuse):

 PPr paragraphProperties = factory.createPPr(); Jc justification = factory.createJc(); justification.setVal(JcEnumeration.CENTER); paragraphProperties.setJc(justification); 

A simple way to figure this out:

  • Create the document (and formatting) that you are looking for in Microsoft Word and save the file
  • Change the suffix of the .docx file to "zip"
  • Open the zip archive, open the word directory and extract the document.xml file
  • Learn XML that will give you hints as to which OpenXML objects to use
+8
source

Source: https://habr.com/ru/post/970975/


All Articles