My teammate came up with a solution to this problem. The idea is to create elements of the DefaultMutableTreeNode tree. Each of the DefaultMutableTreeNode will contain a PdfStructureElement element. The tree should represent an XML hierarchy, for example, consider a piece of XML code in a previous comment. The first DefaultMutableTreeNode should have a PdfStructureElement (PdfName - movies), the parent of which is writer.getStructureTreeRoot (). The child of this node must be another PdfStructureElement (PdfName - movie) whose parent is a PdfStrucutreElement named 'movies', etc.
As soon as the steps mentioned above are completed (this is essentially parsing), we get the PdfStrucutreElements tree. Now we need to analyze the contents. When we analyze the content, we need to go through each of the nodes in the tree. If the parsed node is a leaf node, then we need to get the PdfStructureElement element inside this node. Else If the parsed node is a non-leaf node, then we need to get the PdfName of the PdfStructureElement element within that node. In other words, we can just use the qName variable.
if (node ββis a leaf)
PdfStructureElement element = (PdfStructureElement) node.getUserObject ();
canvas.beginMarkedContentSequence (element);
else
canvas.beginMarkedContentSequence (qName);
Shriram Kalpathy Mohan
source share