예제 #1
0
            public LevelPrototype(ProcessContext context, IEnumerable <XElement> prototypeItems, IEnumerable <string> fieldNames)
            {
                this._context = context;
                var currentLevelPrototype = new List <XElement>();

                // Items for which no content control.
                // Add this items to the prototype if there are items after them.
                var maybeNeedToAdd          = new List <XElement>();
                var numberingElementReached = false;

                foreach (var prototypeItem in prototypeItems)
                {
                    // search for first item with numbering
                    if (!numberingElementReached)
                    {
                        var paragraph = prototypeItem.DescendantsAndSelf(W.p).FirstOrDefault();
                        if (paragraph != null &&
                            ListItemRetriever.RetrieveListItem(
                                context.Document.NumberingPart, context.Document.StylesPart, paragraph)
                            .IsListItem)
                        {
                            numberingElementReached = true;
                        }
                        else
                        {
                            continue;
                        }
                    }

                    if ((!prototypeItem.FirstLevelDescendantsAndSelf(W.sdt).Any() && prototypeItem.Value != string.Empty) ||
                        prototypeItem
                        .FirstLevelDescendantsAndSelf(W.sdt)
                        .Any(sdt => fieldNames.Contains(sdt.SdtTagName())))
                    {
                        currentLevelPrototype.AddRange(maybeNeedToAdd);
                        currentLevelPrototype.Add(prototypeItem);
                    }

                    else
                    {
                        maybeNeedToAdd.Add(prototypeItem);
                    }
                }

                if (!currentLevelPrototype.Any())
                {
                    return;
                }

                this.PrototypeItems = currentLevelPrototype;

                if (fieldNames.Any(fn => !this.SdtTags.Contains(fn)))
                {
                    this.IsValid = false;
                    return;
                }

                this.IsValid        = true;
                this.PrototypeItems = currentLevelPrototype;
            }
예제 #2
0
        public string GetIndentionTextFromParagraph(WordprocessingDocument doc, int elementPosition)
        {
            XDocument xDoc       = doc.MainDocumentPart.GetXDocument();
            var       xParagraph = xDoc.Descendants(W.p).ElementAt(elementPosition);

            string listItem = ListItemRetriever.RetrieveListItem(
                doc, xParagraph, null);

            return(listItem);
        }
예제 #3
0
        public void RetrieveListItem_DocumentWithNumberedLists_ListItemSuccessfullyRetrieved()
        {
            const string path = "Resources\\Numbered Lists.docx";

            using WordprocessingDocument wordDoc = WordprocessingDocument.Open(path, false);

            XElement document = wordDoc.MainDocumentPart.GetXElement();

            foreach (XElement paragraph in document.Descendants(W.p))
            {
                string listItem = ListItemRetriever.RetrieveListItem(wordDoc, paragraph);
                string text     = paragraph.Descendants(W.t).Select(t => t.Value).StringConcatenate();

                _output.WriteLine(string.IsNullOrEmpty(listItem) ? text : $"{listItem} {text}");
            }
        }
예제 #4
0
        public List <StringBuilder> getBody(XWPFDocument Document)
        {
            var allParas = new List <StringBuilder>();
            var bos      = Document.Paragraphs;

            Debug.WriteLine(doc);

            MainDocumentPart mdp  = doc.MainDocumentPart;
            XDocument        xDoc = mdp.GetXDocument();

            var paragraphs = xDoc.Descendants(W.p);

            List <string> allListItems = new List <string>();

            foreach (var para in paragraphs)
            {
                string listItem = string.Empty;

                try
                {
                    string paraText = para.Descendants(W.t).Select(t => (string)t).StringConcatenate();
                    listItem = ListItemRetriever.RetrieveListItem(doc, para, null);

                    Debug.WriteLine(listItem.Length);
                    allListItems.Add(listItem);
                }
                catch (Exception e)
                {
                    Debug.WriteLine("Spaces");
                }
            }

            allParas = this.getHeadings(bos, allListItems);

            Debug.WriteLine(allParas.Count);
            return(allParas);
        }
예제 #5
0
    private static XElement ConvertDocToXml(WordprocessingDocument wDoc, int abstractNumId)
    {
        XDocument xd = wDoc.MainDocumentPart.GetXDocument();

        // First, call RetrieveListItem so that all paragraphs are initialized with ListItemInfo
        var firstParagraph = xd.Descendants(W.p).FirstOrDefault();
        var listItem       = ListItemRetriever.RetrieveListItem(wDoc, firstParagraph);

        XElement xml     = new XElement("Root");
        var      current = new Stack <XmlStackItem>();

        current.Push(
            new XmlStackItem()
        {
            Element      = xml,
            LevelNumbers = new int[] { },
        });
        foreach (var paragraph in xd.Descendants(W.p))
        {
            // The following does not take into account documents that have tracked revisions.
            // As necessary, call RevisionAccepter.AcceptRevisions before converting to XML.
            var text = paragraph.Descendants(W.t).Select(t => (string)t).StringConcatenate();
            ListItemRetriever.ListItemInfo lii =
                paragraph.Annotation <ListItemRetriever.ListItemInfo>();
            if (lii.IsListItem && lii.AbstractNumId == abstractNumId)
            {
                ListItemRetriever.LevelNumbers levelNums =
                    paragraph.Annotation <ListItemRetriever.LevelNumbers>();
                if (levelNums.LevelNumbersArray.Length == current.Peek().LevelNumbers.Length)
                {
                    current.Pop();
                    var    levelNumsForThisIndent = levelNums.LevelNumbersArray;
                    string levelText = levelNums
                                       .LevelNumbersArray
                                       .Select(l => l.ToString() + ".")
                                       .StringConcatenate()
                                       .TrimEnd('.');
                    var newCurrentElement = new XElement("Indent",
                                                         new XAttribute("Level", levelText));
                    current.Peek().Element.Add(newCurrentElement);
                    current.Push(
                        new XmlStackItem()
                    {
                        Element      = newCurrentElement,
                        LevelNumbers = levelNumsForThisIndent,
                    });
                    current.Peek().Element.Add(new XElement("Heading", text));
                }
                else if (levelNums.LevelNumbersArray.Length > current.Peek().LevelNumbers.Length)
                {
                    for (int i = current.Peek().LevelNumbers.Length;
                         i < levelNums.LevelNumbersArray.Length;
                         i++)
                    {
                        var levelNumsForThisIndent = levelNums
                                                     .LevelNumbersArray
                                                     .Take(i + 1)
                                                     .ToArray();
                        string levelText = levelNums
                                           .LevelNumbersArray
                                           .Select(l => l.ToString() + ".")
                                           .StringConcatenate()
                                           .TrimEnd('.');
                        var newCurrentElement = new XElement("Indent",
                                                             new XAttribute("Level", levelText));
                        current.Peek().Element.Add(newCurrentElement);
                        current.Push(
                            new XmlStackItem()
                        {
                            Element      = newCurrentElement,
                            LevelNumbers = levelNumsForThisIndent,
                        });
                        current.Peek().Element.Add(new XElement("Heading", text));
                    }
                }
                else if (levelNums.LevelNumbersArray.Length < current.Peek().LevelNumbers.Length)
                {
                    for (int i = current.Peek().LevelNumbers.Length;
                         i > levelNums.LevelNumbersArray.Length;
                         i--)
                    {
                        current.Pop();
                    }
                    current.Pop();
                    var    levelNumsForThisIndent = levelNums.LevelNumbersArray;
                    string levelText = levelNums
                                       .LevelNumbersArray
                                       .Select(l => l.ToString() + ".")
                                       .StringConcatenate()
                                       .TrimEnd('.');
                    var newCurrentElement = new XElement("Indent",
                                                         new XAttribute("Level", levelText));
                    current.Peek().Element.Add(newCurrentElement);
                    current.Push(
                        new XmlStackItem()
                    {
                        Element      = newCurrentElement,
                        LevelNumbers = levelNumsForThisIndent,
                    });
                    current.Peek().Element.Add(new XElement("Heading", text));
                }
            }
            else
            {
                current.Peek().Element.Add(new XElement("Paragraph", text));
            }
        }
        return(xml);
    }
예제 #6
0
        /// <summary>
        /// Convert to html transform
        /// </summary>
        /// <param name="wordDoc"></param>
        /// <param name="settings"></param>
        /// <param name="node"></param>
        /// <param name="imageHandler"></param>
        /// <returns></returns>
        private object ConvertToHtmlTransform(WordprocessingDocument wordDoc,
                                              HtmlConverterSettings settings, XNode node,
                                              Func <ImageInfo, XElement> imageHandler)
        {
            var element = node as XElement;

            if (element != null)
            {
                if (element.Name == W.document)
                {
                    return(new XElement(Xhtml.Html,
                                        new XElement(Xhtml.Head,
                                                     new XElement(Xhtml.Meta,
                                                                  new XAttribute(HtmlNoNamespace.HttpEquiv, "Content-Type"),
                                                                  new XAttribute(HtmlNoNamespace.Content,
                                                                                 "text/html; charset=windows-1252")),
                                                     new XElement(Xhtml.Meta,
                                                                  new XAttribute(HtmlNoNamespace.Name, "Generator"),
                                                                  new XAttribute(HtmlNoNamespace.Content,
                                                                                 "PowerTools for Open XML")),
                                                     settings.PageTitle != null ? new XElement(Xhtml.Title,
                                                                                               settings.PageTitle) : null,
                                                     settings.Css != null ? new XElement(Xhtml.Style,
                                                                                         new XComment(Environment.NewLine +
                                                                                                      settings.Css + Environment.NewLine)) : null
                                                     ),
                                        element.Elements().Select(e => ConvertToHtmlTransform(
                                                                      wordDoc, settings, e, imageHandler))
                                        ));
                }

                // Transform the w:body element to the XHTML h:body element.
                if (element.Name == W.body)
                {
                    return(new XElement(Xhtml.Body,
                                        element.Elements().Select(e => ConvertToHtmlTransform(
                                                                      wordDoc, settings, e, imageHandler))));
                }

                // Transform every paragraph with a style that has paragraph properties
                // that has an outline level into the same level of heading.  This takes
                // care of transforming headings of every level.
                if (element.Name == W.p)
                {
                    var styleId = (string)element.Elements(W.pPr).Elements(W.pStyle)
                                  .Attributes(W.val).FirstOrDefault();
                    var xElement = wordDoc.MainDocumentPart.StyleDefinitionsPart.GetXDocument().Root;
                    if (xElement != null)
                    {
                        var style = xElement.Elements(W.style).FirstOrDefault(s => (string)s.Attribute(W.styleId) == styleId);
                        if (style != null)
                        {
                            var outlineLevel = (int?)style.Elements(W.pPr)
                                               .Elements(W.outlineLvl).Attributes(W.val).FirstOrDefault();
                            if (outlineLevel != null)
                            {
                                return(new XElement(Xhtml.xhtml + string.Format("h{0}",
                                                                                outlineLevel + 1),
                                                    settings.CssClassPrefix != null ?
                                                    new XAttribute(HtmlNoNamespace.Class,
                                                                   settings.CssClassPrefix + styleId) : null,
                                                    ConvertEntities(ListItemRetriever.RetrieveListItem(wordDoc,
                                                                                                       element, null)),
                                                    element.Elements().Select(e => ConvertToHtmlTransform(wordDoc,
                                                                                                          settings, e, imageHandler))));
                            }
                        }
                    }
                }

                // Transform w:p to h:p.
                if (element.Name == W.p)
                {
                    var styleId = (string)element.Elements(W.pPr).Elements(W.pStyle)
                                  .Attributes(W.val).FirstOrDefault();
                    if (styleId == null)
                    {
                        var xElement = wordDoc.MainDocumentPart.StyleDefinitionsPart.GetXDocument().Root;
                        if (xElement != null)
                        {
                            var firstOrDefault = xElement.Elements(W.style).FirstOrDefault(e => (string)e.Attribute(W.type) == "paragraph" &&
                                                                                           (string)e.Attribute(W._default) == "1");
                            if (firstOrDefault != null)
                            {
                                styleId = (string)firstOrDefault.Attributes(W.styleId).FirstOrDefault();
                            }
                        }
                    }
                    var z = new XElement(Xhtml.P,
                                         styleId != null ? (
                                             settings.CssClassPrefix != null ?
                                             new XAttribute(HtmlNoNamespace.Class,
                                                            settings.CssClassPrefix + styleId) : null
                                             ) : null,
                                         ConvertEntities(ListItemRetriever.RetrieveListItem(wordDoc,
                                                                                            element, null)),
                                         element.Elements().Select(e => ConvertToHtmlTransform(wordDoc,
                                                                                               settings, e, imageHandler)));
                    return(z);
                }

                // Transform every hyperlink in the document to the XHTML h:A element.
                if (element.Name == W.hyperlink && element.Attribute(R.id) != null)
                {
                    try
                    {
                        return(new XElement(Xhtml.A,
                                            new XAttribute(HtmlNoNamespace.Href,
                                                           wordDoc.MainDocumentPart
                                                           .HyperlinkRelationships.First(x => x.Id == (string)element.Attribute(R.id))
                                                           .Uri
                                                           ),
                                            ConvertEntities(element.Elements(W.r)
                                                            .Elements(W.t)
                                                            .Select(s => (string)s).StringConcatenate())
                                            ));
                    }
                    catch (UriFormatException)
                    {
                        return(element.Elements().Select(e => ConvertToHtmlTransform(wordDoc,
                                                                                     settings, e, imageHandler)));
                    }
                }

                // Transform contents of runs that are part of a hyperlink.
                var annotation = element.Annotation <FieldInfo>();
                if (annotation != null && (element.Name == W.r && annotation.Arguments.Length > 0))
                {
                    var fieldInfo = element.Annotation <FieldInfo>();
                    if (fieldInfo != null)
                    {
                        return(new XElement(Xhtml.A,
                                            new XAttribute(HtmlNoNamespace.Href, fieldInfo.Arguments[0]),
                                            ConvertEntities(element.Elements(W.t)
                                                            .Select(s => (string)s).StringConcatenate())
                                            ));
                    }
                }

                // Transform contents of runs.
                if (element.Name == W.r)
                {
                    return(element.Elements().Select(e => ConvertToHtmlTransform(wordDoc,
                                                                                 settings, e, imageHandler)));
                }

                // Transform every w:t element to a text node.
                if (element.Name == W.t)
                {
                    return(ConvertEntities(element.Value));
                }

                // Transform w:br to h:br.
                if (element.Name == W.br || element.Name == W.cr)
                {
                    return(new XElement(Xhtml.Br));
                }

                // Transform w:noBreakHyphen to '-'
                if (element.Name == W.noBreakHyphen)
                {
                    return(new XText("-"));
                }

                // Transform w:tbl to h:tbl.
                if (element.Name == W.tbl)
                {
                    return(new XElement(Xhtml.Table,
                                        new XAttribute(HtmlNoNamespace.Border, 1),
                                        element.Elements().Select(e => ConvertToHtmlTransform(wordDoc,
                                                                                              settings, e, imageHandler))));
                }

                // Transform w:tr to h:tr.
                if (element.Name == W.tr)
                {
                    return(new XElement(Xhtml.Tr,
                                        element.Elements().Select(e => ConvertToHtmlTransform(wordDoc,
                                                                                              settings, e, imageHandler))));
                }

                // Transform w:tc to h:td.
                if (element.Name == W.tc)
                {
                    return(new XElement(Xhtml.Td,
                                        element.Elements().Select(e => ConvertToHtmlTransform(wordDoc,
                                                                                              settings, e, imageHandler))));
                }

                // Transform images.
                if (element.Name == W.drawing || element.Name == W.pict)
                {
                    if (imageHandler == null)
                    {
                        return(null);
                    }
                    return(ProcessImage(wordDoc, element, imageHandler));
                }

                // The following removes any nodes that haven't been transformed.
                return(null);
            }
            return(null);
        }