Beispiel #1
0
        public static bool IsInTableWithTwoColumns(this Paragraph paragraph)
        {
            if (paragraph == null)
            {
                throw new ArgumentNullException(nameof(paragraph));
            }

            var tables = paragraph.Ancestors <Table>();

            if (tables.Any())
            {
                if (tables.Count() > 1)
                {
                    return(false);
                }

                var table = tables.First();

                var columns = table.Descendants <GridColumn>();

                if (columns.Count() == 2)
                {
                    return(true);
                }
            }

            return(false);
        }
Beispiel #2
0
        [ElementProcessing] // require refactoring (again)
        public bool ProcessParagraph(IElementProcessingState state, Paragraph p)
        {
            var props = p.ParagraphProperties;

            state.SetContext(props);
            state.ProcessingState.GetContext(out ICssRegistrator registrator);
            state.GetContext(out ParagraphContext pContext);
            var parentVNode = pContext.Parent;

            // Build and register class
            var param = new ParagraphClassParam(props);

            // Add table properties
            if (state.GetContext(out TableContext tableContext))
            {
                var tableCell = p.Ancestors <TableCell>().FirstOrDefault();
                if (tableCell != null)
                {
                    var cellState = tableContext.GetCell(tableCell);
                    param.TableProperties = cellState.ParagraphProperties;
                }
            }

            // Add numbering properties
            if (state.GetContext(out NumberingContext numberingContext) &&
                props != null)
            {
                // Add the numbering to the cls param
                param.NumberingId    = numberingContext.NumberingId;
                param.NumberingLevel = numberingContext.Level;

                // Register the run mark and add the class to the numbering numbering
                var numberingCls = registrator.RegisterRun(new RunClassParam
                {
                    InlineProperties = props.ParagraphMarkRunProperties,
                    ParagraphStyleId = param.StyleId,
                    NumberingId      = numberingContext.NumberingId,
                    NumberingLevel   = numberingContext.Level
                });
                parentVNode.AddClasses(_config.ContainerWithNumberingCssClassSuffix.Substring(1));
                numberingContext.NumberingNumber.AddClasses(numberingCls.Name);
            }

            // Add the class to the VNode containing the paragraph
            var pCls = registrator.RegisterParagraph(param);

            parentVNode.AddClasses(pCls.Name);

            // Set the paragraph css context
            state.SetContext(new ParagraphCssContext
            {
                CssClass = pCls
            });

            return(false);
        }
Beispiel #3
0
        public static bool IsInFirstColumn(this Paragraph paragraph)
        {
            if (paragraph == null)
            {
                throw new ArgumentNullException(nameof(paragraph));
            }

            var cell = paragraph.Ancestors <TableCell>().FirstOrDefault();

            if (cell == null)
            {
                return(false);
            }

            return(cell.PreviousSibling <TableCell>() == null);
        }
Beispiel #4
0
        public static Paragraph GetLastParagraphFromSecondColumn(this Paragraph paragraph)
        {
            if (paragraph == null)
            {
                throw new ArgumentNullException(nameof(paragraph));
            }

            var cell = paragraph.Ancestors <TableCell>().FirstOrDefault();

            if (cell == null)
            {
                return(null);
            }

            var secondCell = cell.NextSibling <TableCell>();

            if (secondCell == null)
            {
                return(null);
            }

            return(secondCell.Descendants <Paragraph>().LastOrDefault(p => p.IsGood()));
        }
