public byte[] CreateWordDocument(Schema schemaData, string templateLocation)
        {

            byte[] binFile = null;


            byte[] byteArray = File.ReadAllBytes(templateLocation);
            using (MemoryStream mem = new MemoryStream())
            {
                mem.Write(byteArray, 0, (int) byteArray.Length);

                using (WordprocessingDocument doc = WordprocessingDocument.Open(mem, true))
                {
                    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 (SchemaField 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 (SchemaField 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;
        }
        private Schema getSchemaData(string id)
        {

            SchemaData item = null;
            SchemaFieldsData schemaFields = null;

            try
            {

                using (var coreService = Client.GetCoreService())
                {
                    item = coreService.Read(id, new ReadOptions()) as SchemaData;
                    schemaFields = coreService.ReadSchemaFields(id, true, new ReadOptions());
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }



            Schema schema = new Schema();

            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;
        }