Page 1 of 1

Problem inserting "internal" hyperlink

PostPosted: Tue Apr 19, 2011 1:06 am
by guams
Hi,

I have to add internal hyperlink (a reference to a bookmark) to a document but I don't know how to do.

I try to change the HyperlinkTest Sample, removing the instruction
Code: Select all
rel.setTargetMode("External");


But I had an exception: org.docx4j.openpackaging.exceptions.Docx4JException: Failed to add parts from relationships


I also tried to load a document which contains "internal hyperlink" with the OpenAndTraverseDocument Sample and I had the following exception:
Code: Select all
java.lang.NullPointerException
        at org.docx4j.openpackaging.io.LoadFromZipNG.getInputStreamFromZippedPart(LoadFromZipNG.java:305)
        at org.docx4j.openpackaging.io.LoadFromZipNG.getRawPart(LoadFromZipNG.java:508)
        at org.docx4j.openpackaging.io.LoadFromZipNG.getPart(LoadFromZipNG.java:422)
        at org.docx4j.openpackaging.io.LoadFromZipNG.addPartsFromRelationships(LoadFromZipNG.java:351)
        at org.docx4j.openpackaging.io.LoadFromZipNG.getPart(LoadFromZipNG.java:444)
        at org.docx4j.openpackaging.io.LoadFromZipNG.addPartsFromRelationships(LoadFromZipNG.java:351)
        at org.docx4j.openpackaging.io.LoadFromZipNG.process(LoadFromZipNG.java:236)
        at org.docx4j.openpackaging.io.LoadFromZipNG.get(LoadFromZipNG.java:189)
        at org.docx4j.openpackaging.packages.OpcPackage.load(OpcPackage.java:188)
        at org.docx4j.openpackaging.packages.WordprocessingMLPackage.load(WordprocessingMLPackage.java:169)
        at test.OpenMainDocumentAndTraverse.main(OpenMainDocumentAndTraverse.java:58)
Exception in thread "main" org.docx4j.openpackaging.exceptions.Docx4JException: Failed to add parts from relationships
        at org.docx4j.openpackaging.io.LoadFromZipNG.addPartsFromRelationships(LoadFromZipNG.java:353)
        at org.docx4j.openpackaging.io.LoadFromZipNG.process(LoadFromZipNG.java:236)
        at org.docx4j.openpackaging.io.LoadFromZipNG.get(LoadFromZipNG.java:189)
        at org.docx4j.openpackaging.packages.OpcPackage.load(OpcPackage.java:188)
        at org.docx4j.openpackaging.packages.WordprocessingMLPackage.load(WordprocessingMLPackage.java:169)
        at  test.OpenMainDocumentAndTraverse.main(OpenMainDocumentAndTraverse2.java:58)
Caused by: org.docx4j.openpackaging.exceptions.Docx4JException: Failed to add parts from relationships
        at org.docx4j.openpackaging.io.LoadFromZipNG.addPartsFromRelationships(LoadFromZipNG.java:353)
        at org.docx4j.openpackaging.io.LoadFromZipNG.getPart(LoadFromZipNG.java:444)
        at org.docx4j.openpackaging.io.LoadFromZipNG.addPartsFromRelationships(LoadFromZipNG.java:351)
        ... 5 more
Caused by: org.docx4j.openpackaging.exceptions.Docx4JException: Failed to getPart
        at org.docx4j.openpackaging.io.LoadFromZipNG.getRawPart(LoadFromZipNG.java:642)
        at org.docx4j.openpackaging.io.LoadFromZipNG.getPart(LoadFromZipNG.java:422)
        at org.docx4j.openpackaging.io.LoadFromZipNG.addPartsFromRelationships(LoadFromZipNG.java:351)
        ... 7 more
Caused by: java.lang.NullPointerException
        at org.docx4j.openpackaging.io.LoadFromZipNG.getInputStreamFromZippedPart(LoadFromZipNG.java:305)
        at org.docx4j.openpackaging.io.LoadFromZipNG.getRawPart(LoadFromZipNG.java:508)
        ... 9 more


