Page 1 of 1

Help with Hyperlink

PostPosted: Tue Nov 16, 2010 3:19 am
by sperruolo
Hi, I'm trying to modify a Hyperlink from a docx document. I found that Hyperlink class give me this methods:
    h.getAnchor()
    h.getTooltip()
    h.getDocLocation()
    h.getTgtFrame()
The problem is that none of them let me get the URL Address.

There's any way to get/set the Hyperlink's address?

Re: Help with Hyperlink

PostPosted: Tue Nov 16, 2010 8:52 am
by jason
P.Hyperlink has get|setId, which points to a relationship. The rel's target contains the URL.

Code: Select all
       String relId = hyperlink.getId();
       Relationship rel = docxPkg.getMainDocumentPart().getRelationshipsPart().getRelationshipByID(relId);


then use rel.get|setTarget.

Re: Help with Hyperlink

PostPosted: Fri Dec 03, 2010 9:27 am
by sperruolo
Thank you! I'm able now to edit Hyperlink just the way I wanted.

PS: There's some protocol I must follow to close this topic?

Re: Help with Hyperlink

PostPosted: Thu Apr 26, 2012 10:46 pm
by IgorShevchenko
Dear Jason,

I have a problem with rel.get|setTarget.

I have template docx with table and there I have one row and 4 cells with image, text and hyperlink.
Than I copy this row few times with following code:

Tbl finishedTable2 = (Tbl) XmlUtils.unwrap(deepCopyDoc.getMainDocumentPart().getJaxbElement().getBody().getContent().get(0));
Tr finishedTableRow2 = (Tr) finishedTable2.getContent().get(0);

Tr copiedRow = XmlUtils.deepCopy(finishedTableRow2);
for(int i=0; i<10; i++){
finishedTable2.getContent().add(copiedRow);

When I have needed row numbers I replace text and it works fine.
After that I try to replace hyperlinks but all of them are replaced with one value, I need for each hyperlink different value the same as text replacing.
Code with hyperlink replacing:
WordprocessingMLPackage template1 = WordprocessingMLPackage.load(new File(templateFilePath));
Tbl finishedTable2 = (Tbl) XmlUtils.unwrap(template1.getMainDocumentPart().getJaxbElement().getBody().getContent().get(0));

Tr finishedTableRow2 = (Tr) finishedTable2.getContent().get(2); // взять вторую строку
//--------
Tc finishedTableRowCell2 = (Tc) XmlUtils.unwrap(finishedTableRow2.getContent().get(1)); // взять вторую ячейку

P finishedTableRowCellParagraph = (P)finishedTableRowCell2.getContent().get(0);
P.Hyperlink hyperLink = (P.Hyperlink) XmlUtils.unwrap(finishedTableRowCellParagraph.getContent().get(0));
R r = (R) hyperLink.getContent().get(0);
Text t = (Text) XmlUtils.unwrap(r.getContent().get(0));
System.out.println(t.getValue());

String relId = hyperLink.getId();
System.out.println(relId.toString());
Relationship rel = template1.getMainDocumentPart().getRelationshipsPart().getRelationshipByID(relId);
System.out.println(rel.getId());
rel.setTarget(listColumn.get(0).getTaskKey().getHref());

Re: Help with Hyperlink

PostPosted: Thu Apr 26, 2012 10:54 pm
by jason
For each new hyperlink, you need to add a rel. See the HyperlinkTest sample for details, but basically, its:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting

                        org.docx4j.relationships.Relationship rel = factory.createRelationship();
                        rel.setType( Namespaces.HYPERLINK  );
                        rel.setTarget(url);
                        rel.setTargetMode("External");  
                                                                       
                        wordMLPackage.getMainDocumentPart().getRelationshipsPart().addRelationship(rel);
                       
 
Parsed in 0.015 seconds, using GeSHi 1.0.8.4


and then hyperlink.setId(rel.getId())

ps please use
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
 ...
Parsed in 0.013 seconds, using GeSHi 1.0.8.4
around code snippets in your posts, so they are easier to read.

Re: Help with Hyperlink

PostPosted: Thu Apr 26, 2012 11:26 pm
by IgorShevchenko
Jason,

Thanks for fast reply.

But I is not clear form where I need add a rel.

Should I add rel when I copy row or I need to add rel when I replace hyperlink?

Best Regards,
Igor

Re: Help with Hyperlink

PostPosted: Thu Apr 26, 2012 11:43 pm
by jason
You add the rel to the main document part's relationships, if the hyperlink is in that part. Otherwise, header part etc.

You add the rel id to the hyperlink itself.

Re: Help with Hyperlink

PostPosted: Fri Apr 27, 2012 12:12 am
by IgorShevchenko
Dear Jason,

Thanks for your help!!!

It works now!

And my code is:

Code: Select all
WordprocessingMLPackage template1 = WordprocessingMLPackage.load(new File(templateFilePath));
Tbl finishedTable2 = (Tbl) XmlUtils.unwrap(template1.getMainDocumentPart().getJaxbElement().getBody().getContent().get(0));

Tr finishedTableRow2 = (Tr) finishedTable2.getContent().get(2); // взять вторую строку
//--------
Tc finishedTableRowCell2 = (Tc) XmlUtils.unwrap(finishedTableRow2.getContent().get(1)); // взять вторую ячейку

P finishedTableRowCellParagraph = (P)finishedTableRowCell2.getContent().get(0);
P.Hyperlink hyperLink = (P.Hyperlink) XmlUtils.unwrap(finishedTableRowCellParagraph.getContent().get(0));
R r = (R) hyperLink.getContent().get(0);
Text t = (Text) XmlUtils.unwrap(r.getContent().get(0));
System.out.println(t.getValue());

org.docx4j.relationships.ObjectFactory factory = new org.docx4j.relationships.ObjectFactory();         
          org.docx4j.relationships.Relationship rel = factory.createRelationship();
         rel.setType( Namespaces.HYPERLINK  );
         rel.setTarget("http://MYLINK");
         rel.setTargetMode("External");            
         template1.getMainDocumentPart().getRelationshipsPart().addRelationship(rel);
         String relId = rel.getId();        
         hyperLink.setId(relId);