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

You can read the hyperlinks in a presentation using the createHyperlink() method of the XSLFTextRun class. Follow the procedure given below to create a hyperlink in a presentation.

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

  1. XMLSlideShow ppt = new XMLSlideShow();

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

  1. //create an empty presentation
  2. XSLFSlideMaster slideMaster = ppt.getSlideMasters()[0];
  3.  
  4. //creating a slide with title and content layout
  5. XSLFSlideLayout slidelayout = slideMaster.getLayout(SlideLayout.TITLE_AND_CONTENT);
  6. XSLFSlide slide = ppt.createSlide(slidelayout);
  7.  
  8. //selection of body place holder
  9. XSLFTextShape body = slide.getPlaceholder(1);
  10.  
  11. //clear the existing text in the slide
  12. body.clearText();

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

  1. XSLFTextRun textRun = body.addNewTextParagraph().addNewTextRun();
  2. textRun.setText(“Tutorials point”);

Create a hyperlink using the createHyperlink() method of the XSLFTextRun class as shown below −

  1. XSLFHyperlink link = textRun.createHyperlink();

Set the link address to the hyperlink using the setAddress() method of XSLFHyperlink class as shown below −

  1. link.setAddress(“http://www.tutorialspoint.com/“);

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

  1. import java.io.File;
  2. import java.io.FileOutputStream;
  3. import java.io.IOException;
  4.  
  5. import org.apache.poi.xslf.usermodel.SlideLayout;
  6. import org.apache.poi.xslf.usermodel.XMLSlideShow;
  7. import org.apache.poi.xslf.usermodel.XSLFHyperlink;
  8. import org.apache.poi.xslf.usermodel.XSLFSlide;
  9. import org.apache.poi.xslf.usermodel.XSLFSlideLayout;
  10. import org.apache.poi.xslf.usermodel.XSLFSlideMaster;
  11. import org.apache.poi.xslf.usermodel.XSLFTextRun;
  12. import org.apache.poi.xslf.usermodel.XSLFTextShape;
  13.  
  14. public class CreatingHyperlinks {
  15.  
  16. public static void main(String args[]) throws IOException {
  17.  
  18. //create an empty presentation
  19. XMLSlideShow ppt = new XMLSlideShow();
  20.  
  21. //getting the slide master object
  22. XSLFSlideMaster slideMaster = ppt.getSlideMasters()[0];
  23.  
  24. //select a layout from specified list
  25. XSLFSlideLayout slidelayout = slideMaster.getLayout(SlideLayout.TITLE_AND_CONTENT);
  26.  
  27. //creating a slide with title and content layout
  28. XSLFSlide slide = ppt.createSlide(slidelayout);
  29.  
  30. //selection of title place holder
  31. XSLFTextShape body = slide.getPlaceholder(1);
  32.  
  33. //clear the existing text in the slid
  34. body.clearText();
  35.  
  36. //adding new paragraph
  37. XSLFTextRun textRun = body.addNewTextParagraph().addNewTextRun();
  38.  
  39. //setting the text
  40. textRun.setText(“Tutorials point”);
  41. //creating the hyperlink
  42. XSLFHyperlink link = textRun.createHyperlink();
  43.  
  44. //setting the link address
  45. link.setAddress(“http://tutorial.e-learn.cn/“);
  46.  
  47. //create the file object
    File file = new File(“hyperlink.pptx”);
  48. FileOutputStream out = new FileOutputStream(file);
  49.  
  50. //save the changes in a file
  51. ppt.write(out);
  52. System.out.println(“slide cretated successfully”);
  53. out.close();
    }
  54. }

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

  1. $javac CreatingHyperlinks.java
  2. $java CreatingHyperlinks

It will compile and execute to generate the following output −

  1. slide cretated successfully

The newly added slide with the hyperlink in its body looks as follows −

CreateHyperlink.jpg