Beispiel #5
0
        public void OpenXmlElementTraversingMethodsTest()
        {
            var para = new Paragraph();
            var r1   = para.AppendChild(new Run());
            var b1   = r1.AppendChild(new RunProperties()).AppendChild(new Bold());
            var t1   = r1.AppendChild(new Text());

            var r2 = para.AppendChild(new Run());

            r2.AppendChild(new Text());

            var r3 = para.AppendChild(new Run());

            Assert.Equal(7, para.Descendants().Count());
            Assert.Equal(3, r1.Descendants().Count());
            Assert.Single(r2.Descendants());
            Assert.Empty(r3.Descendants());

            Assert.Same(r1, para.Descendants().First());
            Assert.Same(b1, para.Descendants().ElementAt(2));
            Assert.Same(t1, para.Descendants().ElementAt(3));
            Assert.Same(r2, para.Descendants().ElementAt(4));
            Assert.Same(r3, para.Descendants().Last());

            // ancestors
            Assert.Empty(para.Ancestors());
            Assert.Empty(para.Ancestors <Body>());
            Assert.Single(r1.Ancestors());
            Assert.Single(r1.Ancestors <Paragraph>());
            Assert.Empty(r1.Ancestors <Body>());
            Assert.Single(r3.Ancestors());
            Assert.Single(r3.Ancestors <Paragraph>());
            Assert.Empty(r3.Ancestors <Body>());
            Assert.Equal(2, t1.Ancestors().Count());
            Assert.Single(t1.Ancestors <Paragraph>());
            Assert.Empty(t1.Ancestors <Body>());
            Assert.Equal(3, b1.Ancestors().Count());
            Assert.Single(b1.Ancestors <Paragraph>());
            Assert.Single(b1.Ancestors <Run>());
            Assert.Single(b1.Ancestors <RunProperties>());
            Assert.Empty(b1.Ancestors <Body>());

            Assert.Same(para, r1.Ancestors().First());
            Assert.Same(para, r1.Ancestors <Paragraph>().First());
            Assert.Same(r1, t1.Ancestors().First());
            Assert.Same(para, t1.Ancestors().Last());
            Assert.Same(r1, t1.Ancestors <Run>().First());
            Assert.Same(para, t1.Ancestors <Paragraph>().First());
            Assert.Same(para, b1.Ancestors().Last());
            Assert.Same(r1, b1.Ancestors().ElementAt(1));

            // IsBefore() / IsAfter()
            Assert.False(para.IsBefore(para));
            Assert.False(para.IsAfter(para));
            Assert.False(r2.IsBefore(r2));
            Assert.False(r2.IsAfter(r2));

            Assert.True(para.IsBefore(r1));
            Assert.False(para.IsAfter(r1));
            Assert.True(para.IsBefore(b1));
            Assert.False(para.IsAfter(b1));
            Assert.False(r1.IsBefore(para));
            Assert.True(r1.IsAfter(para));
            Assert.False(r1.IsBefore(para));
            Assert.True(r1.IsAfter(para));

            Assert.True(r1.IsBefore(r2));
            Assert.False(r1.IsAfter(r2));
            Assert.False(r3.IsBefore(r1));
            Assert.True(r3.IsAfter(r1));
            Assert.False(b1.IsAfter(r2));
            Assert.True(b1.IsBefore(r2));

            Assert.True(r1.IsBefore(b1));
            Assert.False(r1.IsAfter(b1));
            Assert.True(b1.IsAfter(r1));
            Assert.False(b1.IsBefore(r1));

            var p2   = new Paragraph();
            var p2r1 = p2.AppendChild(new Run());

            Assert.False(p2.IsBefore(para));
            Assert.False(p2.IsAfter(para));
            Assert.False(p2r1.IsAfter(para));
            Assert.False(p2r1.IsBefore(para));

            // ElementsBefore / ElementsAfter
            Assert.False(para.ElementsBefore().Any());
            Assert.False(para.ElementsAfter().Any());
            Assert.True(r1.ElementsAfter().Any());
            Assert.False(r1.ElementsBefore().Any());
            Assert.False(r3.ElementsAfter().Any());
            Assert.True(r3.ElementsBefore().Any());
            Assert.True(r2.ElementsAfter().Any());
            Assert.True(r2.ElementsBefore().Any());
            Assert.Same(r1, r3.ElementsBefore().First());
            Assert.Same(r2, r3.ElementsBefore().Last());
            Assert.Same(r2, r1.ElementsAfter().First());
            Assert.Same(r3, r1.ElementsAfter().Last());
            Assert.False(b1.ElementsBefore().Any());
            Assert.False(b1.ElementsAfter().Any());
        }
        public void OpenXmlElementTraversingMethodsTest()
        {
            Paragraph para = new Paragraph();
            Run r1 = para.AppendChild(new Run());
            Bold b1 = r1.AppendChild(new RunProperties()).AppendChild(new Bold());
            Text t1 = r1.AppendChild(new Text());

            Run r2 = para.AppendChild(new Run());
            r2.AppendChild(new Text());

            Run r3 = para.AppendChild(new Run());

            Assert.Equal(7, para.Descendants().Count());
            Assert.Equal(3, r1.Descendants().Count());
            Assert.Equal(1, r2.Descendants().Count());
            Assert.Equal(0, r3.Descendants().Count());

            Assert.Same(r1, para.Descendants().First());
            Assert.Same(b1, para.Descendants().ElementAt(2));
            Assert.Same(t1, para.Descendants().ElementAt(3));
            Assert.Same(r2, para.Descendants().ElementAt(4));
            Assert.Same(r3, para.Descendants().Last());

            // ancestors
            Assert.Equal(0, para.Ancestors().Count());
            Assert.Equal(0, para.Ancestors<Body>().Count());
            Assert.Equal(1, r1.Ancestors().Count());
            Assert.Equal(1, r1.Ancestors<Paragraph>().Count());
            Assert.Equal(0, r1.Ancestors<Body>().Count());
            Assert.Equal(1, r3.Ancestors().Count());
            Assert.Equal(1, r3.Ancestors<Paragraph>().Count());
            Assert.Equal(0, r3.Ancestors<Body>().Count());
            Assert.Equal(2, t1.Ancestors().Count());
            Assert.Equal(1, t1.Ancestors<Paragraph>().Count());
            Assert.Equal(0, t1.Ancestors<Body>().Count());
            Assert.Equal(3, b1.Ancestors().Count());
            Assert.Equal(1, b1.Ancestors<Paragraph>().Count());
            Assert.Equal(1, b1.Ancestors<Run>().Count());
            Assert.Equal(1, b1.Ancestors<RunProperties>().Count());
            Assert.Equal(0, b1.Ancestors<Body>().Count());

            Assert.Same(para, r1.Ancestors().First());
            Assert.Same(para, r1.Ancestors<Paragraph>().First());
            Assert.Same(r1, t1.Ancestors().First());
            Assert.Same(para, t1.Ancestors().Last());
            Assert.Same(r1, t1.Ancestors<Run>().First());
            Assert.Same(para, t1.Ancestors<Paragraph>().First());
            Assert.Same(para, b1.Ancestors().Last());
            Assert.Same(r1, b1.Ancestors().ElementAt(1));

            // IsBefore() / IsAfter()
            Assert.False(para.IsBefore(para));
            Assert.False(para.IsAfter(para));
            Assert.False(r2.IsBefore(r2));
            Assert.False(r2.IsAfter(r2));

            Assert.True(para.IsBefore(r1));
            Assert.False(para.IsAfter(r1));
            Assert.True(para.IsBefore(b1));
            Assert.False(para.IsAfter(b1));
            Assert.False(r1.IsBefore(para));
            Assert.True(r1.IsAfter(para));
            Assert.False(r1.IsBefore(para));
            Assert.True(r1.IsAfter(para));

            Assert.True(r1.IsBefore(r2));
            Assert.False(r1.IsAfter(r2));
            Assert.False(r3.IsBefore(r1));
            Assert.True(r3.IsAfter(r1));
            Assert.False(b1.IsAfter(r2));
            Assert.True(b1.IsBefore(r2));

            Assert.True(r1.IsBefore(b1));
            Assert.False(r1.IsAfter(b1));
            Assert.True(b1.IsAfter(r1));
            Assert.False(b1.IsBefore(r1));

            Paragraph p2 = new Paragraph();
            Run p2r1 = p2.AppendChild(new Run());

            Assert.False(p2.IsBefore(para));
            Assert.False(p2.IsAfter(para));
            Assert.False(p2r1.IsAfter(para));
            Assert.False(p2r1.IsBefore(para));

            // ElementsBefore / ElementsAfter
            Assert.False(para.ElementsBefore().Any());
            Assert.False(para.ElementsAfter().Any());
            Assert.True(r1.ElementsAfter().Any());
            Assert.False(r1.ElementsBefore().Any());
            Assert.False(r3.ElementsAfter().Any());
            Assert.True(r3.ElementsBefore().Any());
            Assert.True(r2.ElementsAfter().Any());
            Assert.True(r2.ElementsBefore().Any());
            Assert.Same(r1, r3.ElementsBefore().First());
            Assert.Same(r2, r3.ElementsBefore().Last());
            Assert.Same(r2, r1.ElementsAfter().First());
            Assert.Same(r3, r1.ElementsAfter().Last());
            Assert.False(b1.ElementsBefore().Any());
            Assert.False(b1.ElementsAfter().Any());
        }