Page 1 of 1

Create CTHyperlink

PostPosted: Sun Apr 02, 2023 9:47 pm
by Ruslan
I'm using org.docx4j 8.3.9 and java 11.

Trying to create a Hyperlink to the website on one of my slides.
Code example:
Text t = new Text();

t.setValue(text);
CTRegularTextRun rText = new CTRegularTextRun();
rText.setT(t.getValue());
CTTextCharacterProperties rProps = new CTTextCharacterProperties();
rText.setRPr(rProps);
ObjectFactory dmlFactory = new ObjectFactory();
CTHyperlink hyperlink = dmlFactory.createCTHyperlink();
hyperlink.setAction("https://dev.ccc.com/");
hyperlink.setTgtFrame("_blank");
rProps.setHlinkClick(hyperlink);

after opening pptx file, I cannot see the possibility of clicking on this hyperlink. if I use internal linking inside the presentation, it's working.
Also, if I use HTTP instead of HTTPS it throws an exception on opening pptx file with the message "PowerPoint found a problem with content".

Any ideas?

Re: Create CTHyperlink

PostPosted: Tue Apr 04, 2023 9:26 am
by jason
Your are missing the relId and the relationship.

If you create a sample pptx and upload it at http://webapp.docx4java.org/OnlineDemo/PartsList.html you'll see in your slide part, a txBody like:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
                        <p:txBody>
                            <a:bodyPr/>
                            <a:lstStyle/>
                            <a:p>
                                <a:r>
                                    <a:rPr lang="en-US" dirty="false">
                                        <a:hlinkClick r:id="rId2"/>
                                    </a:rPr>
                                    <a:t>https://www.docx4java.org/trac/docx4j</a:t>
                                </a:r>
                                <a:endParaRPr lang="en-US" dirty="false"/>
                            </a:p>
                        </p:txBody>
 
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


There you can generate the following code:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
org.docx4j.dml.ObjectFactory dmlObjectFactory = new org.docx4j.dml.ObjectFactory();

CTTextBody textbody = dmlObjectFactory.createCTTextBody();
    // Create object for p
    CTTextParagraph textparagraph = dmlObjectFactory.createCTTextParagraph();
    textbody.getP().add( textparagraph);
        // Create object for endParaRPr
        CTRegularTextRun regulartextrun = dmlObjectFactory.createCTRegularTextRun();
        textparagraph.getEGTextRun().add( regulartextrun);
            // Create object for rPr
            CTTextCharacterProperties textcharacterproperties = dmlObjectFactory.createCTTextCharacterProperties();
            regulartextrun.setRPr(textcharacterproperties);
                textcharacterproperties.setLang( "en-US");
                // Create object for hlinkClick
                CTHyperlink hyperlink = dmlObjectFactory.createCTHyperlink();
                textcharacterproperties.setHlinkClick(hyperlink);
                    hyperlink.setAction( "");
                    hyperlink.setTgtFrame( "");
                    hyperlink.setTooltip( "");
                    hyperlink.setInvalidUrl( "");
                    hyperlink.setId( "rId2");
                textcharacterproperties.setSmtId( new Long(0) );
            regulartextrun.setT( "https://www.docx4java.org/trac/docx4j");
        // Create object for endParaRPr
        CTTextCharacterProperties textcharacterproperties2 = dmlObjectFactory.createCTTextCharacterProperties();
        textparagraph.setEndParaRPr(textcharacterproperties2);
            textcharacterproperties2.setLang( "en-US");
            textcharacterproperties2.setSmtId( new Long(0) );
    // Create object for bodyPr
    CTTextBodyProperties textbodyproperties = dmlObjectFactory.createCTTextBodyProperties();
    textbody.setBodyPr(textbodyproperties);
    // Create object for lstStyle
    CTTextListStyle textliststyle = dmlObjectFactory.createCTTextListStyle();
    textbody.setLstStyle(textliststyle);
 
Parsed in 0.018 seconds, using GeSHi 1.0.8.4


The slide rels part (not shown by the webapp) contains:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
<Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink" Target="https://www.docx4java.org/trac/docx4j" TargetMode="External"/>
 
Parsed in 0.018 seconds, using GeSHi 1.0.8.4


To add the hyperlink rel, try something like (untested):

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("External");  
                                                                       
                        slidepart.getRelationshipsPart().addRelationship(rel);
 
Parsed in 0.014 seconds, using GeSHi 1.0.8.4


then in your slide, its hyperlink.setId( rel.getId() )