Page 1 of 1

Hyperlink

PostPosted: Tue Oct 21, 2008 12:44 pm
by jecmenb
HI everybody.

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.

Re: Hyperlink

PostPosted: Wed Oct 22, 2008 2:02 am
by jason
Hi Bob

Thanks for the report; JAXB didn't know what to do with the hyperlink element. I've fixed that in SVN by adding @XmlRootElement to the CTHyperlink class.

Pls note that in due course, we will rename that class to more human friendly Hyperlink

I've also refactored the code for adding relationships slightly, so that an external part (like a hyperlink) gets an @id allocated to it as well.

[Edited 2008 11 14]See *CLICK HERE* for an example which includes adding the hyperlink to the rels part.

You'll need to use current SVN to get these changes.

cheers

Jason

Re: Hyperlink

PostPosted: Wed Oct 22, 2008 10:31 am
by jecmenb
HI Jason.
Thank you very much for quick response.

I coocked up other very simply solving:
Code: Select all
org.docx4j.wml.P paragraph0 = new org.docx4j.wml.P();
            paragraph0 = (org.docx4j.wml.P)XmlUtils.unmarshalString(str); // existing paragraph
           
            org.docx4j.wml.P paragraph = new org.docx4j.wml.P();
            paragraph = (org.docx4j.wml.P)XmlUtils.unmarshalString(hpl); // paragraph with hyperlink
           
            paragraph0.getParagraphContent().addAll(paragraph.getParagraphContent()); // merge both paragraphs


But your fix is obviously better, because after that works CTHyperlink class. ;-)

Relationships (if I am linking external files) I create:
Code: Select all
if (wmlPack.getMainDocumentPart().getRelationshipsPart() != null) {
         this.counter = wmlPack.getMainDocumentPart().getRelationshipsPart().size() + 1;
      } else {
         this.counter += 1;
      }
      org.docx4j.relationships.ObjectFactory factory = new org.docx4j.relationships.ObjectFactory();
      Relationship rel = factory.createRelationship();

      rel.setId("rId" + this.counter);
      rel.setType("http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink");
      rel.setTarget(file);
      rel.setTargetMode("External");

      wmlPack.getMainDocumentPart().getRelationshipsPart().addRelationship(rel);


Cheers, Bob.