Java adds text and picture hyperlinks in PPT

Keywords: Java

This paper introduces the method of adding hyperlink in PPT slide by Java program. It can set hyperlink for text or picture. When setting hyperlink, it can set different links to object including web page link, email address link, slide jump link, etc. The method in this paper uses free flame.presentation for Java, which can be found on the official website Download jar package , and unzip the jar under the lib folder into the java program.

The import effect is as follows:

 

 

Program running environment: Java, IDEA, jdk1.8.0, no need to install Microsoft PowerPoint

 

Java code example

import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;

import java.awt.geom.Rectangle2D;

public class AddHyperlink {
    public static void main(String[] args) throws Exception{
        //Load test PPT
        Presentation ppt = new Presentation();
        ppt.loadFromFile("test.pptx");

        //instantiation Rectangle2D.Double Class objects
        Rectangle2D.Double rec = new Rectangle2D.Double(350, 150, 400, 180);

        //Add shape on Slide 1
        IAutoShape shape1 = ppt.getSlides().get(0).getShapes().appendShape(ShapeType.RECTANGLE, rec);
        shape1.getFill().setFillType(FillFormatType.NONE);
        shape1.getLine().setFillType(FillFormatType.NONE);

        //Add hyperlink to web page
        ParagraphEx para1 = new ParagraphEx();
        PortionEx tr1 = new PortionEx();
        tr1.setText("1. Website address link: Click to visit the website");
        tr1.getClickAction().setAddress("https://www.baidu.com/");
        para1.getTextRanges().append(tr1);
        shape1.getTextFrame().getParagraphs().append(para1);
        shape1.getTextFrame().getParagraphs().append(new ParagraphEx());

        //Add hyperlink to mailbox address
        ParagraphEx para2 = new ParagraphEx();
        PortionEx tr2 = new PortionEx();
        tr2.setText("2. Email address link: click send email");
        tr2.getClickAction().setAddress("mailto:123654zz@163.com");
        para2.getTextRanges().append(tr2);
        shape1.getTextFrame().getParagraphs().append(para2);
        shape1.getTextFrame().getParagraphs().append(new ParagraphEx());

        //Add hyperlink to jump to another slide
        ParagraphEx para3 = new ParagraphEx();
        PortionEx tr3 = new PortionEx();
        tr3.setText("3. Slide jump link: Click to jump to the second slide");
        ClickHyperlink link = new ClickHyperlink(ppt.getSlides().get(1));
        tr3.setClickAction(link);
        para3.getTextRanges().append(tr3);
        shape1.getTextFrame().getParagraphs().append(para3);

        //Add picture to slide 2 and set hyperlink
        String imaPath = "pd.png";
        Rectangle2D.Float rect = new Rectangle2D.Float(230, 200, 500, 250);
        IEmbedImage image = ppt.getSlides().get(1).getShapes().appendEmbedImage(ShapeType.RECTANGLE, imaPath, rect);
        image.getLine().setFillType(FillFormatType.NONE);
        ClickHyperlink hyperlink = new ClickHyperlink("https://www.baidu.com/");
        image.setClick(hyperlink);

        //Save document
        ppt.saveToFile("AddHyperlink.pptx", FileFormat.PPTX_2010);
        ppt.dispose();
    }
}

The hyperlink addition effect can be viewed in the slide show:

Text hyperlink:

 

Picture hyperlink:

 

 

 

(end of this paper)

Posted by bla5e on Tue, 07 Apr 2020 11:12:30 -0700