Exemplo n.º 1
0
 /// <summary>
 /// Checks if the root element has content elemens
 /// </summary>
 /// <param name="rootElement"></param>
 /// <returns>True when there are content elements</returns>
 public static bool HasContentElement(DomElement rootElement)
 {
     if (rootElement.Elements.Count == 0)
     {
         return false;
     }
     if (rootElement.Elements.Count == 1)
     {
         if (rootElement.Elements[0] is DomParagraph)
         {
             var p = (DomParagraph) rootElement.Elements[0];
             if (p.Elements.Count == 0)
             {
                 return false;
             }
         }
     }
     return true;
 }
Exemplo n.º 2
0
 /// <summary>
 /// Remove element
 /// </summary>
 /// <param name="node">element</param>
 public void Remove(DomElement node)
 {
     List.Remove(node);
 }
Exemplo n.º 3
0
 /// <summary>
 /// Get the index of special element that starts with 0.
 /// </summary>
 /// <param name="element">element</param>
 /// <returns>index , if not find element , then return -1</returns>
 public int IndexOf(DomElement element)
 {
     return(List.IndexOf(element));
 }
Exemplo n.º 4
0
 /// <summary>
 /// Insert element
 /// </summary>
 /// <param name="index">special index</param>
 /// <param name="element">element</param>
 public void Insert(int index, DomElement element)
 {
     List.Insert(index, element);
 }
Exemplo n.º 5
0
 /// <summary>
 /// Add element
 /// </summary>
 /// <param name="element">element</param>
 /// <returns>index</returns>
 public int Add(DomElement element)
 {
     return(List.Add(element));
 }
