Create a Bullet List in a Text Document Using Java with Apache POI API

I need to have a bullet list in a text document with indentation and custom marks that are generated via Java with the Apache POI API. I searched and I cannot find it for a Word document. It is available for Powerpoint slides using a text box. But I do not want to use a text box. Please let me know the possibilities to achieve it. Any help is greatly appreciated. Thank you Regards, Arun Ganesh. R

+6
source share
2 answers

This is really only possible in the OOXML 2007 format and above (using the XWPF POI). Since it is based on XML, you can always manipulate the DOM to achieve what you want. The easiest way to make markers is to create a list of markers and add a bookmark as the text of this bullet. When processing the document, find your bookmark, then get the DOM node using

`org.w3c.dom.Node bkmk = bookmark.getctBookmark).getDomNode();` 

Then copy the parent element of bkmk node, which is the paragraph tag. You now have a repeating paragraph tag in which all the necessary child tags are part of the bullet list. See, OOXML does not have a bullet list structure, it's just a sequential continuation of paragraphs that have similar swap tags.

 <w:p> <w:pPr> <w:pStyle w:val="style0" /> <w:numPr> <w:ilvl w:val="1" /> <w:numId w:val="2" /> </w:numPr> <w:tabs> <w:tab w:leader="none" w:pos="1807" w:val="left" /> </w:tabs> <w:spacing w:after="0" w:before="120" /> <w:ind w:end="907" w:hanging="360" w:start="907" /> <w:jc w:val="both" /> </w:pPr> <w:bookmarkStart w:id="1" w:name="GIVES" /> <w:r> <w:t>To be inserted Next Bullet</w:t> </w:r> <w:bookmarkEnd w:id="1" /> <w:r> <w:rPr> <w:rFonts w:eastAsia="Times New Roman" /> <w:color w:val="000000" /> <w:lang w:eastAsia="en-US" /> </w:rPr> </w:r> </w:p> 

The key tags are and, and I'm not very familiar with the XML values โ€‹โ€‹of the tags, but if you unzip any docx document and look at document.xml (after formatting it, of course), you can see the differences between paragraphs with and without numbering .

So, as soon as you have a clone of your tag, you can cross the node using the DOM to get and replace the value of the node with what you want, or you can use xpath to search for the node (w: r / w: t). You must set the NamespaceContext and give it the correct code to understand the w prefix:

  NodeList nl; XPath xp = XPathFactory.newInstance().newXPath(); NamespaceContext nsContext = new NamespaceContext(){ @Override public String getNamespaceURI(String prefix) { if (prefix.equals("w")) { return "http://schemas.openxmlformats.org/wordprocessingml/2006/main"; } return null; } @Override public String getPrefix(String namespaceURI) { return null; } @Override public Iterator<?> getPrefixes(String namespaceURI) { return Collections.emptyList() .iterator(); } }; xp.setNamespaceContext(nsContext); nl = (NodeList) xp.evaluate("w:r/w:t", copy, XPathConstants.NODESET); 

Now go to nodeList, setNodeValue ("Hello World"). You can do this after cloning and executing: paragraph.getParentNode().insertBefore(bkmk, paragraph);

to get as many points as you want. If you do

  `paragraph.getParentNode().append(bkmk)` 

your new marker will be at the very end of the document!

So you have to do insertBefore (this is the only dom node function available, other than append). This will result in you having the original empty dot at the end, with a bookmark in it. You need to remove the bookmark using the paragraph.getParentNode () parameter. RemoveChild (paragraph);

Then save the file using the POI.

Basically, the POI does not support bullet lists, because OOXML does not really support bullet lists. Bullet lists are just a run of paragraphs with numbering as children. But with a POI, you can always fall for the basic DOM manipulation, and you can inspect the DOM with unpacking and accuracy.

+2
source

The HWPF POI is an immature api - the leading developer was offered a job that entailed the conclusion of a non-disclosure agreement, and he was forced to refuse to work in it - and it may be impossible to use it to generate the necessary files.

See continued message

+1
source

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


All Articles