public static void CreatingTableRepeatingSectionMappedToCustomXmlPart(string dataDir) { // ExStart:CreatingTableRepeatingSectionMappedToCustomXmlPart Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); CustomXmlPart xmlPart = doc.CustomXmlParts.Add("Books", "<books><book><title>Everyday Italian</title><author>Giada De Laurentiis</author></book>" + "<book><title>Harry Potter</title><author>J K. Rowling</author></book>" + "<book><title>Learning XML</title><author>Erik T. Ray</author></book></books>"); Table table = builder.StartTable(); builder.InsertCell(); builder.Write("Title"); builder.InsertCell(); builder.Write("Author"); builder.EndRow(); builder.EndTable(); StructuredDocumentTag repeatingSectionSdt = new StructuredDocumentTag(doc, SdtType.RepeatingSection, MarkupLevel.Row); repeatingSectionSdt.XmlMapping.SetMapping(xmlPart, "/books[1]/book", ""); table.AppendChild(repeatingSectionSdt); StructuredDocumentTag repeatingSectionItemSdt = new StructuredDocumentTag(doc, SdtType.RepeatingSectionItem, MarkupLevel.Row); repeatingSectionSdt.AppendChild(repeatingSectionItemSdt); Row row = new Row(doc); repeatingSectionItemSdt.AppendChild(row); StructuredDocumentTag titleSdt = new StructuredDocumentTag(doc, SdtType.PlainText, MarkupLevel.Cell); titleSdt.XmlMapping.SetMapping(xmlPart, "/books[1]/book[1]/title[1]", ""); row.AppendChild(titleSdt); StructuredDocumentTag authorSdt = new StructuredDocumentTag(doc, SdtType.PlainText, MarkupLevel.Cell); authorSdt.XmlMapping.SetMapping(xmlPart, "/books[1]/book[1]/author[1]", ""); row.AppendChild(authorSdt); doc.Save(dataDir + "Document.docx"); // ExEnd:CreatingTableRepeatingSectionMappedToCustomXmlPart Console.WriteLine("\nCreation of a Table Repeating Section Mapped To a Custom Xml Part is successfull."); }
public void FillTableUsingRepeatingSectionItem() { //ExStart //ExFor:SdtType //ExSummary:Shows how to fill the table with data contained in the XML part. Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); CustomXmlPart xmlPart = doc.CustomXmlParts.Add("Books", "<books>" + "<book><title>Everyday Italian</title>" + "<author>Giada De Laurentiis</author></book>" + "<book><title>Harry Potter</title>" + "<author>J. K. Rowling</author></book>" + "<book><title>Learning XML</title>" + "<author>Erik T. Ray</author></book>" + "</books>"); // Create headers for data from xml content Table table = builder.StartTable(); builder.InsertCell(); builder.Write("Title"); builder.InsertCell(); builder.Write("Author"); builder.EndRow(); builder.EndTable(); // Create table with RepeatingSection inside StructuredDocumentTag repeatingSectionSdt = new StructuredDocumentTag(doc, SdtType.RepeatingSection, MarkupLevel.Row); repeatingSectionSdt.XmlMapping.SetMapping(xmlPart, "/books[1]/book", string.Empty); table.AppendChild(repeatingSectionSdt); // Add RepeatingSectionItem inside RepeatingSection and mark it as a row StructuredDocumentTag repeatingSectionItemSdt = new StructuredDocumentTag(doc, SdtType.RepeatingSectionItem, MarkupLevel.Row); repeatingSectionSdt.AppendChild(repeatingSectionItemSdt); Row row = new Row(doc); repeatingSectionItemSdt.AppendChild(row); // Map xml data with created table cells for book title and author StructuredDocumentTag titleSdt = new StructuredDocumentTag(doc, SdtType.PlainText, MarkupLevel.Cell); titleSdt.XmlMapping.SetMapping(xmlPart, "/books[1]/book[1]/title[1]", string.Empty); row.AppendChild(titleSdt); StructuredDocumentTag authorSdt = new StructuredDocumentTag(doc, SdtType.PlainText, MarkupLevel.Cell); authorSdt.XmlMapping.SetMapping(xmlPart, "/books[1]/book[1]/author[1]", string.Empty); row.AppendChild(authorSdt); doc.Save(ArtifactsDir + "StructuredDocumentTag.RepeatingSectionItem.docx"); //ExEnd doc = new Document(ArtifactsDir + "StructuredDocumentTag.RepeatingSectionItem.docx"); List <StructuredDocumentTag> tags = doc.GetChildNodes(NodeType.StructuredDocumentTag, true).OfType <StructuredDocumentTag>().ToList(); Assert.AreEqual("/books[1]/book", tags[0].XmlMapping.XPath); Assert.AreEqual(string.Empty, tags[0].XmlMapping.PrefixMappings); Assert.AreEqual(string.Empty, tags[1].XmlMapping.XPath); Assert.AreEqual(string.Empty, tags[1].XmlMapping.PrefixMappings); Assert.AreEqual("/books[1]/book[1]/title[1]", tags[2].XmlMapping.XPath); Assert.AreEqual(string.Empty, tags[2].XmlMapping.PrefixMappings); Assert.AreEqual("/books[1]/book[1]/author[1]", tags[3].XmlMapping.XPath); Assert.AreEqual(string.Empty, tags[3].XmlMapping.PrefixMappings); Assert.AreEqual("Title\u0007Author\u0007\u0007" + "Everyday Italian\u0007Giada De Laurentiis\u0007\u0007" + "Harry Potter\u0007J. K. Rowling\u0007\u0007" + "Learning XML\u0007Erik T. Ray\u0007\u0007", doc.GetChild(NodeType.Table, 0, true).GetText().Trim()); }
public void FillTableUsingRepeatingSectionItem() { //ExStart //ExFor:SdtType //ExSummary:Shows how to fill a table with data from in an XML part. Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); CustomXmlPart xmlPart = doc.CustomXmlParts.Add("Books", "<books>" + "<book>" + "<title>Everyday Italian</title>" + "<author>Giada De Laurentiis</author>" + "</book>" + "<book>" + "<title>The C Programming Language</title>" + "<author>Brian W. Kernighan, Dennis M. Ritchie</author>" + "</book>" + "<book>" + "<title>Learning XML</title>" + "<author>Erik T. Ray</author>" + "</book>" + "</books>"); // Create headers for data from the XML content. Table table = builder.StartTable(); builder.InsertCell(); builder.Write("Title"); builder.InsertCell(); builder.Write("Author"); builder.EndRow(); builder.EndTable(); // Create a table with a repeating section inside. StructuredDocumentTag repeatingSectionSdt = new StructuredDocumentTag(doc, SdtType.RepeatingSection, MarkupLevel.Row); repeatingSectionSdt.XmlMapping.SetMapping(xmlPart, "/books[1]/book", string.Empty); table.AppendChild(repeatingSectionSdt); // Add repeating section item inside the repeating section and mark it as a row. // This table will have a row for each element that we can find in the XML document // using the "/books[1]/book" XPath, of which there are three. StructuredDocumentTag repeatingSectionItemSdt = new StructuredDocumentTag(doc, SdtType.RepeatingSectionItem, MarkupLevel.Row); repeatingSectionSdt.AppendChild(repeatingSectionItemSdt); Row row = new Row(doc); repeatingSectionItemSdt.AppendChild(row); // Map XML data with created table cells for the title and author of each book. StructuredDocumentTag titleSdt = new StructuredDocumentTag(doc, SdtType.PlainText, MarkupLevel.Cell); titleSdt.XmlMapping.SetMapping(xmlPart, "/books[1]/book[1]/title[1]", string.Empty); row.AppendChild(titleSdt); StructuredDocumentTag authorSdt = new StructuredDocumentTag(doc, SdtType.PlainText, MarkupLevel.Cell); authorSdt.XmlMapping.SetMapping(xmlPart, "/books[1]/book[1]/author[1]", string.Empty); row.AppendChild(authorSdt); doc.Save(ArtifactsDir + "StructuredDocumentTag.RepeatingSectionItem.docx"); //ExEnd doc = new Document(ArtifactsDir + "StructuredDocumentTag.RepeatingSectionItem.docx"); List <StructuredDocumentTag> tags = doc.GetChildNodes(NodeType.StructuredDocumentTag, true).OfType <StructuredDocumentTag>().ToList(); Assert.AreEqual("/books[1]/book", tags[0].XmlMapping.XPath); Assert.AreEqual(string.Empty, tags[0].XmlMapping.PrefixMappings); Assert.AreEqual(string.Empty, tags[1].XmlMapping.XPath); Assert.AreEqual(string.Empty, tags[1].XmlMapping.PrefixMappings); Assert.AreEqual("/books[1]/book[1]/title[1]", tags[2].XmlMapping.XPath); Assert.AreEqual(string.Empty, tags[2].XmlMapping.PrefixMappings); Assert.AreEqual("/books[1]/book[1]/author[1]", tags[3].XmlMapping.XPath); Assert.AreEqual(string.Empty, tags[3].XmlMapping.PrefixMappings); Assert.AreEqual("Title\u0007Author\u0007\u0007" + "Everyday Italian\u0007Giada De Laurentiis\u0007\u0007" + "The C Programming Language\u0007Brian W. Kernighan, Dennis M. Ritchie\u0007\u0007" + "Learning XML\u0007Erik T. Ray\u0007\u0007", doc.FirstSection.Body.Tables[0].GetText().Trim()); }
public void FillTableUsingRepeatingSectionItem() { //ExStart //ExFor:SdtType //ExSummary:Shows how to fill the table with data contained in the XML part. Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); CustomXmlPart xmlPart = doc.CustomXmlParts.Add("Books", "<books>" + "<book><title>Everyday Italian</title>" + "<author>Giada De Laurentiis</author></book>" + "<book><title>Harry Potter</title>" + "<author>J K. Rowling</author></book>" + "<book><title>Learning XML</title>" + "<author>Erik T. Ray</author></book>" + "</books>"); // Create headers for data from xml content Table table = builder.StartTable(); builder.InsertCell(); builder.Write("Title"); builder.InsertCell(); builder.Write("Author"); builder.EndRow(); builder.EndTable(); // Create table with RepeatingSection inside StructuredDocumentTag repeatingSectionSdt = new StructuredDocumentTag(doc, SdtType.RepeatingSection, MarkupLevel.Row); repeatingSectionSdt.XmlMapping.SetMapping(xmlPart, "/books[1]/book", ""); table.AppendChild(repeatingSectionSdt); // Add RepeatingSectionItem inside RepeatingSection and mark it as a row StructuredDocumentTag repeatingSectionItemSdt = new StructuredDocumentTag(doc, SdtType.RepeatingSectionItem, MarkupLevel.Row); repeatingSectionSdt.AppendChild(repeatingSectionItemSdt); Row row = new Row(doc); repeatingSectionItemSdt.AppendChild(row); // Map xml data with created table cells for book title and author StructuredDocumentTag titleSdt = new StructuredDocumentTag(doc, SdtType.PlainText, MarkupLevel.Cell); titleSdt.XmlMapping.SetMapping(xmlPart, "/books[1]/book[1]/title[1]", ""); row.AppendChild(titleSdt); StructuredDocumentTag authorSdt = new StructuredDocumentTag(doc, SdtType.PlainText, MarkupLevel.Cell); authorSdt.XmlMapping.SetMapping(xmlPart, "/books[1]/book[1]/author[1]", ""); row.AppendChild(authorSdt); doc.Save(ArtifactsDir + "StructuredDocumentTag.RepeatingSectionItem.docx"); //ExEnd }
//public List<string> GetDocumentTags(byte[] buffer) //{ // if (buffer == null) throw new ArgumentNullException(nameof(buffer)); // Document document; // using (var ms = new MemoryStream(buffer)) // { // document = new Document(ms); // } // return document // .GetChildNodes(NodeType.StructuredDocumentTag, true) // .ToArray() // .Select(x => ((StructuredDocumentTag)x).Title) // .ToList(); //} private void FillUserInputControl(StructuredDocumentTag std, Dictionary <string, string> userInputFieldsDictionaty, Document document, DocumentBuilder builder, string defaultValue) { //std.RemoveAllChildren(); Paragraph paragraph; var html = userInputFieldsDictionaty[std.Title.Trim()]; if (string.IsNullOrEmpty(html)) { return; } html = TrimParagraphTag(html); html = ResizeBase64Images(html); //была проблема при вставке html если тег находился в параграфе //если Content Control находится не в Body, а во вложенном теге if (std.ParentNode.NodeType != NodeType.Body) { //проверяем текущее поле на простой текст без форматирования if (std.Title.Trim().EndsWith("_UserInput")) { if (std.ParentNode.NodeType == NodeType.Paragraph) { var run = new Run(document, html); // set font Inline inlineElement = GetFirstInline(std.ChildNodes); if (inlineElement != null) { CopyFont(run.Font, inlineElement.Font); } std.RemoveAllChildren(); std.AppendChild(run); std.RemoveSelfOnly(); } else { paragraph = (Paragraph)std.ParentNode.InsertAfter(new Paragraph(document), std); // set font Paragraph paragraphSource = (Paragraph)std.FirstChild; Inline inlineElement = GetFirstInline(paragraphSource.ChildNodes); var run = new Run(document, html); if (inlineElement != null) { CopyFont(run.Font, inlineElement.Font); } paragraph.AppendChild(run); std.Remove(); } } //если поле со значение по умолчанию else if (std.Title.Trim().EndsWith("_UserInputDefault")) { html = html.Equals(string.Empty) ? defaultValue : html; if (std.ParentNode.NodeType == NodeType.Paragraph) { var run = new Run(document, html); // set font Inline inlineElement = GetFirstInline(std.ChildNodes); if (inlineElement != null) { CopyFont(run.Font, inlineElement.Font); } std.RemoveAllChildren(); std.AppendChild(run); std.RemoveSelfOnly(); } else { paragraph = (Paragraph)std.ParentNode.InsertAfter(new Paragraph(document), std); // set font Paragraph paragraphSource = (Paragraph)std.FirstChild; Inline inlineElement = GetFirstInline(paragraphSource.ChildNodes); var run = new Run(document, html); if (inlineElement != null) { CopyFont(run.Font, inlineElement.Font); } paragraph.AppendChild(run); std.Remove(); } } //если текст из wysiwyg редактора else { std.RemoveAllChildren(); if (std.ParentNode.NodeType == NodeType.Paragraph) { builder.MoveTo(std); builder.InsertHtml(html, true); std.RemoveSelfOnly(); } else { paragraph = (Paragraph)std.ParentNode.InsertAfter(new Paragraph(document), std); builder.MoveTo(paragraph); builder.InsertHtml(html, true); std.AppendChild(paragraph); std.RemoveSelfOnly(); } } } //если Content Control находится в корне тега Body else { if (std.Title.Trim().EndsWith("_UserInput")) { paragraph = (Paragraph)std.ParentNode.InsertAfter(new Paragraph(document), std); // set font Paragraph paragraphSource = (Paragraph)std.FirstChild; Inline inlineElement = GetFirstInline(paragraphSource.ChildNodes); var run = new Run(document, html); if (inlineElement != null) { CopyFont(run.Font, inlineElement.Font); } paragraph.AppendChild(run); std.Remove(); } else if (std.Title.Trim().EndsWith("_UserInputDefault")) { html = html.Equals(string.Empty) ? defaultValue : html; paragraph = (Paragraph)std.ParentNode.InsertAfter(new Paragraph(document), std); // set font Paragraph paragraphSource = (Paragraph)std.FirstChild; Inline inlineElement = GetFirstInline(paragraphSource.ChildNodes); var run = new Run(document, html); if (inlineElement != null) { CopyFont(run.Font, inlineElement.Font); } paragraph.AppendChild(run); std.Remove(); } //если текст из wysiwyg редактора else { paragraph = (Paragraph)std.ParentNode.InsertAfter(new Paragraph(document), std); builder.MoveTo(paragraph); builder.InsertHtml(html, true); std.Remove(); } } }