Exemplo n.º 6
0
        /// <summary>
        /// Add content element
        /// </summary>
        /// <param name="newElement"></param>
        private void AddContentElement(DomElement newElement)
        {
            var elements = GetLastElements(true);
            DomElement lastElement = null;
            if (elements.Length > 0)
                lastElement = elements[elements.Length - 1];

            if (lastElement is DomDocument
                || lastElement is DomHeader
                || lastElement is DomFooter)
            {
                if (newElement is DomText
                    || newElement is DomImage
                    || newElement is DomObject
                    || newElement is DomShape
                    || newElement is DomShapeGroup)
                {
                    var paragraph = new DomParagraph();
                    if (lastElement.Elements.Count > 0)
                        paragraph.IsTemplateGenerated = true;

                    if (_paragraphFormat != null)
                        paragraph.Format = _paragraphFormat;

                    lastElement.AppendChild(paragraph);
                    paragraph.Elements.Add(newElement);
                    return;
                }
            }

            var element = elements[elements.Length - 1];
            
            if (newElement != null && newElement.NativeLevel > 0)
            {
                for (var count = elements.Length - 1; count >= 0; count--)
                {
                    if (elements[count].NativeLevel == newElement.NativeLevel)
                    {
                        for (var count2 = count; count2 < elements.Length; count2++)
                        {
                            var element2 = elements[count2];
                            if (newElement is DomText
                                || newElement is DomImage
                                || newElement is DomObject
                                || newElement is DomShape
                                || newElement is DomShapeGroup
                                || newElement is DomField
                                || newElement is DomBookmark
                                || newElement is DomLineBreak)
                            {
                                if (newElement.NativeLevel == element2.NativeLevel)
                                {
                                    if (element2 is DomTableRow
                                        || element2 is DomTableCell
                                        || element2 is DomField
                                        || element2 is DomParagraph)
                                        continue;
                                }
                            }

                            elements[count2].Locked = true;
                        }
                        break;
                    }
                }
            }

            for (var count = elements.Length - 1; count >= 0; count--)
            {
                if (elements[count].Locked == false)
                {
                    element = elements[count];
                    if (element is DomImage)
                        element.Locked = true;
                    else
                        break;
                }
            }
            if (element is DomTableRow)
            {
                // If the last element is table row 
                // can not contains any element , 
                // so need create a cell element.
                var tableCell = new DomTableCell {NativeLevel = element.NativeLevel};
                element.AppendChild(tableCell);
                if (newElement is DomTableRow)
                {
                    tableCell.Elements.Add(newElement);
                }
                else
                {
                    var cellParagraph = new DomParagraph
                    {
                        Format = _paragraphFormat.Clone(),
                        NativeLevel = tableCell.NativeLevel
                    };

                    tableCell.AppendChild(cellParagraph);
                    if (newElement != null)
                        cellParagraph.AppendChild(newElement);
                }
            }
            else
            {
                if (newElement != null)
                {
                    if (element is DomParagraph &&
                        (newElement is DomParagraph
                         || newElement is DomTableRow))
                    {
                        // If both is paragraph , append new paragraph to the parent of old paragraph
                        element.Locked = true;
                        element.Parent.AppendChild(newElement);
                    }
                    else
                    {
                        element.AppendChild(newElement);
                    }
                }
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Combine tables
        /// </summary>
        /// <param name="parentElement">The parent element</param>
        private void CombineTable(DomElement parentElement)
        {
            var result = new ArrayList();
            var rows = new ArrayList();
            var lastRowWidth = -1;
            DomTableRow lastRow = null;

            foreach (DomElement element in parentElement.Elements)
            {
                if (element is DomTableRow)
                {
                    var tableRow = (DomTableRow) element;
                    tableRow.Locked = false;
                    var cellSettings = tableRow.CellSettings;

                    if (cellSettings.Count == 0)
                    {
                        if (lastRow != null && lastRow.CellSettings.Count == tableRow.Elements.Count)
                        {
                            cellSettings = lastRow.CellSettings;
                        }
                    }

                    if (cellSettings.Count == tableRow.Elements.Count)
                    {
                        for (var count = 0; count < tableRow.Elements.Count; count++)
                            tableRow.Elements[count].Attributes = (AttributeList) cellSettings[count];
                    }

                    var isLastRow = tableRow.HasAttribute(Consts.Lastrow);

                    if (isLastRow == false)
                    {
                        var index = parentElement.Elements.IndexOf(element);
                        if (index == parentElement.Elements.Count - 1)
                        {
                            // If this element is the last element
                            // then this row is the last row
                            isLastRow = true;
                        }
                        else
                        {
                            var element2 = parentElement.Elements[index + 1];
                            if (!(element2 is DomTableRow))
                                // Next element is not row 
                                isLastRow = true;
                        }
                    }
                    
                    // Split to table
                    if (isLastRow)
                    {
                        // If current row mark the last row , then generate a new table
                        rows.Add(tableRow);
                        result.Add(CreateTable(rows));
                        lastRowWidth = -1;
                    }
                    else
                    {
                        var width = 0;
                        if (tableRow.HasAttribute(Consts.TrwWidth))
                        {
                            width = tableRow.Attributes[Consts.TrwWidth];
                            if (tableRow.HasAttribute(Consts.TrwWidthA))
                                width = width - tableRow.Attributes[Consts.TrwWidthA];
                        }
                        else
                        {
                            foreach (DomTableCell tableCell in tableRow.Elements)
                            {
                                if (tableCell.HasAttribute(Consts.Cellx))
                                    width = Math.Max(width, tableCell.Attributes[Consts.Cellx]);
                            }
                        }
                        if (lastRowWidth > 0 && lastRowWidth != width)
                        {
                            // If row's width is change , then can consider multi-table combin
                            // then split and generate new table
                            if (rows.Count > 0)
                                result.Add(CreateTable(rows));
                        }
                        lastRowWidth = width;
                        rows.Add(tableRow);
                    }
                    lastRow = tableRow;
                }
                else if (element is DomTableCell)
                {
                    lastRow = null;
                    CombineTable(element);
                    if (rows.Count > 0)
                    {
                        result.Add(CreateTable(rows));
                    }
                    result.Add(element);
                    lastRowWidth = -1;
                }
                else
                {
                    lastRow = null;
                    CombineTable(element);
                    if (rows.Count > 0)
                        result.Add(CreateTable(rows));
                    result.Add(element);
                    lastRowWidth = -1;
                }
            }

            if (rows.Count > 0)
                result.Add(CreateTable(rows));
            
            parentElement.Locked = false;
            parentElement.Elements.Clear();
            
            foreach (DomElement element in result)
                parentElement.AppendChild(element);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Fixes invalid paragraphs
        /// </summary>
        // ReSharper disable UnusedMember.Global
        public void FixForParagraphs(DomElement parentElement)
        // ReSharper restore UnusedMember.Global
        {
            DomParagraph lastParagraph = null;
            var list = new DomElementList();
            foreach (DomElement element in parentElement.Elements)
            {
                if (element is DomHeader
                    || element is DomFooter)
                {
                    FixForParagraphs(element);
                    lastParagraph = null;
                    list.Add(element);
                    continue;
                }

                if (element is DomParagraph
                    || element is DomTableRow
                    || element is DomTable
                    || element is DomTableCell)
                {
                    lastParagraph = null;
                    list.Add(element);
                    continue;
                }

                if (lastParagraph == null)
                {
                    lastParagraph = new DomParagraph();
                    list.Add(lastParagraph);
                    if (element is DomText)
                        lastParagraph.Format = ((DomText) element).Format.Clone();
                }

                lastParagraph.Elements.Add(element);
            }

            parentElement.Elements.Clear();
            
            foreach (DomElement element in list)
                parentElement.Elements.Add(element);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Fixes invalid dom elements
        /// </summary>
        /// <param name="parentElement"></param>
        private void FixElements(DomElement parentElement)
        {
            // combin text element , decrease number of RTFDomText instance
            var result = new ArrayList();
            foreach (DomElement element in parentElement.Elements)
            {
                if (element is DomParagraph)
                {
                    var p = (DomParagraph) element;
                    if (p.Format.PageBreak)
                    {
                        p.Format.PageBreak = false;
                        result.Add(new DomPageBreak());
                    }
                }

                if (element is DomText)
                {
                    if (result.Count > 0 && result[result.Count - 1] is DomText)
                    {
                        var lastText = (DomText) result[result.Count - 1];
                        var txt = (DomText) element;
                        if (lastText.Text.Length == 0 || txt.Text.Length == 0)
                        {
                            if (lastText.Text.Length == 0)
                            {
                                // close text format
                                lastText.Format = txt.Format.Clone();
                            }
                            lastText.Text = lastText.Text + txt.Text;
                        }
                        else
                        {
                            if (lastText.Format.EqualsSettings(txt.Format))
                            {
                                lastText.Text = lastText.Text + txt.Text;
                            }
                            else
                            {
                                result.Add(txt);
                            }
                        }
                    }
                    else
                        result.Add(element);
                }
                else
                    result.Add(element);
            }

            parentElement.Elements.Clear();
            parentElement.Locked = false;
            
            foreach (DomElement element in result)
                parentElement.AppendChild(element);

            foreach (var element in parentElement.Elements.ToArray())
            {
                if (element is DomTable)
                    UpdateTableCells((DomTable) element, true);
            }

            // Recursive
            foreach (DomElement element in parentElement.Elements)
                FixElements(element);
        }
Exemplo n.º 10
0
 /// <summary>
 /// Append child element
 /// </summary>
 /// <param name="element">child element</param>
 /// <returns>index of element</returns>
 public int AppendChild(DomElement element)
 {
     CheckLocked();
     element.Parent = this;
     element.OwnerDocument = _ownerDocument;
     return Elements.Add(element);
 }
Exemplo n.º 11
0
 /// <summary>
 /// Remove element
 /// </summary>
 /// <param name="node">element</param>
 public void Remove(DomElement node)
 {
     List.Remove(node);
 }
Exemplo n.º 12
0
 /// <summary>
 /// Get the index of special element that starts with 0.
 /// </summary>
 /// <param name="element">element</param>
 /// <returns>index , if not find element , then return -1</returns>
 public int IndexOf(DomElement element)
 {
     return List.IndexOf(element);
 }
Exemplo n.º 13
0
 /// <summary>
 /// Insert element
 /// </summary>
 /// <param name="index">special index</param>
 /// <param name="element">element</param>
 public void Insert(int index, DomElement element)
 {
     List.Insert(index, element);
 }
Exemplo n.º 14
0
 /// <summary>
 /// Add element
 /// </summary>
 /// <param name="element">element</param>
 /// <returns>index</returns>
 public int Add(DomElement element)
 {
     return List.Add(element);
 }