public byte[] GenerateSlides(EntityCollection parentRows, byte[] templateDocumentData) { byte[] outputData; Dictionary <string, object> serviceLocator = new Dictionary <string, object> { { "metadataService", new MetadataLookupService(_service) } }; CustomVariable rowNumber = new CustomVariable(System.Xml.XPath.XPathResultType.Number, 0); serviceLocator.Add("$rowNumber", rowNumber); // Load PowerPoint Template from annotation _trace.Trace("verbose={0},map={1}", _verboseDebug, _mapDebug); using (var templateDocument = new Presentation(templateDocumentData, serviceLocator, _trace, _verboseDebug, _mapDebug)) { // Parameterise slides using the template slide XmlDocument coverDataSets = new XmlDocument(); var dataSetsNode = coverDataSets.CreateElement("datasets"); coverDataSets.AppendChild(dataSetsNode); SerialiseData(dataSetsNode, "parent", parentRows); AddConfigDataSet(dataSetsNode); AddTemplateConfigDataSet(dataSetsNode); AddContactsConfigDataSet(dataSetsNode); AddImageConfigDataSet(dataSetsNode); AddListDataSet(dataSetsNode); AddOrderDataSet(dataSetsNode); templateDocument.CreateCoverPages(coverDataSets); int i = 1; TemplateConfigActions(); if (ItemsPerPage == 0) { foreach (Entity parentRow in parentRows.Entities) { rowNumber.Value = i; XmlDocument dataSets = GetDataSets(parentRow); // Parameterise slides using the template slides templateDocument.CreateSlides(dataSets); i++; } } else { int y = parentRows.Entities.Count; int x = 0; while (x < y) { EntityCollection collection = new EntityCollection(parentRows.Entities.Skip(x).Take(ItemsPerPage).ToList()); rowNumber.Value = i; XmlDocument dataSets = GetDataSets(collection, i); templateDocument.CreateSlides(dataSets); i++; x = x + ItemsPerPage; } } XmlDocument endDataSets = GetProfileDataSets(i, out bool res); if (!res) { templateDocument.CreateEndPages(coverDataSets); } else { templateDocument.CreateEndPages(endDataSets); } //templateDocument.CreateEndPages(coverDataSets); // Save the document to a base64 string outputData = templateDocument.Save(); } return(outputData); }
private void ReplaceTextTokens(XPathNavigator slideXml, XPathNavigator navigator) { //var nodes = slideXml.Select("//a:hlinkClick[starts-with(@tooltip,'!')]", _nsManagerPresentationXml); XPathNavigator lastNode = null; var root = slideXml.Clone(); while (slideXml.MoveToFollowing("hlinkClick", drawingMlNs)) { // Don't go up to parent when itterating over child nodes if (!root.IsDescendant(slideXml)) { break; } if (lastNode != null) { lastNode.DeleteSelf(); // Remove the hyperlink reference } var tokenNode = slideXml; lastNode = slideXml.Clone(); // Get the xpath string xpath = tokenNode.GetAttribute("tooltip", ""); bool match = (xpath != null && xpath.StartsWith("!")); if (!match) { continue; } Trace("Text Token:{0}", xpath); if (xpath.StartsWith("!table:")) { // Add table iterator variable CustomVariable pos = (CustomVariable)_serviceLocator["$rowNumber"]; xpath = xpath.Substring(7); // change the node context and repeat the table row for each vluae // Get the parent row var trNode = tokenNode.SelectAncestors("tr", drawingMlNs, false); var tableNode = trNode.Current.SelectAncestors("graphicFrame", presentationNs, false); tableNode.MoveNext(); // Find the table marker var tableMarker = slideXml.SelectAncestors("r", drawingMlNs, false); var tableMarkerFound = tableMarker.MoveNext(); // Move main itterator to after new table slideXml.MoveTo(tableNode.Current); slideXml.MoveToNext(); int i = 1; if (trNode.MoveNext()) { // Remove table marker tableMarker.Current.DeleteSelf(); //var tableNode = tokenNode.SelectAncestors("graphicFrame", presentationNs, false); //tableNode.Current.MoveToNext(); //nodes.Current.MoveTo(tableNode.Current); string rowXml = trNode.Current.OuterXml; // evaluate the ontext var tableRows = navigator.Select(String.Format(dataRoot, xpath)); // Get each row xpath navigator var templateRow = trNode.Current.Clone(); while (tableRows.MoveNext()) { pos.Value = i; trNode.Current.InsertAfter(rowXml); var newRow = trNode.Current.Clone(); newRow.MoveToNext(); ReplaceTextTokens(newRow, tableRows.Current); trNode.Current.MoveToNext(); i++; } //Remove first template row templateRow.DeleteSelf(); } } else { string value = GetData(navigator, xpath); Trace("Value:{0}", value); tokenNode.MoveToParent(); if (tokenNode.Name == "a:rPr") { if (tokenNode.MoveToNext("t", drawingMlNs)) { tokenNode.SetValue(value); } } else { // Move over this hyperlink since it does not have a text element tokenNode.MoveToParent(); tokenNode.MoveToFollowing("hlinkClick", drawingMlNs); } } } if (lastNode != null) { lastNode.DeleteSelf(); // Remove the hyperlink reference } }