protected void appendFlagAttribute(XmlElement node, SinglePropertyModifier sprm, string attributeName)
        {
            XmlAttribute att = _nodeFactory.CreateAttribute("w", attributeName, OpenXmlNamespaces.WordprocessingML);

            att.Value = sprm.Arguments[0].ToString();
            node.Attributes.Append(att);
        }
Exemple #2
0
        private bool applyToggleHierachy(SinglePropertyModifier sprm)
        {
            bool ret = false;

            foreach (CharacterPropertyExceptions ancientChpx in _hierarchy)
            {
                foreach (SinglePropertyModifier ancientSprm in ancientChpx.grpprl)
                {
                    if (ancientSprm.OpCode == sprm.OpCode)
                    {
                        byte ancient = ancientSprm.Arguments[0];
                        ret = toogleValue(ret, ancient);
                        break;
                    }
                }
            }
            return(ret);
        }
        protected virtual void appendFlagElement(XmlElement node, SinglePropertyModifier sprm, string elementName, bool unique)
        {
            XmlElement ele = _nodeFactory.CreateElement("w", elementName, OpenXmlNamespaces.WordprocessingML);

            if (sprm.Arguments[0] == 0)
            {
                XmlAttribute val = _nodeFactory.CreateAttribute("w", "val", OpenXmlNamespaces.WordprocessingML);
                val.Value = "false";
                ele.Attributes.Append(val);
            }
            if (unique)
            {
                foreach (XmlElement exEle in node.ChildNodes)
                {
                    if (exEle.Name == ele.Name)
                    {
                        node.RemoveChild(exEle);
                        break;
                    }
                }
            }
            node.AppendChild(ele);
        }
Exemple #4
0
        public void Apply(TablePropertyExceptions tapx)
        {
            //int lastBdr = getLastTabelBorderOccurrence(tapx.grpprl);

            for (int i = 0; i < tapx.grpprl.Count; i++)
            {
                SinglePropertyModifier sprm = tapx.grpprl[i];

                switch (sprm.OpCode)
                {
                //Table definition SPRM
                case  SinglePropertyModifier.OperationCode.sprmTDefTable:
                    SprmTDefTable tdef = new SprmTDefTable(sprm.Arguments);
                    _tGrid = tdef.rgdxaCenter;
                    _tcDef = tdef.rgTc80[_cellIndex];

                    appendValueElement(_tcPr, "textDirection", _tcDef.textFlow.ToString(), false);

                    if (_tcDef.vertMerge == Global.VerticalMergeFlag.fvmMerge)
                    {
                        appendValueElement(_tcPr, "vMerge", "continue", false);
                    }
                    else if (_tcDef.vertMerge == Global.VerticalMergeFlag.fvmRestart)
                    {
                        appendValueElement(_tcPr, "vMerge", "restart", false);
                    }
                    else if (_tcDef.vertMerge == Global.VerticalMergeFlag.fvmRestart)
                    {
                        appendValueElement(_tcPr, "vMerge", "restart", false);
                    }

                    appendValueElement(_tcPr, "vAlign", _tcDef.vertAlign.ToString(), false);

                    if (_tcDef.fFitText)
                    {
                        appendValueElement(_tcPr, "tcFitText", "", false);
                    }

                    if (_tcDef.fNoWrap)
                    {
                        appendValueElement(_tcPr, "noWrap", "", true);
                    }

                    //_width = _tcDef.wWidth;
                    _width    = (Int16)(tdef.rgdxaCenter[_cellIndex + 1] - tdef.rgdxaCenter[_cellIndex]);
                    _ftsWidth = _tcDef.ftsWidth;

                    //borders
                    // if the sprm has a higher priority than the last sprmTTableBorder sprm in the list
                    //if (i > lastBdr)
                    //{
                    //    _brcTop = _tcDef.brcTop;
                    //    _brcLeft = _tcDef.brcLeft;
                    //    _brcRight = _tcDef.brcRight;
                    //    _brcBottom = _tcDef.brcBottom;
                    //}

                    break;

                //margins
                case SinglePropertyModifier.OperationCode.sprmTCellPadding:
                    byte  first     = sprm.Arguments[0];
                    byte  lim       = sprm.Arguments[1];
                    byte  ftsMargin = sprm.Arguments[3];
                    Int16 wMargin   = System.BitConverter.ToInt16(sprm.Arguments, 4);

                    if (_cellIndex >= first && _cellIndex < lim)
                    {
                        BitArray borderBits = new BitArray(new byte[] { sprm.Arguments[2] });
                        if (borderBits[0] == true)
                        {
                            appendDxaElement(_tcMar, "top", wMargin.ToString(), true);
                        }
                        if (borderBits[1] == true)
                        {
                            appendDxaElement(_tcMar, "left", wMargin.ToString(), true);
                        }
                        if (borderBits[2] == true)
                        {
                            appendDxaElement(_tcMar, "bottom", wMargin.ToString(), true);
                        }
                        if (borderBits[3] == true)
                        {
                            appendDxaElement(_tcMar, "right", wMargin.ToString(), true);
                        }
                    }
                    break;

                //shading
                case SinglePropertyModifier.OperationCode.sprmTDefTableShd:
                    //cell shading for cells 0-20
                    apppendCellShading(sprm.Arguments, _cellIndex);
                    break;

                case SinglePropertyModifier.OperationCode.sprmTDefTableShd2nd:
                    //cell shading for cells 21-42
                    apppendCellShading(sprm.Arguments, _cellIndex - 21);
                    break;

                case SinglePropertyModifier.OperationCode.sprmTDefTableShd3rd:
                    //cell shading for cells 43-62
                    apppendCellShading(sprm.Arguments, _cellIndex - 43);
                    break;

                //width
                case SinglePropertyModifier.OperationCode.sprmTCellWidth:
                    first = sprm.Arguments[0];
                    lim   = sprm.Arguments[1];
                    if (_cellIndex >= first && _cellIndex < lim)
                    {
                        _ftsWidth = (Global.CellWidthType)sprm.Arguments[2];
                        _width    = System.BitConverter.ToInt16(sprm.Arguments, 3);
                    }
                    break;

                //vertical alignment
                case SinglePropertyModifier.OperationCode.sprmTVertAlign:
                    first = sprm.Arguments[0];
                    lim   = sprm.Arguments[1];
                    if (_cellIndex >= first && _cellIndex < lim)
                    {
                        appendValueElement(_tcPr, "vAlign", ((VerticalCellAlignment)sprm.Arguments[2]).ToString(), true);
                    }
                    break;

                //Autofit
                case SinglePropertyModifier.OperationCode.sprmTFitText:
                    first = sprm.Arguments[0];
                    lim   = sprm.Arguments[1];
                    if (_cellIndex >= first && _cellIndex < lim)
                    {
                        appendValueElement(_tcPr, "tcFitText", sprm.Arguments[2].ToString(), true);
                    }
                    break;

                //borders (cell definition)
                case SinglePropertyModifier.OperationCode.sprmTSetBrc:
                    byte min            = sprm.Arguments[0];
                    byte max            = sprm.Arguments[1];
                    int  bordersToApply = (int)sprm.Arguments[2];

                    if (_cellIndex >= min && _cellIndex < max)
                    {
                        byte[] brcBytes = new byte[8];
                        Array.Copy(sprm.Arguments, 3, brcBytes, 0, 8);
                        BorderCode border = new BorderCode(brcBytes);
                        if (Utils.BitmaskToBool(bordersToApply, 0x01))
                        {
                            _brcTop = border;
                        }
                        if (Utils.BitmaskToBool(bordersToApply, 0x02))
                        {
                            _brcLeft = border;
                        }
                        if (Utils.BitmaskToBool(bordersToApply, 0x04))
                        {
                            _brcBottom = border;
                        }
                        if (Utils.BitmaskToBool(bordersToApply, 0x08))
                        {
                            _brcRight = border;
                        }
                    }
                    break;
                }
            }

            //width
            XmlElement   tcW     = _nodeFactory.CreateElement("w", "tcW", OpenXmlNamespaces.WordprocessingML);
            XmlAttribute tcWType = _nodeFactory.CreateAttribute("w", "type", OpenXmlNamespaces.WordprocessingML);
            XmlAttribute tcWVal  = _nodeFactory.CreateAttribute("w", "w", OpenXmlNamespaces.WordprocessingML);

            tcWType.Value = _ftsWidth.ToString();
            tcWVal.Value  = _width.ToString();
            tcW.Attributes.Append(tcWType);
            tcW.Attributes.Append(tcWVal);
            _tcPr.AppendChild(tcW);

            //grid span
            _gridSpan = 1;
            if (_width > _grid[_gridIndex])
            {
                //check the number of merged cells
                int w = _grid[_gridIndex];
                for (int i = _gridIndex + 1; i < _grid.Count; i++)
                {
                    _gridSpan++;
                    w += _grid[i];
                    if (w >= _width)
                    {
                        break;
                    }
                }

                appendValueElement(_tcPr, "gridSpan", _gridSpan.ToString(), true);
            }

            //append margins
            if (_tcMar.ChildNodes.Count > 0)
            {
                _tcPr.AppendChild(_tcMar);
            }

            //append borders
            if (_brcTop != null)
            {
                XmlNode topBorder = _nodeFactory.CreateNode(XmlNodeType.Element, "w", "top", OpenXmlNamespaces.WordprocessingML);
                appendBorderAttributes(_brcTop, topBorder);
                addOrSetBorder(_tcBorders, topBorder);
            }
            if (_brcLeft != null)
            {
                XmlNode leftBorder = _nodeFactory.CreateNode(XmlNodeType.Element, "w", "left", OpenXmlNamespaces.WordprocessingML);
                appendBorderAttributes(_brcLeft, leftBorder);
                addOrSetBorder(_tcBorders, leftBorder);
            }
            if (_brcBottom != null)
            {
                XmlNode bottomBorder = _nodeFactory.CreateNode(XmlNodeType.Element, "w", "bottom", OpenXmlNamespaces.WordprocessingML);
                appendBorderAttributes(_brcBottom, bottomBorder);
                addOrSetBorder(_tcBorders, bottomBorder);
            }
            if (_brcRight != null)
            {
                XmlNode rightBorder = _nodeFactory.CreateNode(XmlNodeType.Element, "w", "right", OpenXmlNamespaces.WordprocessingML);
                appendBorderAttributes(_brcRight, rightBorder);
                addOrSetBorder(_tcBorders, rightBorder);
            }
            if (_tcBorders.ChildNodes.Count > 0)
            {
                _tcPr.AppendChild(_tcBorders);
            }

            //write Properties
            if (_tcPr.ChildNodes.Count > 0 || _tcPr.Attributes.Count > 0)
            {
                _tcPr.WriteTo(_writer);
            }
        }