Is it a bug or a feature not supported?

Re: Problem inserting "internal" hyperlink

PostPosted: Tue Apr 19, 2011 1:13 am
by jason
guams wrote:also tried to load a document which contains "internal hyperlink" with the OpenAndTraverseDocument Sample and I had the following exception


You should never get an error loading a valid docx...

What version of docx4j are you using - 2.6.0 or the latest nightly, or something else?

Can you upload the docx and I will take a look?

Re: Problem inserting "internal" hyperlink

PostPosted: Tue Apr 19, 2011 2:09 am
by guams
Ok, I cannot share the docx I'm working on, but I reproduce the bug with another small document.

I'm using docx4j 2.6.0.

Re: Problem inserting "internal" hyperlink

PostPosted: Tue Apr 19, 2011 9:49 am
by jason
I'm a bit surprised that has never come up before, but in any case it is now fixed as http://dev.plutext.org/trac/docx4j/changeset/1481

I will make a nightly build tonight.

To answer your original question properly, are you trying to add your internal hyperlink from an image as per this example docx, or from text?

Re: Problem inserting "internal" hyperlink

PostPosted: Tue Apr 19, 2011 10:33 am
by guams
From an image.

Re: Problem inserting "internal" hyperlink

PostPosted: Tue Apr 19, 2011 9:03 pm
by guams
Hi Jason

With the nightly build, I still have an exception:

java.io.IOException: part 'word/document.xml#toto' not found
etc...

Re: Problem inserting "internal" hyperlink

PostPosted: Tue Apr 19, 2011 9:19 pm
by jason
I haven't made a nightly build yet :-)

ok ... see now http://dev.plutext.org/docx4j/docx4j-ni ... 110419.jar

Re: Problem inserting "internal" hyperlink

PostPosted: Wed Apr 20, 2011 3:23 am
by guams
Ok, it works now.... but I still don't know how to add hyperlink from an image.

Re: Problem inserting "internal" hyperlink

PostPosted: Wed Apr 20, 2011 10:16 pm
by jason
Here is a sketch of how to do it.

You are looking to add hlinkClick to wp:docPr:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
            <wp:docPr id="2" name="Image 1" descr="Coucher de soleil.jpg">
              <a:hlinkClick xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" r:id="rId4"/>
            </wp:docPr>
 
Parsed in 0.001 seconds, using GeSHi 1.0.8.4

in

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
        <w:drawing>
          <wp:inline distT="0" distB="0" distL="0" distR="0">
 
Parsed in 0.000 seconds, using GeSHi 1.0.8.4


Where your r:id is an internal hyperlink.

First the code from HyperlinkTest sample:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
                        org.docx4j.relationships.ObjectFactory factory =
                                new org.docx4j.relationships.ObjectFactory();
                       
                        org.docx4j.relationships.Relationship rel = factory.createRelationship();
                        rel.setType( Namespaces.HYPERLINK  );
                        rel.setTarget(url);
                        rel.setTargetMode("Internal");  
                                                                       
                        wordMLPackage.getMainDocumentPart().getRelationshipsPart().addRelationship(rel);
 
Parsed in 0.015 seconds, using GeSHi 1.0.8.4


Assuming org.docx4j.dml.wordprocessingDrawing.Inline inline,

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
CTNonVisualDrawingProps docPr = inline.getDocPr();

org.docx4j.dml.ObjectFactory dmlOF = org.docx4j.dml.ObjectFactory.ObjectFactory();
org.docx4j.dml.CTHyperlink hyperlink = dmlOF.createCTHyperlink();

docPr.setHlinkClick(hyperlink);

hyperlink.setId(rel.getId() );

 
Parsed in 0.013 seconds, using GeSHi 1.0.8.4


Please confirm this works and/or post for the benefit of others a version with whatever tweaks you find necessary :-)

Re: Problem inserting "internal" hyperlink

