public byte[] CreateSchemaDocument(SchemaDocumentData schemaData, string templateLocation) { byte[] binFile = null; byte[] byteArray = File.ReadAllBytes(templateLocation); using (MemoryStream mem = new MemoryStream()) { mem.Write(byteArray, 0, byteArray.Length); using (WordprocessingDocument doc = WordprocessingDocument.Open(mem, true)) { Paragraph header = doc.MainDocumentPart.Document.Body.GetFirstChild<Paragraph>(); header.ChildElements.First(x => x is Run).Append(new Text(String.Format(" \"{0}\"", schemaData.Title))); Table table = doc.MainDocumentPart.Document.Body.GetFirstChild<Table>(); RunFonts textFonts = table.GetFirstChild<TableRow>().GetFirstChild<TableCell>().GetFirstChild<Paragraph>(). GetFirstChild<ParagraphProperties>().GetFirstChild<ParagraphMarkRunProperties>(). GetFirstChild<RunFonts>(); string paragraphPropertiesOuterXml = getParagraphPropertiesXml(textFonts); string runPropertiesOuterXml = getRunPropertiesXml(textFonts); table.Elements<TableRow>().ElementAt(0).Elements<TableCell>().ElementAt(1).Elements<Paragraph>().ElementAt(0).Append(new Run(new RunProperties(runPropertiesOuterXml), new Text(schemaData.Title))); table.Elements<TableRow>().ElementAt(1).Elements<TableCell>().ElementAt(1).Elements<Paragraph>().ElementAt(0).Append(new Run(new RunProperties(runPropertiesOuterXml), new Text(schemaData.Description))); table.Elements<TableRow>().ElementAt(2).Elements<TableCell>().ElementAt(1).Elements<Paragraph>().ElementAt(0).Append(new Run(new RunProperties(runPropertiesOuterXml), new Text(schemaData.SchemaType + TEXT_SCHEMA))); table.Elements<TableRow>().ElementAt(3).Elements<TableCell>().ElementAt(1).Elements<Paragraph>().ElementAt(0).Append(new Run(new RunProperties(runPropertiesOuterXml), new Text(schemaData.RootElementName))); table.Elements<TableRow>().ElementAt(4).Elements<TableCell>().ElementAt(1).Elements<Paragraph>().ElementAt(0).Append(new Run(new RunProperties(runPropertiesOuterXml), new Text(schemaData.NamespaceUri))); // Save properties for metadata row TableRowProperties metaDataRowProperties = table.Elements<TableRow>().ElementAt(5).GetFirstChild<TableRowProperties>(); TableCellProperties metaDataCellProperties = table.Elements<TableRow>().ElementAt(5).GetFirstChild<TableCell>().GetFirstChild<TableCellProperties>(); ParagraphProperties metaDataParagraphProperties = table.Elements<TableRow>().ElementAt(5).GetFirstChild<TableCell>().GetFirstChild<Paragraph>().GetFirstChild<ParagraphProperties>(); RunProperties metaDataRunProperties = table.Elements<TableRow>().ElementAt(5).GetFirstChild<TableCell>().GetFirstChild<Paragraph>().GetFirstChild<Run>().GetFirstChild<RunProperties>(); // Clear empty rows foreach (TableRow row in table.Elements<TableRow>()) { Text text = row.Elements<TableCell>().ElementAt(1).GetFirstChild<Paragraph>().GetFirstChild<Run>().GetFirstChild<Text>(); if (string.IsNullOrEmpty(text.InnerText.Trim())) { table.RemoveChild(row); } } foreach (SchemaFieldDocumentData field in schemaData.Fields) { TableRow tr = new TableRow(); TableCell tc1 = new TableCell(); tc1.Append(new Paragraph(new ParagraphProperties(paragraphPropertiesOuterXml), new Run(new RunProperties(runPropertiesOuterXml), new Text(field.Description)))); tr.Append(tc1); TableCell tc2 = new TableCell(); tc2.Append(new Paragraph(new ParagraphProperties(paragraphPropertiesOuterXml), new Run(new RunProperties(runPropertiesOuterXml), new Text(field.XmlName)))); tr.Append(tc2); TableCell tc3 = new TableCell(); tc3.Append(new Paragraph(new ParagraphProperties(paragraphPropertiesOuterXml), new Run(new RunProperties(runPropertiesOuterXml), new Text(field.FieldType)))); tr.Append(tc3); TableCell tc4 = new TableCell(); tc4.Append(new Paragraph(new ParagraphProperties(paragraphPropertiesOuterXml), new Run(new RunProperties(runPropertiesOuterXml), new Text((field.Required) ? TEXT_YES : TEXT_NO)))); tr.Append(tc4); TableCell tc5 = new TableCell(); tc5.Append(new Paragraph(new ParagraphProperties(paragraphPropertiesOuterXml), new Run(new RunProperties(runPropertiesOuterXml), new Text((field.MultiValue) ? TEXT_YES : TEXT_NO)))); tr.Append(tc5); TableCell tc6 = new TableCell(); tc6.Append(new Paragraph(new ParagraphProperties(paragraphPropertiesOuterXml), new Run(new RunProperties(runPropertiesOuterXml), new Text(field.Properties)))); tr.Append(tc6); table.Append(tr); } if (schemaData.MetadataFields.Count > 0 && schemaData.SchemaType == "Component") { TableRow tr = new TableRow(); TableCell tc = new TableCell(); TableCellProperties tcProperties = new TableCellProperties(metaDataCellProperties.OuterXml); GridSpan gridSpan = new GridSpan { Val = 6 }; tcProperties.Append(gridSpan); tr.Append(new TableRowProperties(metaDataRowProperties.OuterXml)); tc.Append(tcProperties); tc.Append(new Paragraph(new ParagraphProperties(metaDataParagraphProperties.OuterXml), new Run(new RunProperties(metaDataRunProperties.OuterXml), new Text(TEXT_METADATA)))); tr.Append(tc); table.Append(tr); } foreach (SchemaFieldDocumentData field in schemaData.MetadataFields) { TableRow tr = new TableRow(); TableCell tc1 = new TableCell(); tc1.Append(new Paragraph(new ParagraphProperties(paragraphPropertiesOuterXml), new Run(new RunProperties(runPropertiesOuterXml), new Text(field.Description)))); tr.Append(tc1); TableCell tc2 = new TableCell(); tc2.Append(new Paragraph(new ParagraphProperties(paragraphPropertiesOuterXml), new Run(new RunProperties(runPropertiesOuterXml), new Text(field.XmlName)))); tr.Append(tc2); TableCell tc3 = new TableCell(); tc3.Append(new Paragraph(new ParagraphProperties(paragraphPropertiesOuterXml), new Run(new RunProperties(runPropertiesOuterXml), new Text(field.FieldType)))); tr.Append(tc3); TableCell tc4 = new TableCell(); tc4.Append(new Paragraph(new ParagraphProperties(paragraphPropertiesOuterXml), new Run(new RunProperties(runPropertiesOuterXml), new Text((field.Required) ? TEXT_YES : TEXT_NO)))); tr.Append(tc4); TableCell tc5 = new TableCell(); tc5.Append(new Paragraph(new ParagraphProperties(paragraphPropertiesOuterXml), new Run(new RunProperties(runPropertiesOuterXml), new Text((field.MultiValue) ? TEXT_YES : TEXT_NO)))); tr.Append(tc5); TableCell tc6 = new TableCell(); tc6.Append(new Paragraph(new ParagraphProperties(paragraphPropertiesOuterXml), new Run(new RunProperties(runPropertiesOuterXml), new Text(field.Properties)))); tr.Append(tc6); table.Append(tr); } } binFile = mem.ToArray(); } return binFile; }
public static SchemaDocumentData GetSchemaData(ILocalClient client, string id) { SchemaData item; SchemaFieldsData schemaFields; try { item = client.Read(id, new ReadOptions()) as SchemaData; schemaFields = client.ReadSchemaFields(id, true, new ReadOptions()); } catch (Exception ex) { throw ex; } SchemaDocumentData schema = new SchemaDocumentData(); schema.Title = item.Title; schema.Description = item.Description; schema.RootElementName = item.RootElementName; schema.NamespaceUri = item.NamespaceUri; schema.LocationInfo = item.LocationInfo.Path; schema.SchemaType = item.Purpose.Value.ToString(); schema.Fields = GetFieldsDescription(schemaFields.Fields); schema.MetadataFields = GetFieldsDescription(schemaFields.MetadataFields); return schema; }