Пример #1
0
        private static void CopyPageFillTemplate(DataRowView drv, Body wordBody, SimplePropertyCollection templateSimples)
        {
            foreach (SdtElement fillItem in wordBody.Descendants <SdtElement>())
            {
                foreach (SdtAlias sdtAlias in fillItem.Descendants <SdtAlias>())
                {
                    DCTSimpleProperty dcts = templateSimples[sdtAlias.Val];
                    if (dcts != null)
                    {
                        var items = fillItem.Descendants <Run>();
                        if (items.Count() > 0)
                        {
                            Run runElement = items.First();
                            runElement.RemoveAllChildren <Text>();
                            runElement.AppendChild <Text>(new Text(GeneralFormatter.ToString(drv[dcts.TagID], dcts.FormatString)));

                            if (dcts.IsReadOnly)
                            {
                                Lock lockControl = new Lock();
                                lockControl.Val = LockingValues.SdtContentLocked;
                                fillItem.SdtProperties.Append(lockControl);
                            }
                        }
                    }
                }
            }
        }
Пример #2
0
        public static byte[] CopyPageFillData(byte[] templateBuffer, DataView dv, SimplePropertyCollection simpleTemplate)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                ms.Write(templateBuffer, 0, templateBuffer.Length);
                using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(ms, true))
                {
                    Body wordBody = wordDoc.MainDocumentPart.Document.Body;
                    List <OpenXmlElement> ChildElements         = wordBody.ChildElements.ToList <OpenXmlElement>();
                    SectionProperties     bodySectionProperties = wordBody.Descendants <SectionProperties>().LastOrDefault();
                    bool tagFirst = true;
                    int  i        = 1;
                    foreach (DataRowView drv in dv)
                    {
                        if (i == 1)
                        {
                            CopyPageFillTemplate(wordBody, simpleTemplate, (tagID) => drv[tagID]);
                            i++;
                            continue;
                        }

                        foreach (OpenXmlElement item in ChildElements)
                        {
                            if (item is SectionProperties)
                            {
                                break;
                            }

                            OpenXmlElement addItem = (OpenXmlElement)item.CloneNode(true);

                            if (tagFirst)
                            {
                                AddPageEnd(wordBody, i, bodySectionProperties);
                                tagFirst = false;
                            }

                            ClonePageAndFill(drv, item, ref addItem, simpleTemplate);
                            wordBody.InsertBefore <OpenXmlElement>(addItem, bodySectionProperties);
                        }
                        tagFirst = true;
                        i++;
                    }
                }
                return(ms.ToArray());
            }
        }
Пример #3
0
        private static void ClonePageAndFill(DataRowView drv, OpenXmlElement item, ref OpenXmlElement addItem, SimplePropertyCollection templateSimples)
        {
            foreach (SdtElement sdtElement in item.Descendants <SdtElement>())
            {
                foreach (SdtAlias ite11 in sdtElement.Descendants <SdtAlias>())
                {
                    DCTSimpleProperty temSimple = templateSimples[ite11.Val];
                    if (temSimple != null)
                    {
                        var items = sdtElement.Descendants <Run>();
                        if (items.Count() > 0)
                        {
                            Run runElement = items.First();
                            runElement.RemoveAllChildren <Text>();
                            runElement.AppendChild <Text>(new Text(GeneralFormatter.ToString(drv[temSimple.TagID], temSimple.FormatString)));

                            if (temSimple.IsReadOnly)
                            {
                                Lock lockControl = new Lock();
                                lockControl.Val = LockingValues.SdtContentLocked;
                                sdtElement.SdtProperties.Append(lockControl);
                            }
                        }
                    }
                }
            }
        }