📜  使用Java在 PPT 中的幻灯片上创建超链接

📅  最后修改于: 2022-05-13 01:55:24.758000             🧑  作者: Mango

使用Java在 PPT 中的幻灯片上创建超链接

Apache POI 是一个强大的 API,它使用户能够使用Java程序创建、操作和显示基于 Microsoft Office 的各种文件格式。使用 POI,您应该能够对以下文件格式执行创建、修改和显示/读取操作。例如, Java不提供处理 Presentation 文件的内置支持,因此我们需要为这项工作寻找开源 API。

它是由 Apache Software Foundation 开发和分发的开源库,用于使用Java程序设计或修改 Microsoft Office 文件。它包含将用户输入数据或文件解码为 MS Office 文档的类和方法。

Apache POI 架构

Apache POI 由各种组件组成,这些组件构成了一个架构以形成一个工作系统:

POIFS(Poor Obfuscation Implementation File System)- 该组件是所有其他 POI 元素的基本因素。它用于显式读取不同的文件。

  • HSSF(可怕的电子表格格式)- 用于读取和写入 MS-Excel 文件的 xls 格式。
  • XSSF(XML 电子表格格式)- 用于 MS-Excel 的 xlsx 文件格式。
  • HPSF(可怕的属性集格式)- 用于提取 MS-Office 文件的属性集。
  • HWPF (Horrible Word Processor Format) - 用于读写 MS-Word 的 doc 扩展文件。
  • XWPF (XML Word Processor Format) - 用于读写 MS-Word 的 Docx 扩展文件。
  • HSLF(可怕的幻灯片布局格式)- 用于阅读、创建和编辑 PowerPoint 演示文稿。
  • HDGF (Horrible DiaGram Format) - 它包含 MS-Visio 二进制文件的类和方法。
  • HPBF (Horrible PuBlisher Format) - 用于读写 MS-Publisher 文件。

依赖项:

poi.ooxml

执行:

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();
    }
}


输出: