public void LoadParagraphWithRedefinedPrefixTest()
        {
            this.MyTestInitialize(TestContext.GetCurrentMethod());
            Log.Comment("Constructing a Paragraph with rawxml...");
            var rawxml = "<w:p w:rsidR=\"006B669D\" w:rsidRDefault=\"006B669D\" w:rsidP=\"006B669D\" xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" xmlns:myw=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" >\n"
                         + "	<w12:r xmlns:w12=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" >\n"
                         + "		<w:t xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" xml:space=\"preserve\">This is the text in this paragraph. </w:t>\n"
                         + "	</w12:r>\n"
                         + "</w:p>\n";

            Log.Comment(rawxml);
            var para = new DocumentFormat.OpenXml.Wordprocessing.Paragraph(rawxml);

            Log.VerifyValue(para.NamespaceDeclarations.Count(), 2, "Count of returned Namespace Declarations does NOT match expectation.");
            var run = para.FirstChild;

            Log.VerifyValue(run.NamespaceDeclarations.Count(), 1, "Count of returned Namespace Declarations does NOT match expectation.");
            Log.VerifyValue(run.Prefix, "w12", "Unexpected prefix returned for the Run.");
            Log.VerifyValue(run.NamespaceUri, run.NamespaceDeclarations.ElementAt(0).Value, "NamespaceUri does NOT match declarations on itself.");
            Log.VerifyValue(para.NamespaceUri, run.NamespaceUri, "NamespaceUri of Run does NOT match Paragraph.");
            var text = run.FirstChild;

            Log.VerifyValue(text.NamespaceDeclarations.Count(), 1, "Count of returned Namespace Declarations does NOT match expectation.");
            Log.VerifyValue(text.Prefix, "w", "Unexpected prefix returned for the Text.");
            Log.VerifyValue(text.NamespaceUri, text.NamespaceDeclarations.ElementAt(0).Value, "NamespaceUri does NOT match declarations on itself.");
            Log.VerifyValue(run.NamespaceUri, text.NamespaceUri, "NamespaceUri of Text does NOT match Run.");
            Log.Comment("OuterXml of Paragraph:\n{0}", para.OuterXml);
        }
Ejemplo n.º 2
0
        public Paragraph ShuffleSentenceUnit(Paragraph xmlSentenceElement)
        {
            _sentence = new Sentence(xmlSentenceElement); // move to a constructor later!

            var sentenceArray = _sentence.SentenceArray;

            if (NoAdverbFoundInSentence(sentenceArray))
            {
                return(xmlSentenceElement);
            }

            // If an ADV is found, continue to search for the next ADV until reaching any of VB / PAST / PRES / Full - Stop.
            int AdverbIndexPosition =
                Array.FindIndex(sentenceArray, i => i.IsAdverb());

            //get number of adverbs between first adverb and next breaker
            int AdverbCount     = 0;
            int breakerPosition = 0;

            for (int i = AdverbIndexPosition; i >= AdverbIndexPosition; i++)
            {
                if (sentenceArray[i].IsAdverb())
                {
                    AdverbCount = AdverbCount + 1;
                }

                if (sentenceArray[i].ReachedSentenceBreaker())
                {
                    breakerPosition = i;
                    break;
                }
            }

            if (IsMoreThanOneAdverb(AdverbCount))
            {
                _sentence.UnderlineJoinedSentenceUnit(
                    sentenceArray, AdverbIndexPosition, breakerPosition);

                xmlSentenceElement = _sentence.HasVBAToTheLeft(sentenceArray, AdverbIndexPosition)
                    ? MoveAdverbUnitBetweenTheVBAAndPASTPRES(sentenceArray, AdverbIndexPosition)
                    : MoveAdverbUnitBeforeVBOrPASTorPRESUnit(sentenceArray, AdverbIndexPosition);
            }
            else if (IsOneAdverb(AdverbCount))
            {
                if (_sentence.UnitNotFoundInSentence(
                        sentenceArray,
                        element => element.InnerText.IsVBA()))
                {
                    xmlSentenceElement =
                        MoveSingleAdverbBeforeVBOrPASTorPRESUnit(
                            sentenceArray, AdverbIndexPosition, breakerPosition);
                }
            }

            return(xmlSentenceElement);
        }
