Monday, 14 March 2022

Embed a Zip File in a PowerPoint Document using Java

ZIP is a file format for storing documents and compressing data, and it is one of several mainstream compression formats. This article will demonstrate how to embed a zip file as an OLE object in a PowerPoint document using Java codes.

DEPENDENCY

In order to finish the operation mentioned above, you need to download a package of Free Spire.Presentation for Java from this link, and then add Spire.Presentation.jar to your Java program. Or if you use Maven, you can type the following codes in the pom.xml file to easily import the Jar file.

<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.presentation.free</artifactId>
<version>5.1.0</version>
</dependency>
</dependencies>

USING THE CODE

Free Spire.Presentation for Java supports embedding a zip file in a new PowerPoint document. The detailed steps are listed below.

l  Create a Presentation instance and set the size of slides using Presentation.getSlideSize().setType() method.

l  Get a specific slide of the document using Presentation.getSlides().get() method.

l  Load a zip file and convert it to a byte[] object.

l  Load an image and add it to the slide as the display icon of the zip file using Presentaion.getImages().append() method.

l  Insert the zip file as an OLE object to the specific slide using ISlide.getShapes().appendOleObject() method.

l  Get the fill properties object of OleObject image using IOleObject.getSubstituteImagePictureFillFormat() method, and then set the embedded image using PictureFillFormat.getPicture().setEmbedImage() method.

l  Save the document to another file using Presentation.saveToFile() method.

import com.spire.presentation.*;
import com.spire.presentation.drawing.IImageData;

import javax.imageio.ImageIO;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;

public class InsertZip {
public static void main(String[] args) throws Exception {
//Create a Presentation object
Presentation presentation = new Presentation();
presentation.getSlideSize().setType(SlideSizeType.SCREEN_16_X_9);

//Get the first slide
ISlide slide = presentation.getSlides().get(0);

//Load a zip file and convert it to a byte[] object
String filePath = "sample.zip";
File zipFile = new File(filePath);
FileInputStream inputStream = new FileInputStream(zipFile);
byte[] data = new byte[(int) zipFile.length()];
inputStream.read(data, 0, data.length);

//Load an image file as the display icon
File file = new File("Image.jpeg");
BufferedImage image = ImageIO.read(file);
IImageData oleImage = presentation.getImages().append(image);

//Insert the zip file as an OLE object to the first slide
Rectangle2D rect = new Rectangle2D.Float(60, 60, image.getWidth(), image.getHeight());
IOleObject oleObject = slide.getShapes().appendOleObject("zip", data, rect);
oleObject.getSubstituteImagePictureFillFormat().getPicture().setEmbedImage(oleImage);
oleObject.setProgId("Package");

//Save to file
presentation.saveToFile("output/InsertZip.pptx", FileFormat.PPTX_2013);
}
}


No comments:

Post a Comment

Change PDF Versions in Java

In daily work, you might need to change the version of a PDF document you have in order to ensure compatibility with another version which a...