hi
I want to copy a slide with its contents into another slide in powerpoint presentation using openXml format.
thanks
Hi,
Do you want to achieve this programatically?
You can copy the slide content, it's slide layout and the slide master. To create a new
slide, create slide part, slide layout and slide master part and dump the contents copied
into the corresponding parts.
These links may useful to you. Some of the samples were on beta versiob of Open XML. Change
the namespace and the relationship wherever required.
http://openxmldeveloper.org/archive/2006/08/01/424.aspx
http://openxmldeveloper.org/archive/2006/08/24/559.aspx
http://openxmldeveloper.org/forums/post/1744.aspx
Sheela
Thanks Sheela.
I tried it out. Parts of the code are working correctly but these items still remain.I need to add an entry to ppt/presentation.xml. Also the slide1.xml.rels is not created automatically so how can I add it
Your help is really apperciated
If you want to add an entry into presentation.xml file, get the content of the file in xmldocument object, add details about slidemaster and slide as
<p:sldMasterIdLst> <p:sldMasterId id="2147483648" r:id="rId1"/></p:sldMasterIdLst><p:sldIdLst> <p:sldId id="256" r:id="rId2"/></p:sldIdLst>
slide1.xml.rels file will get created when you link slide and the slide layout.
I have a sample to create a new presentationML document. This will give you the parts and the reationship details. I am using existing xml files rather than creating a new one.
Here is the sample code
XmlDocument doc; List<string> Heading = new List<string>();//holds the contents of Slide Heading from the UI List<string> Body = new List<string>();//holds the contents of Slide Body from the UI int positionCount = 0;//holds the count of slides
string docFileName = ""; Uri default1 = null;
//Declaration,Package Package package = null; PackageRelationship rel = null; Uri uri; Uri uriPart; PackagePart part; StreamWriter partWrt; PackagePart getPart;
int slId = 256;//Id given to the slide 1 in the ppt,which goes on incrementing by 1 for every new slide added int relationId = 2;
int resourceId = 0; //Declaration,Namespace string nameSpace = "http://schemas.openxmlformats.org/presentationml/2006/main"; string nameSpaceDrawing = "http://schemas.openxmlformats.org/drawingml/2006/main"; string nameSpaceRel = "http://schemas.openxmlformats.org/officeDocument/2006/relationships"; string fileName;
public createPPTx() { InitializeComponent(); }
private void createPPTx_Load(object sender, EventArgs e) { fileName = GetSavePath(); createPresentation(); createSlide(1); addresource();
}
private void addresource() { uri = new Uri("/ppt/slideLayout1.xml", UriKind.Relative); part = package.CreatePart(uri, "application/vnd.openxmlformats-officedocument.presentationml.slideLayout+xml");
partWrt = new StreamWriter(part.GetStream(FileMode.Create, FileAccess.ReadWrite)); doc = new XmlDocument();
doc.Load(@"D:\adonet2\systemIO\systemIO\slideLayout1.xml"); doc.Save(partWrt); partWrt.Close();
package.Flush();
uri = new Uri("/ppt/slideMaster1.xml", UriKind.Relative); part = package.CreatePart(uri, "application/vnd.openxmlformats-officedocument.presentationml.slideMaster+xml");
partWrt = new StreamWriter(part.GetStream(FileMode.Create, FileAccess.ReadWrite)); doc = new XmlDocument(); doc.Load(@"D:\adonet2\systemIO\systemIO\slideMaster1.xml"); doc.Save(partWrt); partWrt.Close();
//create relationship to the slidemaster uri = new Uri("slideMaster1.xml", UriKind.Relative); uriPart = new Uri("/ppt/presentation.xml", UriKind.Relative); getPart = package.GetPart(uriPart);
rel = getPart.CreateRelationship(uri, TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideMaster", "rId2");//
package.Flush(); //create relationship to the slide layout
uri = new Uri("slideLayout1.xml", UriKind.Relative); uriPart = new Uri("/ppt/slideMaster1.xml", UriKind.Relative); getPart = package.GetPart(uriPart);
rel = getPart.CreateRelationship(uri, TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideLayout", "rId1");//
// create a relationship from slide to slidelayout uri = new Uri("slideLayout1.xml", UriKind.Relative); uriPart = new Uri("/ppt/slide1.xml", UriKind.Relative); getPart = package.GetPart(uriPart);
//create relationship from layout to master uri = new Uri("slideMaster1.xml", UriKind.Relative); uriPart = new Uri("/ppt/slideLayout1.xml", UriKind.Relative); getPart = package.GetPart(uriPart);
public void createPresentation() { package = Package.Open(fileName, FileMode.Create, FileAccess.ReadWrite);
XmlDocument docPresentation = new XmlDocument(); docPresentation.Load(@"D:\adonet2\systemIO\systemIO\presentation.xml");
default1 = new Uri("/default.xml", UriKind.Relative); PackagePart part = package.CreatePart(default1, "application/xml");
uri = new Uri("/ppt/presentation.xml", UriKind.Relative); part = package.CreatePart(uri, "application/vnd.openxmlformats-officedocument.presentationml.presentation.main+xml"); partWrt = new StreamWriter(part.GetStream(FileMode.Create, FileAccess.Write)); docPresentation.Save(partWrt); partWrt.Close();
// Create the relationship file uri = new Uri("/ppt/presentation.xml", UriKind.Relative); rel = package.CreateRelationship(uri, TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument", "rId1");
public void createSlide(int slideCount) { // string FileName = "slide1"; string rIdStr = "rId3"; XmlDocument doc = new XmlDocument(); doc.Load(@"D:\adonet2\systemIO\systemIO\slide1.xml");
uri = new Uri("/ppt/slide1.xml", UriKind.RelativeOrAbsolute); part = package.CreatePart(uri, "application/vnd.openxmlformats-officedocument.presentationml.slide+xml"); partWrt = new StreamWriter(part.GetStream(FileMode.Create, FileAccess.Write)); doc.Save(partWrt);
partWrt.Close(); package.Flush();
//Create the relationship file
uri = new Uri("slide1.xml", UriKind.Relative); uriPart = new Uri("/ppt/presentation.xml", UriKind.Relative); getPart = package.GetPart(uriPart); // PackageRelationship rel = getPart.CreateRelationship(uri, TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/slide", rIdStr);//
package.DeletePart(default1); package.Flush();
private string GetSavePath() { SaveFileDialog sfd = new SaveFileDialog(); sfd.AddExtension = true; //Get only Docx file sfd.Filter = "pptx|"; sfd.CheckPathExists = true; sfd.DefaultExt = ".pptx";
sfd.ShowDialog();
return sfd.FileName; // return the filename and the path in which the user wants to create the file }
hi sheela;
Thanks for your reply, but ia have a problem while copying the slide, The new slide is empty.
Also the pptx file gave me an error while opening about corrupting in some of the slide components like "Text and Image... etc" .
Here is the code that i wrote
//Copying slide (1) into new slide (6):
XmlDocument
doc.Load(
uri = new Uri("/ppt/slides/slide6.xml", UriKind.RelativeOrAbsolute);
part = package.CreatePart(uri, "application/vnd.openxmlformats-officedocument.presentationml.slide+xml");
StreamWriter partWrt = new StreamWriter(part.GetStream(FileMode.Create, FileAccess.Write));
doc.Save(partWrt);
partWrt.Close();
uri =
part = package.CreatePart(uri,
partWrt =
doc =
uriPart =
getPart = package.GetPart(uriPart);
rel = getPart.CreateRelationship(uri,
xDoc.Load(documentPart.GetStream());
x.SetAttribute(
parentNode.AppendChild(x);
xDoc.Save(documentPart.GetStream(
I am giving a sample code for copying a slide into another.Please try doing this.
string nameSpace = "http://schemas.openxmlformats.org/presentationml/2006/main"; string presentationRelationshipType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument"; const string sliderelationship = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/slide"; Uri presentaionUri = null; Uri slideUri = null; Uri layoutUri = null; PackagePart presentationPart = null; PackagePart slidePart = null; PackagePart oldslidePart = null;//opening the package Package pack = Package.Open(path,FileMode.Open, FileAccess.ReadWrite); foreach (System.IO.Packaging.PackageRelationship relationship in pack.GetRelationshipsByType(presentationRelationshipType)) { presentaionUri = PackUriHelper.ResolvePartUri(new Uri("/", UriKind.Relative), relationship.TargetUri); presentationPart = pack.GetPart(presentaionUri); break; } NameTable nt = new NameTable(); XmlNamespaceManager nsManager = new XmlNamespaceManager(nt); nsManager.AddNamespace("p", nameSpace); XmlDocument xdoc = new XmlDocument(nt);
//loading the presentation.xml and making entry for the new slide xdoc.Load(presentationPart.GetStream()); MessageBox.Show(xdoc.InnerXml.ToString()); XmlNode slidelistNode = xdoc.SelectSingleNode("//" + "p:sldIdLst", nsManager); XmlNode slideidNode = xdoc.CreateNode(XmlNodeType.Element, "p:sldId", nameSpace); XmlNode sld= slidelistNode.FirstChild; XmlNode dplicate= sld.Clone(); XmlAttributeCollection atts = dplicate.Attributes; foreach (XmlAttribute att in atts) { if (att.Name == "id") { att.Value = "257"; } if (att.Name == "r:id") { att.Value = "rId10"; } } slidelistNode.AppendChild(dplicate); xdoc.Save(presentationPart.GetStream());
//loading the slide part which is to be copied foreach (System.IO.Packaging.PackageRelationship rels in presentationPart.GetRelationshipsByType(sliderelationship)) { Uri sUri= PackUriHelper.ResolvePartUri(presentaionUri, rels.TargetUri); oldslidePart = pack.GetPart(sUri); break; } XmlDocument oldslideDoc = new XmlDocument(nt); oldslideDoc.Load(oldslidePart.GetStream());
//making the new slide part and copying the content in it. slideUri = new Uri("/ppt/slides/slide3.xml", UriKind.RelativeOrAbsolute); slidePart = pack.CreatePart(slideUri, "application/vnd.openxmlformats-officedocument.presentationml.slide+xml"); StreamWriter slideWriter = null; slideWriter = new StreamWriter(slidePart.GetStream(FileMode.Create, FileAccess.Write)); oldslideDoc.Save(slideWriter); oldslideDoc.Load(slidePart.GetStream()); MessageBox.Show(oldslideDoc.InnerXml.ToString()); slideWriter.Close(); pack.Flush();
//creating the new slide's relationship with slide layout layoutUri = new Uri("/ppt/slideLayouts/slideLayout1.xml", UriKind.Relative); PackageRelationship slideRels = null; slideRels = slidePart.CreateRelationship(layoutUri, TargetMode.Internal,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideLayout", "rId10"); pack.Flush();
//creating the new slide's relationship with presentation part.
PackageRelationship pptsliderels = null; pptsliderels = presentationPart.CreateRelationship(slideUri, TargetMode.Internal,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/slide", "rId10"); pack.Flush();
Mallika
Hi1
How can I copy a Slide into another powerpoint presentation with use SDK like this?
Dim pptCre2 As PresentationDocument = PresentationDocument.Open("C:\Documents and Settings\CHT\My Documents\pptx\Test1.pptx", PresentationDocumentType.Presentation)
Dim
pptCre.AddPart(slide)
pptCre.Close()
As your question is slightly different from the discussion that was going on in this thread,please put yourquestion as a new question.
I guess Java Api doesn't do that.But you can go through the documentation of openxml4j API.