PostPosted: Thu Apr 21, 2011 9:58 pm
by guams
Hi Jason,

I try your code but it don't work for me, I have the following exception:

org.docx4j.openpackaging.exceptions.Docx4JException: Failed to add parts from relationships
at org.docx4j.openpackaging.io.SaveToZipFile.addPartsFromRelationships(SaveToZipFile.java:330)
at org.docx4j.openpackaging.io.SaveToZipFile.save(SaveToZipFile.java:158)
at org.docx4j.openpackaging.io.SaveToZipFile.save(SaveToZipFile.java:99)
at org.docx4j.openpackaging.packages.WordprocessingMLPackage.save(WordprocessingMLPackage.java:204)
.....
Caused by: org.docx4j.openpackaging.exceptions.Docx4JException: Failed to add parts from relationships
at org.docx4j.openpackaging.io.SaveToZipFile.addPartsFromRelationships(SaveToZipFile.java:330)
at org.docx4j.openpackaging.io.SaveToZipFile.savePart(SaveToZipFile.java:377)
at org.docx4j.openpackaging.io.SaveToZipFile.addPartsFromRelationships(SaveToZipFile.java:325)
... 8 more
Caused by: java.lang.NullPointerException
at org.docx4j.openpackaging.io.SaveToZipFile.savePart(SaveToZipFile.java:349)
at org.docx4j.openpackaging.io.SaveToZipFile.addPartsFromRelationships(SaveToZipFile.java:325)
... 10 more

Re: Problem inserting "internal" hyperlink

PostPosted: Fri Apr 22, 2011 3:28 pm
by jason
Just committed http://dev.plutext.org/trac/docx4j/changeset/1482 which should fix this.

I'll make a nightly later .. will edit this post to say so when done.

Re: Problem inserting "internal" hyperlink

PostPosted: Tue Apr 26, 2011 8:38 pm
by guams
Hi Jason,

When do you think a nightly build for this feature will be available?

regards

Re: Problem inserting "internal" hyperlink

PostPosted: Tue Apr 26, 2011 8:55 pm
by jason
Sorry, Easter got in the way.

Here it is: http://dev.plutext.org/docx4j/docx4j-ni ... 110426.jar

cheers .. Jason

Re: Problem inserting "internal" hyperlink

PostPosted: Fri Apr 29, 2011 9:19 pm
by guams
Ok, it works fine now.

Thanks for the help and quick support :)

Re: Problem inserting "internal" hyperlink

PostPosted: Sat Jan 28, 2012 6:07 am
by nicotejera
Hi Jason, I have a problem related to this. I can create external hyperlinks no problem. I tried creating hyperlinks by setting the target mode to "Internal". The code compiles and runs just fine, but when I try to open the document it says it's corrupt. If I tell word to open in anyway, it opens just fine and even the links work ok, they go to the bookmark and everything. Any ideas?

Thanks, Nicolas

Re: Problem inserting "internal" hyperlink

PostPosted: Wed Feb 01, 2012 6:39 pm
by jason
You haven't given me much to go on :-)

What does your code look like? Are you sure it is the hyperlink stuff which is the problem, and not something else you are also doing to the docx file?

Re: Problem inserting "internal" hyperlink

PostPosted: Thu Mar 15, 2012 4:58 am
by UndefienedUser
Hi Jason,

Is it the same process to add an internal hyperlink from text?

thanks

Re: Problem inserting "internal" hyperlink

PostPosted: Fri Apr 06, 2012 8:37 pm
by UndefienedUser
Hi, No idea ...?

Re: Problem inserting "internal" hyperlink

PostPosted: Wed Apr 11, 2012 1:11 pm
by jason
This whole thread is confusing/confused, because usually you don't need a rel at all for an internal hyperlink.

Please see http://www.docx4java.org/blog/2012/04/d ... yperlinks/

Re: Problem inserting "internal" hyperlink

PostPosted: Wed Apr 11, 2012 11:54 pm
by UndefienedUser
Thanks a lot i got it!!