Exemple #5
0
        /// <summary>
        /// CHPX flags are special flags because the can be 0,1,128 and 129,
        /// so this method overrides the appendFlagElement method.
        /// </summary>
        protected override void appendFlagElement(XmlElement node, SinglePropertyModifier sprm, string elementName, bool unique)
        {
            byte flag = sprm.Arguments[0];

            if (flag != 128)
            {
                XmlElement   ele = _nodeFactory.CreateElement("w", elementName, OpenXmlNamespaces.WordprocessingML);
                XmlAttribute val = _nodeFactory.CreateAttribute("w", "val", OpenXmlNamespaces.WordprocessingML);

                if (unique)
                {
                    foreach (XmlElement exEle in node.ChildNodes)
                    {
                        if (exEle.Name == ele.Name)
                        {
                            node.RemoveChild(exEle);
                            break;
                        }
                    }
                }

                if (flag == 0)
                {
                    val.Value = "false";
                    ele.Attributes.Append(val);
                    node.AppendChild(ele);
                }
                else if (flag == 1)
                {
                    //dont append attribute val
                    //no attribute means true
                    node.AppendChild(ele);
                }
                else if (flag == 129)
                {
                    //Invert the value of the style

                    //determine the style id of the current style
                    UInt16 styleId = 0;
                    if (_currentIstd != UInt16.MaxValue)
                    {
                        styleId = _currentIstd;
                    }
                    else if (_currentPapx != null)
                    {
                        styleId = _currentPapx.istd;
                    }

                    //this chpx is the chpx of a style,
                    //don't use the id of the chpx or the papx, use the baseOn style
                    if (_styleChpx)
                    {
                        StyleSheetDescription thisStyle = _doc.Styles.Styles[styleId];
                        styleId = (UInt16)thisStyle.istdBase;
                    }

                    //build the style hierarchy
                    if (_hierarchy == null)
                    {
                        _hierarchy = buildHierarchy(_doc.Styles, styleId);
                    }

                    //apply the toggle values to get the real value of the style
                    bool stylesVal = applyToggleHierachy(sprm);

                    //invert it
                    if (stylesVal)
                    {
                        val.Value = "0";
                        ele.Attributes.Append(val);
                    }
                    node.AppendChild(ele);
                }
            }
        }