Page 1 of 1

Pb creating image as hyperlink

PostPosted: Sun Jan 30, 2011 3:17 am
by myjupiler
My image creation works fine. I'm trying to add a hyperlink on it.
The target xml in Word is :

<w:drawing>
<wp:inline distT="0" distB="0" distL="0" distR="0">
<wp:extent cx="1800000" cy="466394"/><wp:effectExtent l="19050" t="0" r="0" b="0"/>
<wp:docPr id="9999" name="ABC.png" descr="ABC">
<a:hlinkClick xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" r:id="rId7"/>
</wp:docPr>

I have used the Hyperlink sample to do this :


Inline inline = img.createImageInline("ABC.png", "ABC", 9999, 9998, false);
...
inline.getDocPr().setHlinkClick(createImageHyperLink(url));
...


public CTHyperlink createImageHyperLink(String url) throws JAXBException {

//add relationship to document
ObjectFactory factory = new ObjectFactory();
Relationship newRel = factory.createRelationship();
newRel.setType(Namespaces.HYPERLINK);
newRel.setTarget(url);
newRel.setTargetMode("External");

pack.getMainDocumentPart().getRelationshipsPart().addRelationship(newRel);

String hls = "<a:hlinkClick xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\" r:id=\"" +
newRel.getId() +
"\"/>";

return (CTHyperlink) XmlUtils.unmarshalString(hls);

}



The unmarshal operation gives the the following error :

2011-01-29 16:39:11,403 INFO [org.docx4j.jaxb.JaxbValidationEventHandler] continuing (with possible element/attribute loss) (http-0.0.0.0-8080-1:)
2011-01-29 16:39:11,403 ERROR [STDERR] javax.xml.bind.UnmarshalException
- with linked exception:
[org.xml.sax.SAXParseException: The prefix "r" for attribute "r:id" associated with an element type "a:hlinkClick" is not bound.] (http-0.0.0.0-8080-1:)
2011-01-29 16:39:11,404 ERROR [STDERR] at javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(AbstractUnmarshallerImpl.java:315) (http-0.0.0.0-8080-1:)
2011-01-29 16:39:11,404 ERROR [STDERR] at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.createUnmarshalException(UnmarshallerImpl.java:514) (http-0.0.0.0-8080-1:)
2011-01-29 16:39:11,404 ERROR [STDERR] at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:215) (http-0.0.0.0-8080-1:)
2011-01-29 16:39:11,404 ERROR [STDERR] at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:184) (http-0.0.0.0-8080-1:)
2011-01-29 16:39:11,404 ERROR [STDERR] at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:137) (http-0.0.0.0-8080-1:)
2011-01-29 16:39:11,404 ERROR [STDERR] at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:105) (http-0.0.0.0-8080-1:)
2011-01-29 16:39:11,404 ERROR [STDERR] at org.docx4j.XmlUtils.unmarshalString(XmlUtils.java:239) (http-0.0.0.0-8080-1:)
2011-01-29 16:39:11,404 ERROR [STDERR] at org.docx4j.XmlUtils.unmarshalString(XmlUtils.java:219) (http-0.0.0.0-8080-1:)

Re: Pb creating image as hyperlink

PostPosted: Sun Jan 30, 2011 4:58 am
by myjupiler
Making progress. I added the xmlns for r:id.

Now I get this one :

ERROR [STDERR] javax.xml.bind.UnmarshalException: unexpected element (uri:"http://schemas.openxmlformats.org/drawingml/2006/main", local:"hlinkClick")

Re: Pb creating image as hyperlink

PostPosted: Sun Jan 30, 2011 5:07 am
by myjupiler
Trying to replace

xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"
by
xmlns:a="http://schemas.openxmlformats.org/wordprocessingml/2006/main"

I get this error :
java.lang.ClassCastException: org.docx4j.wml.P$Hyperlink cannot be cast to org.docx4j.dml.CTHyperlink

Re: Pb creating image as hyperlink

PostPosted: Mon Jan 31, 2011 11:58 pm
by jason
try:
Code: Select all
         String hpl = "<a:hlinkClick r:id=\"rId4\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\" />";
         org.docx4j.dml.CTHyperlink hl = (org.docx4j.dml.CTHyperlink)XmlUtils.unmarshalString(hpl, Context.jc, org.docx4j.dml.CTHyperlink.class);

Re: Pb creating image as hyperlink

PostPosted: Wed Feb 02, 2011 8:27 pm
by myjupiler
Works great, thanks !

Can you explain me basically how this works ?