Suppose there are two PowerPoint documents, and you want to copy a certain slide from one document to a specified location of the other. Manual copying and pasting is an option, but the quicker and more efficient way is to use Java codes for automatic operation. This article will show you how to programmatically copy slides between two different PowerPoint documents using Spire.Presentation for Java.
Install Spire.Presentation for Java
First of all, you're required to add the Spire.Presentation.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml 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</artifactId> <version>9.10.2</version> </dependency> </dependencies>
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.
- Create a Presentation object and load one sample document using Presentation.loadFromFile() method.
- Create another Presentation object and load the other sample document using Presentation.loadFromFile() method.
- 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.
- 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.
- Save the document two to another file using Presentation.saveToFile() method.
- Java
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 the other sample document Presentation pptTwo = new Presentation(); pptTwo.loadFromFile("C:\\Users\\Test1\\Desktop\\sample2.pptx"); //Insert the specific slide from document one into the specified position of document two pptTwo.getSlides().insert(0,pptOne.getSlides().get(0)); //Append the specific slide of document one to the end of document two pptTwo.getSlides().append(pptOne.getSlides().get(3)); //Save the document two to another file pptTwo.saveToFile("output/CopySlidesBetweenPPT.pptx", FileFormat.PPTX_2013); } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.