/// <summary> /// Generates the content of the custom XML properties part. /// </summary> /// <param name="customXmlPropertiesPart">The custom XML properties part1.</param> private void GenerateCustomXmlPropertiesPartContent(CustomXmlPropertiesPart customXmlPropertiesPart) { DataStoreItem dataStoreItem = new DataStoreItem() { ItemId = "{" + Guid.NewGuid().ToString().ToUpper() + "}" }; dataStoreItem.AddNamespaceDeclaration("ds", "http://schemas.openxmlformats.org/officeDocument/2006/customXml"); SchemaReferences schemaReferences = new SchemaReferences(); SchemaReference schemaReference = new SchemaReference() { Uri = namespaceUri }; schemaReferences.Append(schemaReference); dataStoreItem.Append(schemaReferences); customXmlPropertiesPart.DataStoreItem = dataStoreItem; }
public static void InsertCustomXmlForPpt(string pptFileName, string customXML) { using (PresentationDocument openXmlDoc = PresentationDocument.Open(pptFileName, true)) { // Create a new custom XML part int count = openXmlDoc.PresentationPart.Parts.Count(); string customXmlId = string.Format("rId{0}", count + 1); CustomXmlPart customXmlPart = openXmlDoc.PresentationPart.AddCustomXmlPart(CustomXmlPartType.CustomXml, customXmlId); using (Stream outputStream = customXmlPart.GetStream()) { using (StreamWriter ts = new StreamWriter(outputStream)) { ts.Write(customXML); } } // Add datastore so that the xml will persist after the document is modified count = customXmlPart.Parts.Count(); CustomXmlPropertiesPart customXmlPropertiesPart = customXmlPart.AddNewPart <CustomXmlPropertiesPart>(string.Format("rId{0}", count + 1)); DataStoreItem dataStoreItem = new DataStoreItem() { ItemId = string.Format("{0}", Guid.NewGuid()) }; dataStoreItem.AddNamespaceDeclaration("ds", "http://schemas.openxmlformats.org/officeDocument/2006/customXml"); SchemaReferences schemaReferences = new SchemaReferences(); SchemaReference schemaReference = new SchemaReference() { Uri = "http://www.w3.org/2001/XMLSchema" }; schemaReferences.Append(schemaReference); dataStoreItem.Append(schemaReferences); customXmlPropertiesPart.DataStoreItem = dataStoreItem; // Add the xml to the customer data section of the document CustomerData customerData = new CustomerData() { Id = customXmlId }; if (openXmlDoc.PresentationPart.Presentation.CustomerDataList != null) { // Sequence matters: http://www.schemacentral.com/sc/ooxml/e-p_custDataLst-1.html if (openXmlDoc.PresentationPart.Presentation.CustomerDataList.Count() > 0) { openXmlDoc.PresentationPart.Presentation.CustomerDataList.InsertBefore(customerData, openXmlDoc.PresentationPart.Presentation.CustomerDataList.FirstChild); } else { openXmlDoc.PresentationPart.Presentation.CustomerDataList.Append(customerData); } } else { CustomerDataList customerDataList = new CustomerDataList(); customerDataList.Append(customerData); // Sequence matters: http://www.schemacentral.com/sc/ooxml/e-p_presentation.html if (openXmlDoc.PresentationPart.Presentation.Kinsoku != null) { openXmlDoc.PresentationPart.Presentation.InsertBefore(customerDataList, openXmlDoc.PresentationPart.Presentation.Kinsoku); } else if (openXmlDoc.PresentationPart.Presentation.DefaultTextStyle != null) { openXmlDoc.PresentationPart.Presentation.InsertBefore(customerDataList, openXmlDoc.PresentationPart.Presentation.DefaultTextStyle); } else if (openXmlDoc.PresentationPart.Presentation.ModificationVerifier != null) { openXmlDoc.PresentationPart.Presentation.InsertBefore(customerDataList, openXmlDoc.PresentationPart.Presentation.ModificationVerifier); } else if (openXmlDoc.PresentationPart.Presentation.PresentationExtensionList != null) { openXmlDoc.PresentationPart.Presentation.InsertBefore(customerDataList, openXmlDoc.PresentationPart.Presentation.PresentationExtensionList); } else { openXmlDoc.PresentationPart.Presentation.Append(customerDataList); } } } }