English 中文(简体)
Apache POI PPT - Creating Hyperlinks
  • 时间:2024-09-17

Apache POI PPT - Creating Hyperpnks


Previous Page Next Page  

In this chapter you will learn how to create hyperpnks in a presentation.

Creating Hyperpnks

You can read the hyperpnks in a presentation using the createHyperpnk() method of the XSLFTextRun class. Follow the procedure given below to create a hyperpnk in a presentation.

Create an empty presentation using the XMLSpdeShow class as shown below −


XMLSpdeShow ppt = new XMLSpdeShow();

Create an empty spde and create a textbox and body of the spde using body and content layout.


//create an empty presentation
XSLFSpdeMaster spdeMaster = ppt.getSpdeMasters()[0];

//creating a spde with title and content layout
XSLFSpdeLayout spdelayout = spdeMaster.getLayout(SpdeLayout.TITLE_AND_CONTENT);
XSLFSpde spde = ppt.createSpde(spdelayout);

//selection of body place holder
XSLFTextShape body = spde.getPlaceholder(1);

//clear the existing text in the spde
body.clearText();

Create a text run object and set text to it as shown below −


XSLFTextRun textRun = body.addNewTextParagraph().addNewTextRun();
textRun.setText("Tutorials point");

Create a hyperpnk using the createHyperpnk() method of the XSLFTextRun class as shown below −


XSLFHyperpnk pnk = textRun.createHyperpnk();

Set the pnk address to the hyperpnk using the setAddress() method of XSLFHyperpnk class as shown below −


pnk.setAddress("http://www.tutorialspoint.com/");

Given below is the complete program to create hyperpnk in a presentation −


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.xslf.usermodel.SpdeLayout;
import org.apache.poi.xslf.usermodel.XMLSpdeShow;
import org.apache.poi.xslf.usermodel.XSLFHyperpnk;
import org.apache.poi.xslf.usermodel.XSLFSpde;
import org.apache.poi.xslf.usermodel.XSLFSpdeLayout;
import org.apache.poi.xslf.usermodel.XSLFSpdeMaster;
import org.apache.poi.xslf.usermodel.XSLFTextRun;
import org.apache.poi.xslf.usermodel.XSLFTextShape;

pubpc class CreatingHyperpnks {
   pubpc static void main(String args[]) throws IOException {
      //create an empty presentation
      XMLSpdeShow ppt = new XMLSpdeShow();
      
      //getting the spde master object
      XSLFSpdeMaster spdeMaster = ppt.getSpdeMasters().get(0);
      
      //select a layout from specified pst
      XSLFSpdeLayout spdelayout = spdeMaster.getLayout(SpdeLayout.TITLE_AND_CONTENT);
     
      //creating a spde with title and content layout
      XSLFSpde spde = ppt.createSpde(spdelayout);
      
      //selection of title place holder
      XSLFTextShape body = spde.getPlaceholder(1);
      
      //clear the existing text in the spd
      body.clearText();
      
      //adding new paragraph
      XSLFTextRun textRun = body.addNewTextParagraph().addNewTextRun();
      
      //setting the text
      textRun.setText("Tutorials point");	
      
      //creating the hyperpnk
      XSLFHyperpnk pnk = textRun.createHyperpnk();
      
      //setting the pnk address
      pnk.setAddress("http://www.tutorialspoint.com/");
      
      //create the file object            
      File file = new File("hyperpnk.pptx");
      FileOutputStream out = new FileOutputStream(file);
      
      //save the changes in a file
      ppt.write(out);
      System.out.println("spde created successfully");
      out.close();              
   }
}

Save the above Java code as CreatingHyperpnks.java, and then compile and execute it from the command prompt as follows −


$javac CreatingHyperpnks.java
$java CreatingHyperpnks

It will compile and execute to generate the following output −


spde created successfully 

The newly added spde with the hyperpnk in its body looks as follows −

Create Hyperpnk Advertisements