Introduction
Cloning is the process of making an exact copy or replica of something. Aspose.Slides also makes it possible to make a copy or clone of any slide and then insert that cloned slide to the current or any other opened presentation. The process of slide cloning creates a new slide that can be modified by developers without changing the original slide. In this topic, we will learn that how can we perform slide cloning.
Cloning a Slide
There are two possible scenarios in which developers may what to clone a slide, which are given below:
- Cloning a slide from one position to another one within the same presentation
- Cloning a slide from one presentation to another one
Aspose.Slides offers Presentation class that provides a set of overloaded methods to perform the above types of slide cloning. Let's discuss the use of these overloaded methods in the below sections with the help of examples.
Within the Same Presentation
If you want to clone a slide and then use it within the same presentation file then please follows the steps below:
- Create an instance of Presentation class
- Obtain the reference of a slide by using its Position
- Clone the Referenced Slide to another Position of the presentation
- Write the modified presentation as a PPT file
In the example given below, we have cloned a slide (lying at the 5th position of the presentation) to the end of the presentation.
Example:
[C#]
//Instantiate a Presentation object that represents a PPT file
Presentation pres=new Presentation("demo.ppt");
//Accessing a slide using its slide position
Slide slide = pres.GetSlideByPosition(5);
//Cloning the selected slide at the end of the same presentation file
pres.CloneSlide(slide,pres.Slides.LastSlidePosition + 1);
//Writing the presentation as a PPT file
pres.Write("C:\\modified.ppt");
[VB.NET]
'Instantiate a Presentation object that represents a PPT file
Dim pres As Presentation = New Presentation("demo.ppt")
'Accessing a slide using its slide position
Dim slide As Slide = pres.GetSlideByPosition(5)
'Cloning the selected slide at the end of the same presentation file
pres.CloneSlide(slide,pres.Slides.LastSlidePosition + 1)
'Writing the presentation as a PPT file
pres.Write("C:\\modified.ppt")
[Java]
try
{
//Instantiate a Presentation object that represents a PPT file
Presentation pres=new Presentation(new FileInputStream(new File("demo.ppt")));
//Accessing a slide using its slide position
Slide slide = pres.getSlideByPosition(5);
//Cloning the selected slide at the end of the same presentation file
pres.cloneSlide(slide,pres.getSlides().getLastSlidePosition() + 1);
//Writing the presentation as a PPT file
pres.write(new FileOutputStream(new File("modified.ppt")));
}
catch(Exception ex)
{
System.out.println(ex.toString());
}
[PHP]
<?php
//Using aspose.slides.jar file so that the classes inside the jar file
//can be used
java_require("aspose.slides.jar");
try
{
//Creating a file input stream to read the PPT file
$fistream=new Java("java.io.FileInputStream","C:\\demo.ppt");
//Instantiate a Presentation object that represents a PPT file
$pres=new Java("com.aspose.slides.Presentation",$fistream);
//Accessing a slide using its slide position
$slide=$pres->getSlideByPosition(5);
//Cloning the selected slide at the end of the same presentation file
$pres->cloneSlide($slide,$pres->getSlides()->getLastSlidePosition() + 1);
//Creating a file output stream to write the output file
$fostream=new Java("java.io.FileOutputStream","C:\\modified.ppt");
//Writing the presentation as a PPT file
$pres->write($fostream);
//Closing the streams
$fistream->close();
$fostream->close();
}
catch(JavaException $ex)
{
echo $ex->toString();
}
?>
In Another Presentation
If you need to clone a slide from one presentation and use it in another presentation file then please follows the steps below:
- Create an instance of Presentation class containing the source presentation from where the slide is to be cloned
- Create an instance of Presentation class containing the destination presentation where the cloned slide will be added
- Obtain the reference of a slide by using its Position
- Create the object of an Empty Collection that will be used to store the temporary information about the masters of PPT file
- Clone the Referenced Slide to a specific Position of another presentation (destination)
- Write the modified presentation as a PPT file
In the example given below, we have cloned a slide (lying at the 5th position of the source presentation) to the end of the destination presentation.
Example:
[C#]
//Instantiate a Presentation from where the slide will be cloned
Presentation pres1=new Presentation("source.ppt");
//Instantiate a Presentation where the cloned slide will be added
Presentation pres2=new Presentation("destination.ppt");
//Accessing a slide using its slide position
Slide slide = pres1.GetSlideByPosition(5);
//Creating SortedList object that is used to store the temporary information
//about the masters of PPT file. No value should be added to it.
SortedList sList=new SortedList();
//Cloning the selected slide at the end of another presentation file
pres1.CloneSlide(slide,pres2.Slides.LastSlidePosition + 1,pres2,sList);
//Writing the presentation as a PPT file
pres2.Write("C:\\modified.ppt");
[VB.NET]
'Instantiate a Presentation from where the slide will be cloned
Dim pres1 As Presentation = New Presentation("source.ppt")
'Instantiate a Presentation where the cloned slide will be added
Dim pres2 As Presentation = New Presentation("destination.ppt")
'Accessing a slide using its slide position
Dim slide As Slide = pres1.GetSlideByPosition(5)
'Creating SortedList object that is used to store the temporary information
'about the masters of PPT file. No value should be added to it.
Dim sList As SortedList = New SortedList()
'Cloning the selected slide at the end of another presentation file
pres1.CloneSlide(slide,pres2.Slides.LastSlidePosition + 1,pres2,sList)
'Writing the presentation as a PPT file
pres2.Write("C:\\modified.ppt")
[Java]
try
{
//Instantiate a Presentation object that represents a PPT file
Presentation pres1=new Presentation(new FileInputStream(new File("source.ppt")));
//Instantiate a Presentation object that represents a PPT file
Presentation pres2=new Presentation(new FileInputStream(new File("destination.ppt")));
//Accessing a slide using its slide position
Slide slide = pres1.getSlideByPosition(5);
//Creating TreeMap object that is used to store the temporary information
//about the masters of PPT file. No value should be added to it.
TreeMap tMap=new TreeMap();
//Cloning the selected slide at the end of another presentation file
pres1.cloneSlide(slide,pres1.getSlides().getLastSlidePosition() + 1,pres2,tMap);
//Writing the presentation as a PPT file
pres2.write(new FileOutputStream(new File("modified.ppt")));
}
catch(Exception ex)
{
System.out.println(ex.toString());
}
[PHP]
<?php
//Using aspose.slides.jar file so that the classes inside the jar file
//can be used
java_require("aspose.slides.jar");
try
{
//Creating a file input stream to read the source file
$fistream1=new Java("java.io.FileInputStream","C:\\source.ppt");
//Instantiate a Presentation from where the slide will be cloned
$pres1=new Java("com.aspose.slides.Presentation",$fistream1);
//Creating a file input stream to read the destination file
$fistream2=new Java("java.io.FileInputStream","C:\\destination.ppt");
//Instantiate a Presentation where the cloned slide will be added
$pres2=new Java("com.aspose.slides.Presentation",$fistream2);
//Accessing a slide using its slide position
$slide=$pres1->getSlideByPosition(5);
//Creating TreeMap object that is used to store the temporary information
//about the masters of PPT file. No value should be added to it.
$tMap=new Java("java.util.TreeMap");
//Cloning the selected slide at the end of another presentation file
$pres1->cloneSlide($slide,$pres1->getSlides()->getLastSlidePosition() + 1,$pres2,$tMap);
//Creating a file output stream to write the output file
$fostream=new Java("java.io.FileOutputStream","C:\\modified.ppt");
//Writing the presentation as a PPT file
$pres2->write($fostream);
//Closing the streams
$fistream1->close();
$fistream2->close();
$fostream->close();
}
catch(JavaException $ex)
{
echo $ex->toString();
}
?>