I'm trying to find a solution to add hyperlink to a paragraph.
When I do something, like this:
- Code: Select all
...
String hpl = "<w:p xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" " +
"xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" >" +
"<w:hyperlink r:id=\"rId" + counter + "\">" +
"<w:r>" +
"<w:rPr>" +
"<w:rStyle w:val=\"Title\" />" +
"</w:rPr>" +
"<w:t>Link</w:t>" +
"</w:r>" +
"</w:hyperlink>" +
"</w:p>";
// wmlPack is WordprocessingMLPackage
wmlPack.getMainDocumentPart().addObject(XmlUtils.unmarshalString(hpl));
, it works fine, but if I need insert hyperlink into existing paragraph:
- Code: Select all
String hpl2 = "<w:hyperlink xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" r:id=\"rId" + counter + "\">" +
"<w:r>" +
"<w:rPr>" +
"<w:rStyle w:val=\"Title\" />" +
"</w:rPr>" +
"<w:t>Odkaz</w:t>" +
"</w:r>" +
"</w:hyperlink>";
// wmlFactory is org.docx4j.wml.ObjectFactory
org.docx4j.wml.P paragraph = wmlFactory.createP();
paragraph.getParagraphContent().add(XmlUtils.unmarshalString(hpl2));
wmlPack.getMainDocumentPart().addObject(paragraph);
, I obtain following Exception:
- Code: Select all
javax.xml.bind.UnmarshalException: unexpected element (uri:"http://schemas.openxmlformats.org/wordprocessingml/2006/main", local:"hyperlink"). Expected elements are <{http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing}anchor>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}annotationRef>, ...
Please, exist any way, how to do this (I want not create new paragraph, I need add hyperlink to an existing paragraph)?
Thanks for any advice.
Bob.