Esempio n. 1
0
        public static TbbDocumentData GetTBBData(ILocalClient client, string id)
        {
            TemplateBuildingBlockData item = client.Read(id, new ReadOptions()) as TemplateBuildingBlockData;

            if (item == null)
                return null;

            TbbDocumentData tbb = new TbbDocumentData();
            tbb.Title = item.Title;
            tbb.TbbType = item.TemplateType;

            return tbb;
        }
        public byte[] CreateTbbDocument(TbbDocumentData tbbData, 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}\"", tbbData.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 runPropertiesOuterXml = getRunPropertiesXml(textFonts);

                    table.Elements<TableRow>().ElementAt(0).Elements<TableCell>().ElementAt(1).Elements<Paragraph>().ElementAt(0).Append(new Run(new RunProperties(runPropertiesOuterXml), new Text(tbbData.Title)));
                    table.Elements<TableRow>().ElementAt(1).Elements<TableCell>().ElementAt(1).Elements<Paragraph>().ElementAt(0).Append(new Run(new RunProperties(runPropertiesOuterXml), new Text(tbbData.TbbType)));

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

                binFile = mem.ToArray();
            }

            return binFile;
        }