Javaexamples-create-hyperlink-on-slide

提供:Dev Guides
移動先:案内検索

Javaの例-スライドにハイパーリンクを作成

問題の説明

Javaを使用してPPTのスライドにハイパーリンクを作成する方法。

溶液

以下は、Javaを使用してPPTのスライドにハイパーリンクを作成するプログラムです。

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;

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("Tutorials point");

     //creating the hyperlink
      XSLFHyperlink link = textRun.createHyperlink();

     //setting the link address
      link.setAddress("http://www.finddevguides.com/");

     //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("slide cretated successfully");
      out.close();
   }
}

結果

ハイパーリンク