0
点赞
收藏
分享

微信扫一扫

java poi hslf

云上笔记 2023-08-08 阅读 55

Java POI HSLF

1. Introduction

Java POI HSLF (Horrible Slide Layout Format) is a Java library provided by Apache that allows developers to read, create, and manipulate PowerPoint presentations in Java. It is a part of the Apache POI project, which provides libraries for working with various Microsoft Office file formats.

HSLF provides an easy-to-use API to create and modify PowerPoint presentations programmatically. It supports both reading and writing of presentations, allowing developers to extract data from existing presentations and generate new ones from scratch.

2. Getting Started

To use Java POI HSLF, you need to include the HSLF library in your project. You can download the latest version of Apache POI from the official Apache POI website ( Once you have downloaded the library, you can add it to your project's classpath.

3. Reading PowerPoint Presentations

To read a PowerPoint presentation using Java POI HSLF, you need to create an instance of the SlideShow class and pass the file path of the presentation to its constructor. Here is an example:

import org.apache.poi.hslf.usermodel.SlideShow;

public class ReadPresentation {
    public static void main(String[] args) throws Exception {
        String filePath = "path/to/presentation.ppt";
        SlideShow ppt = new SlideShow(new FileInputStream(filePath));
        
        // Iterate over slides
        for (org.apache.poi.hslf.model.Slide slide : ppt.getSlides()) {
            // Print slide number
            System.out.println("Slide number: " + slide.getSlideNumber());
            
            // Print slide title
            org.apache.poi.hslf.model.Title title = slide.getTitle();
            if (title != null) {
                System.out.println("Slide title: " + title.getText());
            }
            
            // Print slide content
            for (org.apache.poi.hslf.model.Shape shape : slide.getShapes()) {
                if (shape instanceof org.apache.poi.hslf.model.TextShape) {
                    org.apache.poi.hslf.model.TextShape textShape = (org.apache.poi.hslf.model.TextShape) shape;
                    System.out.println("Text: " + textShape.getText());
                }
            }
        }
        
        ppt.close();
    }
}

This code reads a PowerPoint presentation from the specified file path and prints the slide number, title (if available), and text content of each slide.

4. Creating PowerPoint Presentations

To create a PowerPoint presentation using Java POI HSLF, you need to create an instance of the SlideShow class and add slides to it. Here is an example:

import org.apache.poi.hslf.usermodel.SlideShow;
import org.apache.poi.hslf.usermodel.Slide;

public class CreatePresentation {
    public static void main(String[] args) throws Exception {
        SlideShow ppt = new SlideShow();
        
        // Create a slide
        Slide slide1 = ppt.createSlide();
        
        // Set slide title
        slide1.setTitle("Hello, World!");
        
        // Add text content to the slide
        slide1.addText("This is a sample slide created using Java POI HSLF.");
        
        // Save the presentation to a file
        FileOutputStream out = new FileOutputStream("path/to/new_presentation.ppt");
        ppt.write(out);
        out.close();
        
        ppt.close();
    }
}

This code creates a new PowerPoint presentation with a single slide. The slide has a title and some text content. The presentation is then saved to the specified file path.

5. Manipulating PowerPoint Presentations

Java POI HSLF provides various classes and methods to manipulate PowerPoint presentations. You can add and remove slides, modify slide content, apply formatting, add images and charts, and much more.

Here are some examples of common operations:

  • Adding an image to a slide:
// Create a slide
Slide slide = ppt.createSlide();

// Add an image to the slide
byte[] imageData = Files.readAllBytes(Paths.get("path/to/image.jpg"));
int pictureIndex = ppt.addPicture(imageData, Picture.PNG);
slide.createPicture(pictureIndex);
  • Adding a table to a slide:
// Create a slide
Slide slide = ppt.createSlide();

// Create a table
int numRows = 3;
int numCols = 2;
int x = 100;
int y = 100;
int width = 400;
int height = 200;
Table table = slide.createTable(numRows, numCols, x, y, width, height);

// Set table content
for (int row = 0; row < numRows; row++) {
    for (int col = 0; col < numCols; col++) {
        TableCell cell = table.getCell(row, col);
        cell.setText("Row " + (row + 1) + ", Column " + (col + 1));
    }
}
  • Applying formatting to a slide:
// Create a slide
Slide slide = ppt.createSlide();

// Set slide background color
slide.setFollowMasterBackground(false);
slide.setBackground(new Color(255, 255, 0)); // Yellow

// Set slide title font size and color
TextRun title = slide
举报

相关推荐

java poi pptx

Apache POI(Java)

【Java】POI解析excel

java poi pdf实例

JAVA poi操作word

0 条评论