Ejemplo n.º 3
0
        internal override OpenXmlElement Render(int index, bool isFirst, bool isLast)
        {
            var result = new DocumentFormat.OpenXml.Wordprocessing.Paragraph
            {
                ParagraphProperties = _config.GetParagraphProperties()
            };

            foreach (var run in _config.GetRuns())
            {
                result.AppendChild(run);
            }

            return(result);
        }
        public override IEnumerable<OpenXmlElement> ToOpenXmlElements(DocumentFormat.OpenXml.Packaging.MainDocumentPart mainDocumentPart)
        {
            var result = new DocumentFormat.OpenXml.Wordprocessing.Paragraph();
            ForEachChild(x =>
            {
                if (x is TextFormattedElement)
                {
                    result.Append(
                        new DocumentFormat.OpenXml.Wordprocessing.Run(x.ToOpenXmlElements(mainDocumentPart))
                    );

                }
                else
                {
                    result.Append(x.ToOpenXmlElements(mainDocumentPart));
                }
            });
            return new List<OpenXmlElement> { result };
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Render the document
        /// </summary>
        /// <param name="document"></param>
        /// <param name="wdDoc"></param>
        /// <param name="context"></param>
        /// <param name="formatProvider"></param>
        public static void Render(this Document document, WordprocessingDocument wdDoc, ContextModel context, bool addPageBreak, IFormatProvider formatProvider)
        {
            foreach (var pageItem in document.Pages)
            {
                if (pageItem is ForEachPage)
                {
                    // render page
                    ((ForEachPage)pageItem).Render(document, wdDoc.MainDocumentPart.Document.Body, context, wdDoc.MainDocumentPart, formatProvider);
                }
                else if (pageItem is Page)
                {
                    var page = (Page)pageItem;

                    // doc inherit margin from page
                    if (document.Margin == null && page.Margin != null)
                    {
                        document.Margin = page.Margin;
                    }
                    // page inherit margin from doc
                    else if (document.Margin != null && page.Margin == null)
                    {
                        page.Margin = document.Margin;
                    }

                    // render page
                    page.Render(document, wdDoc.MainDocumentPart.Document.Body, context, wdDoc.MainDocumentPart, formatProvider);
                }
            }
            //Replace Last page break
            if (!addPageBreak &&
                wdDoc.MainDocumentPart.Document.Body.LastChild != null &&
                wdDoc.MainDocumentPart.Document.Body.LastChild is DocumentFormat.OpenXml.Wordprocessing.Paragraph &&
                wdDoc.MainDocumentPart.Document.Body.LastChild.FirstChild != null &&
                wdDoc.MainDocumentPart.Document.Body.LastChild.FirstChild is DocumentFormat.OpenXml.Wordprocessing.ParagraphProperties &&
                wdDoc.MainDocumentPart.Document.Body.LastChild.FirstChild.FirstChild != null &&
                wdDoc.MainDocumentPart.Document.Body.LastChild.FirstChild.FirstChild is DocumentFormat.OpenXml.Wordprocessing.SectionProperties)
            {
                DocumentFormat.OpenXml.Wordprocessing.Paragraph lastChild = (DocumentFormat.OpenXml.Wordprocessing.Paragraph)wdDoc.MainDocumentPart.Document.Body.LastChild;
                wdDoc.MainDocumentPart.Document.Body.RemoveChild(lastChild);
            }
        }
Ejemplo n.º 6
0
        public static OpenXmlElement Render(this Paragraph paragraph, OpenXmlElement parent, ContextModel context, IFormatProvider formatProvider)
        {
            context.ReplaceItem(paragraph, formatProvider);

            var openXmlPar = new DocumentFormat.OpenXml.Wordprocessing.Paragraph();

            openXmlPar.ParagraphProperties = new DocumentFormat.OpenXml.Wordprocessing.ParagraphProperties()
            {
                Shading = new DocumentFormat.OpenXml.Wordprocessing.Shading()
                {
                    Fill = paragraph.Shading
                },
                Justification = new DocumentFormat.OpenXml.Wordprocessing.Justification()
                {
                    Val = paragraph.Justification.ToOOxml()
                },
                SpacingBetweenLines = new DocumentFormat.OpenXml.Wordprocessing.SpacingBetweenLines()
            };
            if (paragraph.SpacingBefore.HasValue)
            {
                openXmlPar.ParagraphProperties.SpacingBetweenLines.Before = paragraph.SpacingBefore.ToString();
            }
            if (paragraph.SpacingAfter.HasValue)
            {
                openXmlPar.ParagraphProperties.SpacingBetweenLines.After = paragraph.SpacingAfter.ToString();
            }
            if (paragraph.SpacingBetweenLines.HasValue)
            {
                openXmlPar.ParagraphProperties.SpacingBetweenLines.Line = paragraph.SpacingBetweenLines.ToString();
            }
            if (!string.IsNullOrWhiteSpace(paragraph.ParagraphStyleId))
            {
                openXmlPar.ParagraphProperties.ParagraphStyleId = new DocumentFormat.OpenXml.Wordprocessing.ParagraphStyleId()
                {
                    Val = paragraph.ParagraphStyleId
                }
            }
            ;
            if (paragraph.Borders != null)
            {
                openXmlPar.ParagraphProperties.AppendChild(paragraph.Borders.RenderParagraphBorder());
            }
            if (paragraph.Keeplines)
            {
                openXmlPar.ParagraphProperties.KeepLines = new DocumentFormat.OpenXml.Wordprocessing.KeepLines();
            }
            if (paragraph.KeepNext)
            {
                openXmlPar.ParagraphProperties.KeepNext = new DocumentFormat.OpenXml.Wordprocessing.KeepNext();
            }
            if (paragraph.PageBreakBefore)
            {
                openXmlPar.ParagraphProperties.PageBreakBefore = new DocumentFormat.OpenXml.Wordprocessing.PageBreakBefore();
            }

            // Indents :
            if (paragraph.Indentation != null)
            {
                openXmlPar.ParagraphProperties.Indentation = paragraph.Indentation.ToOpenXmlElement();
            }

            parent.Append(openXmlPar);
            return(openXmlPar);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Render the document
        /// </summary>
        /// <param name="document"></param>
        /// <param name="wdDoc"></param>
        /// <param name="context"></param>
        /// <param name="formatProvider"></param>
        public static void Render(this Document document, WordprocessingDocument wdDoc, ContextModel context, IFormatProvider formatProvider)
        {
            // add styles in document
            var spart = wdDoc.MainDocumentPart.AddNewPart <StyleDefinitionsPart>();

            spart.Styles = new DocumentFormat.OpenXml.Wordprocessing.Styles();
            foreach (var style in document.Styles)
            {
                style.Render(spart, context);
            }

            foreach (var pageItem in document.Pages)
            {
                if (pageItem is ForEachPage)
                {
                    // render page
                    ((ForEachPage)pageItem).Render(document, wdDoc.MainDocumentPart.Document.Body, context, wdDoc.MainDocumentPart, formatProvider);
                }
                else if (pageItem is Page)
                {
                    var page = (Page)pageItem;

                    // doc inherit margin from page
                    if (document.Margin == null && page.Margin != null)
                    {
                        document.Margin = page.Margin;
                    }
                    // page inherit margin from doc
                    else if (document.Margin != null && page.Margin == null)
                    {
                        page.Margin = document.Margin;
                    }

                    // render page
                    page.Render(document, wdDoc.MainDocumentPart.Document.Body, context, wdDoc.MainDocumentPart, formatProvider);
                }
            }

            //Replace Last page break
            if (wdDoc.MainDocumentPart.Document.Body.LastChild != null &&
                wdDoc.MainDocumentPart.Document.Body.LastChild is DocumentFormat.OpenXml.Wordprocessing.Paragraph &&
                wdDoc.MainDocumentPart.Document.Body.LastChild.FirstChild != null &&
                wdDoc.MainDocumentPart.Document.Body.LastChild.FirstChild is DocumentFormat.OpenXml.Wordprocessing.ParagraphProperties &&
                wdDoc.MainDocumentPart.Document.Body.LastChild.FirstChild.FirstChild != null &&
                wdDoc.MainDocumentPart.Document.Body.LastChild.FirstChild.FirstChild is DocumentFormat.OpenXml.Wordprocessing.SectionProperties)
            {
                DocumentFormat.OpenXml.Wordprocessing.Paragraph         lastChild        = (DocumentFormat.OpenXml.Wordprocessing.Paragraph)wdDoc.MainDocumentPart.Document.Body.LastChild;
                DocumentFormat.OpenXml.Wordprocessing.SectionProperties sectionPropertie = (DocumentFormat.OpenXml.Wordprocessing.SectionProperties)lastChild.FirstChild.FirstChild.Clone();
                wdDoc.MainDocumentPart.Document.Body.ReplaceChild(sectionPropertie, wdDoc.MainDocumentPart.Document.Body.LastChild);
            }

            // footers
            foreach (var footer in document.Footers)
            {
                footer.Render(document, wdDoc.MainDocumentPart, context, formatProvider);
            }
            // headers
            foreach (var header in document.Headers)
            {
                header.Render(document, wdDoc.MainDocumentPart, context, formatProvider);
            }
        }