i try to add a hyperlink into a paragraph but it does not work.
This is my Code:
- Code: Select all
private P.Hyperlink createHyperlink(String target, String text)
{
//... creating Relationship with addRelationship() ...
P.Hyperlink hyperlink = this.objectFactory.createPHyperlink();
hyperlink.setId(relationship.getId());
R run = this.objectFactory.createR();
RPr runProperties = this.objectFactory.createRPr();
RStyle styleValue = this.objectFactory.createRStyle();
styleValue.setVal("Hyperlink");
runProperties.setRStyle(styleValue);
run.setRPr(runProperties);
Text xmlText = this.objectFactory.createText();
xmlText.setValue(text);
run.getRunContent().add(xmlText);
hyperlink.getParagraphContent().add(run);
return hyperlink;
}
- Code: Select all
P.Hyperlink hyperlink = this.createHyperlink("destination.docx", "text");
//create paragraph and add the hyperlink
paragraph = this.objectFactory.createP();
paragraph.getParagraphContent().add(hyperlink);
//adding the paragraph
wordMLPackage.getMainDocumentPart().addObject(paragraph);
The code is throwing the followed exception:
- Code: Select all
javax.xml.bind.MarshalException
- with linked exception:
[com.sun.istack.internal.SAXException2: unable to marshal type "org.docx4j.wml.P$Hyperlink" as an element because it is missing an @XmlRootElement annotation]
If I try another workarround like this http://dev.plutext.org/trac/docx4j/browser/trunk/docx4j/src/main/java/org/docx4j/samples/HyperlinkTest.java example, this exception will be thrown:
- Code: Select all
javax.xml.bind.UnmarshalException: unexpected element (uri:"http://schemas.openxmlformats.org/wordprocessingml/2006/main", local:"hyperlink"). Expected elements are <{urn:schemas-microsoft-com:office:office}OLEObject>,...
Can anybody help?