Sunday, 26 December 2021

Copy Slides in PowerPoint Documents using Java

In some cases, you want to copy slides in a PowerPoint document to another specified place. You can copy and paste manually, but it is faster and more efficient to use Java codes for automatic operation. This article will show you how to copy slides from the two aspects below using Free Spire.Presentation forJava.

l  Copy slides within one PowerPoint document

l  Copy slides between two PowerPoint documents

DEPENDENCY

First of all, you’re required to download the package of Free Spire.Presentation for Java from the link, and then add Spire.Presentation.jar as a dependency to your Java program. Or if you use Maven, you can directly add the following code to your project’s pom.xml file to 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>3.9.0</version>
</dependency>
</dependencies>

Copy Slides within One PowerPoint Document

Spire.Presentation for Java supports getting a specific slide of a PowerPoint document using Presentation.getSlides().get() method and insert it to the specified position of the document using Presentation.getSlides().insert() method or append it at the end of the document using Presentation.getSlides().append() method. You can follow the steps below.

l  Create a Presentation object and load a sample PowerPoint document using Presentation.loadFromFile() method.

l  Get a list of all slides and choose a specific slide to be copied using Presentation.getSlides().get() method.

l  Insert the desired slide to the specified position of the document using Presentation.getSlides().insert() method.

l  Append the slide at the end of the document using Presentation.getSlides().append() method.

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

import com.spire.presentation.*;

public class CopySlidesWithinPPT {
public static void main(String[] args) throws Exception {
//Create a Presentation object
Presentation ppt = new Presentation();
//Load a sample PowerPoint document
ppt.loadFromFile("C:\\Users\\Test1\\Desktop\\sample1.pptx");

//Get a list of slides and choose the second slide to be copied
ISlide slide = ppt.getSlides().get(1);

//Insert the desired slide to the specified index in the same presentation
int index = 3;
ppt.getSlides().insert(index, slide);

//Append the slide at the end of the document
ppt.getSlides().append(slide);

//Save the document to another file
String result = "output/CopySlideWithinAPPT.pptx";
ppt.saveToFile(result, FileFormat.PPTX_2013);
}
}



Copy Slides between Two PowerPoint Documents

The following are detailed steps to copy a slide from one PowerPoint document to a specified position or the end of the other document.

l  Create a Presentation object and load one sample document using Presentation.loadFromFile() method.

l  Create another Presentation object and load the other sample document using Presentation.loadFromFile() method.

l  Get a specific slide of document one using Presentation.getSlides().get() method and insert its copy into the specified position of document two using Presentation.getSlides().insert() method.

l  Get another specific slide of document one using Presentation.getSlides().get() method and add its copy to the end of document two using Presentation.getSlides().append() method.

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

import com.spire.presentation.FileFormat;
import com.spire.presentation.Presentation;

public class CopySlidesBetweenPPT {
public static void main(String[] args) throws Exception {
//Create a Presentation object to load one sample document
Presentation pptOne= new Presentation();
pptOne.loadFromFile("C:\\Users\\Test1\\Desktop\\sample1.pptx");

//Create another Presentation object to load another sample document
Presentation pptTwo = new Presentation();
pptTwo.loadFromFile("C:\\Users\\Test1\\Desktop\\sample2.pptx");

//Insert the first slide of sample 1 into the first slide of sample 2
pptTwo.getSlides().insert(0,pptOne.getSlides().get(0));

//Append the last slide of sample 1 to the end of the sample 2
pptTwo.getSlides().append(pptOne.getSlides().get(3));

//Save the sample 2 to another file
pptTwo.saveToFile("output/CopySlidesBetweenPPT.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...