Ejemplo n.º 1
0
        private int ProcessTemplate(ArrayTemplate template, Table table, Model context)
        {
            if (!(context.Find(template.Start.ModelDescription.Expression) is CollectionModel collection))
            {
                return(template.End.Position.RowIndex + 1);
            }

            var resultRows = new List <TableRow>();

            foreach (var item in collection.Items)
            {
                foreach (var row in template.OpenXml.Elements.Select(e => e.CloneNode(true)).Cast <TableRow>())
                {
                    this.ProcessCellsOfRow(row, item);
                    resultRows.Add(row);
                }
            }

            var originalRows = table.Rows()
                               .GetTemplateRows(template)
                               .ToArray();

            for (var i = 0; i < resultRows.Count; i++)
            {
                originalRows.First().InsertBeforeSelf(resultRows[i]);
            }

            originalRows.RemoveSelfFromParent();

            return(template.Start.Position.RowIndex + resultRows.Count);
        }
Ejemplo n.º 2
0
        private (Paragraph, int) ProcessTemplate(
            ArrayTemplate template,
            IReadOnlyCollection <Paragraph> bodyParagraphs,
            Model context)
        {
            if (!(context.Find(template.Start.ModelDescription.Expression) is CollectionModel collection))
            {
                return(null, 0);
            }

            var startParagraph = bodyParagraphs.ElementAt(template.Start.Position.ParagraphIndex);
            var endParagraph   = bodyParagraphs.ElementAt(template.End.Position.ParagraphIndex);

            if (startParagraph != endParagraph)
            {
                var s = startParagraph.NextSibling();
                while (s != endParagraph)
                {
                    var t = s;
                    s = t.NextSibling();
                    t.Remove();
                }
            }

            var result = new List <OpenXmlElement>();
            var compositeElementProcessor = new CompositeElementProcessor(_engineConfig, _imageProcessor, _logger);

            foreach (var item in collection.Items)
            {
                var itemBody = template.OpenXml.CreateBody();
                compositeElementProcessor.Process(itemBody, item);

                result.AddRange(itemBody.ChildElements.Select(e => e.CloneNode(true)));
            }

            startParagraph.ReplaceToken(template.Start, Model.Empty, _imageProcessor);
            var textEnd = endParagraph.ReplaceToken(template.End, Model.Empty, _imageProcessor);

            foreach (var e in result)
            {
                endParagraph.InsertBeforeSelf(e);
            }

            return(endParagraph, textEnd);
        }