Skip to content
Related Articles

Related Articles

Creating Hyperlink on a Slide in a PPT using Java
  • Last Updated : 22 Dec, 2020
GeeksforGeeks - Summer Carnival Banner

Apache POI is a powerful API that enables the user to create, manipulate, and display various file formats based on Microsoft Office using Java programs. Using POI, one should be able to perform create, modify, and display/read operations on the following file formats. For Example, Java doesn’t provide built-in support for working with Presentation files, so we need to look for open-source APIs for the job.

It is an open-source library developed and distributed by Apache Software Foundation to design or modify Microsoft Office files using Java program. It contains classes and methods to decode the user input data or a file into MS Office documents.

Apache POI Architecture

Apache POI consists of various components that make an architecture to form a working system:

POIFS (Poor Obfuscation Implementation File System) − This component is the basic factor of all other POI elements. It is used to read different files explicitly.



  • HSSF (Horrible Spreadsheet Format) − It is used to read and write xls format of MS-Excel files.
  • XSSF (XML Spreadsheet Format) − It is used for xlsx file format of MS-Excel.
  • HPSF (Horrible Property Set Format) − It is used to extract property sets of the MS-Office files.
  • HWPF (Horrible Word Processor Format) − It is used to read and write doc extension files of MS-Word.
  • XWPF (XML Word Processor Format) − It is used to read and write Docx extension files of MS-Word.
  • HSLF (Horrible Slide Layout Format) − It is used to read, create, and edit PowerPoint presentations.
  • HDGF (Horrible DiaGram Format) − It contains classes and methods for MS-Visio binary files.
  • HPBF (Horrible PuBlisher Format) − It is used to read and write MS-Publisher files.

Dependencies:

poi.ooxml

Implementation:

Java




// Creating Hyperlink on a slide in a PPT using Java
  
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
  
import org.apache.poi.xslf.usermodel.SlideLayout;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFHyperlink;
import org.apache.poi.xslf.usermodel.XSLFSlide;
import org.apache.poi.xslf.usermodel.XSLFSlideLayout;
import org.apache.poi.xslf.usermodel.XSLFSlideMaster;
import org.apache.poi.xslf.usermodel.XSLFTextRun;
import org.apache.poi.xslf.usermodel.XSLFTextShape;
  
// Driver code
public class HyperlinkToPPT {
    public static void main(String args[])
        throws IOException
    {
  
        // create an empty presentation
        XMLSlideShow ppt = new XMLSlideShow();
  
        // getting the slide master object
        XSLFSlideMaster slideMaster
            = ppt.getSlideMasters()[0];
  
        // select a layout from specified list
        XSLFSlideLayout slidelayout = slideMaster.getLayout(
            SlideLayout.TITLE_AND_CONTENT);
  
        // creating a slide with title and content layout
        XSLFSlide slide = ppt.createSlide(slidelayout);
  
        // selection of title place holder
        XSLFTextShape body = slide.getPlaceholder(1);
  
        // clear the existing text in the slide
        body.clearText();
  
        // adding new paragraph
        XSLFTextRun textRun
            = body.addNewTextParagraph().addNewTextRun();
  
        // setting the text
        textRun.setText("GeeksforGeeks");
  
        // creating the hyperlink
        XSLFHyperlink link = textRun.createHyperlink();
  
        // setting the link address
        link.setAddress("http://www.geeksforgeeks.org/");
  
        // create the file object
        File file = new File("C:/poippt/hyperlink.pptx");
        FileOutputStream out = new FileOutputStream(file);
  
        // save the changes in a file
        ppt.write(out);
        System.out.println("sucsess!");
        out.close();
    }
}

Output:

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :