Ejemplo n.º 1
0
        public XElement Parse(Paragraph paragraph)
        {
            Style style = (Style)paragraph.get_Style();

            if (style.NameLocal == DocumentAndParagraphStyles.ImagePreviewStyle)
            {
                IEnumerable <Hyperlink> hs = paragraph.Range.Hyperlinks.Cast <Hyperlink>().ToArray();
                if (hs.Count() == 0)
                {
                    return(null);
                }

                try
                {
                    var hyperline = hs.First();

                    if (!WordUtils.IsHyperlinkValid(hyperline))
                    {
                        return(null);
                    }

                    Uri tempUri = new Uri(hyperline.Address);
                    var src     = tempUri.AbsolutePath;
                    //XElement wrapper = GetImageElement(src, hyperline.ScreenTip);
                    XElement wrapper = GetImageElement(src);
                    return(wrapper);
                }
                catch (WebException e)
                {
                    Globals.SitecoreAddin.LogException("", e);
                    Globals.SitecoreAddin.AlertConnectionFailure();
                }
                catch (Exception e)
                {
                    Globals.SitecoreAddin.LogException("", e);
                    throw;
                }
            }
            if (ImageStyles.Contains(style.NameLocal))
            {
                WordStyleStruct w;
                if (!ParagraphStyles.TryGetValue(style.NameLocal, out w))
                {
                    return(null);
                }
                var element = new XElement("p");
                element.SetAttributeValue("class", w.CssClass);
                element = Transformer.GetCharacterStyledElement(element, paragraph, CharacterStyleFactory.GetCharacterStyles(), false);//new XElement(w.CssElement);

                //var value = Transformer.GetCharacterStylesValue(paragraph).Replace("\a", "");
                string value = element.Value;
                if (value.StartsWith("SOURCE: "))
                {
                    element.Value = value.Remove(0, 8);
                }
                return(element);
            }
            return(null);
        }
Ejemplo n.º 2
0
        public XElement GetSidebox(WordUtils wordUtils)
        {
            var sidebox = new XElement("div");

            // Changed the class from "side-box" to "quick-facts"
            sidebox.SetAttributeValue("class", "quick-facts");

            sidebox.Add(wordUtils.ParagraphsToXml(_paragraphs, null, this).Elements());

            return(sidebox);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Parses hyperlink if the current Range in the enumerator e
        /// is part of a hyperlink and moves the enumerator to the next
        /// Range not part of current hyperlink
        /// </summary>
        /// <param name="e"></param>
        /// <param name="currentElement"></param>
        /// <returns>True if is hyperlink, else false</returns>
        protected bool ParseHyperlink(ref List <Range> .Enumerator e, XElement currentElement)
        {
            var cur = e.Current;

            if (cur == null)
            {
                return(false);
            }
            IEnumerable <Hyperlink> hs = cur.Hyperlinks.Cast <Hyperlink>().ToList();

            if (hs.Any())
            {
                Hyperlink currentHyperlink      = hs.First();
                Range     currentHyperlinkRange = currentHyperlink.Range;

                if (currentHyperlinkRange != null)
                {
                    while (e.Current != null && (e.Current.End < currentHyperlinkRange.End && e.MoveNext()))
                    {
                    }
                }

                if (!WordUtils.IsHyperlinkValid(currentHyperlink))
                {
                    return(false);
                }

                XElement temp = SupportingDocumentsReferenceBuilder.GetHtmlHyperlink(currentHyperlink);
                if (temp != null)
                {
                    currentElement.Add(temp);
                    return(true);
                }
                if (!ShouldNotTransformHyperlink(currentHyperlink))
                {
                    currentElement.Add(GetExternalHyperlink(currentHyperlink));
                    return(true);
                }
                if (currentHyperlinkRange != null)
                {
                    currentElement.Add(currentHyperlinkRange.Text.RinseMsChars());
                    return(false);
                }
            }
            return(false);
        }
Ejemplo n.º 4
0
        //private float TableWidth(Table table)
        //{
        //    int colIndex = 1;
        //    float width = 0;
        //    try
        //    {
        //        while (true)
        //        {	//major hacks because documentation is lacking
        //            and i can't find another solution

        //            table.Rows is inaccessble if there are merged cells
        //            across rows

        //            table.Columns is inaccessible if there are merged
        //            cells across columns

        //            so how do i find out the number of cells in a table row?
        //            WE KEEP GOING TILL WE BUST
        //            width += table.Cell(1, colIndex).Width;
        //            colIndex++;
        //        }
        //    } catch
        //    {
        //        return width;
        //    }

        //}

        public XElement ParseTable(int index)
        {
            Table table = GetTable(index);

            _retrieved[index] = true;
            var root = new XElement("table");

            root.SetAttributeValue("class", "data");
            var tbody = new XElement("tbody");

            root.Add(tbody);
            //var rows = table.Rows;
            var       wordUtils     = new WordUtils();
            var       tableAnalyzer = new TableAnalyzer(table);
            const int maxTableWidth = 544;
            decimal   curSetWidth   = Math.Truncate(tableAnalyzer.TableCellWidthSums.Last());

            if (curSetWidth < maxTableWidth)
            {
                root.SetAttributeValue("width", curSetWidth);
            }
            bool first = true;

            for (int r = 1; r <= table.Rows.Count; r++)
            {
                //Row row = rows[r];
                var currentRow = new XElement("tr");
                tbody.Add(currentRow);
                //var rowCellEnumerator = row.Cells.Cast<Cell>().GetEnumerator();
                decimal currentWidthSum = 0;
                int     numColSpansUsed = 0;
                //while (rowCellEnumerator.MoveNext())
                for (int c = 1; c <= table.Columns.Count; c++)
                {
                    try
                    {
                        var curCell = table.Cell(r, c);                         //rowCellEnumerator.Current);
                        curCell.Select();
                        var    app      = curCell.Application;
                        string cellType = first ? "th" : "td";
                        var    xmlCell  = new XElement(cellType);
                        currentRow.Add(xmlCell);
                        Paragraphs paragraphs        = curCell.Range.Paragraphs;
                        var        tableBuilder      = new TableBuilder(curCell.Tables);
                        XNode      currentDescendent = wordUtils.ParagraphsToXml(paragraphs, tableBuilder).FirstNode;
                        while (currentDescendent != null)
                        {
                            xmlCell.Add(currentDescendent);
                            currentDescendent = currentDescendent.NextNode;
                        }
                        currentWidthSum += (decimal)curCell.Width;
                        int widthIndex = tableAnalyzer.TableCellWidthSums.IndexOf(currentWidthSum);
                        int curNumCol  = widthIndex + 1 - numColSpansUsed;
                        xmlCell.SetAttributeValue("colspan", curNumCol);
                        xmlCell.SetAttributeValue("rowspan",
                                                  app.Selection.Information[WdInformation.wdEndOfRangeRowNumber] -
                                                  app.Selection.Information[WdInformation.wdStartOfRangeRowNumber] + 1);
                        decimal cellWidth = 0;
                        for (int i = widthIndex; i >= widthIndex - curNumCol + 1; i--)
                        {
                            cellWidth += tableAnalyzer.CellWidthPercentages[i];
                        }
                        xmlCell.SetAttributeValue("width", cellWidth + "%");
                        numColSpansUsed += curNumCol;
                    }
                    catch (System.Runtime.InteropServices.COMException)
                    {                     //it seems that the only way to tell if a cell at a particular [row, col] index
                        //exists is to call table.Cell(row, col) and see if it throws an exception
                        //the reason it may not exist is rowspans and colspans
                        int nr = r;
                        while (nr > 0)
                        {
                            try
                            {
                                currentWidthSum += (decimal)table.Cell(nr, c).Width;
                                int widthIndex = tableAnalyzer.TableCellWidthSums.IndexOf(currentWidthSum);
                                int curNumCol  = widthIndex + 1 - numColSpansUsed;
                                numColSpansUsed += curNumCol;
                                break;
                            }
                            catch (System.Runtime.InteropServices.COMException)
                            {
                                nr--;
                            }
                        }
                    }
                    //var colspan = (int)(curCell.Width / smallestWidth);
                    //if (colspan > 1) xmlCell.SetAttributeValue("colspan", colspan);
                }
                first = false;
            }
            return(root);
        }