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); } }
public void Apply(TablePropertyExceptions tapx) { var tblBorders = this._nodeFactory.CreateElement("w", "tblBorders", OpenXmlNamespaces.WordprocessingML); var tblCellMar = this._nodeFactory.CreateElement("w", "tblCellMar", OpenXmlNamespaces.WordprocessingML); var tblLayout = this._nodeFactory.CreateElement("w", "tblLayout", OpenXmlNamespaces.WordprocessingML); var tblpPr = this._nodeFactory.CreateElement("w", "tblpPr", OpenXmlNamespaces.WordprocessingML); var layoutType = this._nodeFactory.CreateAttribute("w", "type", OpenXmlNamespaces.WordprocessingML); layoutType.Value = "fixed"; short tblIndent = 0; short gabHalf = 0; short marginLeft = 0; short marginRight = 0; foreach (var sprm in tapx.grpprl) { switch (sprm.OpCode) { case SinglePropertyModifier.OperationCode.sprmTDxaGapHalf: gabHalf = System.BitConverter.ToInt16(sprm.Arguments, 0); break; //table definition case SinglePropertyModifier.OperationCode.sprmTDefTable: var tDef = new SprmTDefTable(sprm.Arguments); //Workaround for retrieving the indent of the table: //In some files there is a indent but no sprmTWidthIndent is set. //For this cases we can calculate the indent of the table by getting the //first boundary of the TDef and adding the padding of the cells tblIndent = System.BitConverter.ToInt16(sprm.Arguments, 1); //add the gabHalf tblIndent += gabHalf; //If there follows a real sprmTWidthIndent, this value will be overwritten break; //preferred table width case SinglePropertyModifier.OperationCode.sprmTTableWidth: var fts = (WidthType)sprm.Arguments[0]; short width = System.BitConverter.ToInt16(sprm.Arguments, 1); var tblW = this._nodeFactory.CreateElement("w", "tblW", OpenXmlNamespaces.WordprocessingML); var w = this._nodeFactory.CreateAttribute("w", "w", OpenXmlNamespaces.WordprocessingML); w.Value = width.ToString(); var type = this._nodeFactory.CreateAttribute("w", "type", OpenXmlNamespaces.WordprocessingML); type.Value = fts.ToString(); tblW.Attributes.Append(type); tblW.Attributes.Append(w); this._tblPr.AppendChild(tblW); break; //justification case SinglePropertyModifier.OperationCode.sprmTJc: case SinglePropertyModifier.OperationCode.sprmTJcRow: appendValueElement(this._tblPr, "jc", ((Global.JustificationCode)sprm.Arguments[0]).ToString(), true); break; //indent case SinglePropertyModifier.OperationCode.sprmTWidthIndent: tblIndent = System.BitConverter.ToInt16(sprm.Arguments, 1); break; //style case SinglePropertyModifier.OperationCode.sprmTIstd: case SinglePropertyModifier.OperationCode.sprmTIstdPermute: short styleIndex = System.BitConverter.ToInt16(sprm.Arguments, 0); if (this._styles.Styles.Count > styleIndex) { string id = StyleSheetMapping.MakeStyleId(this._styles.Styles[styleIndex]); if (id != "TableNormal") { appendValueElement(this._tblPr, "tblStyle", id, true); } } break; //bidi case SinglePropertyModifier.OperationCode.sprmTFBiDi: case SinglePropertyModifier.OperationCode.sprmTFBiDi90: appendValueElement(this._tblPr, "bidiVisual", System.BitConverter.ToInt16(sprm.Arguments, 0).ToString(), true); break; //table look case SinglePropertyModifier.OperationCode.sprmTTlp: appendValueElement(this._tblPr, "tblLook", string.Format("{0:x4}", System.BitConverter.ToInt16(sprm.Arguments, 2)), true); break; //autofit case SinglePropertyModifier.OperationCode.sprmTFAutofit: if (sprm.Arguments[0] == 1) { layoutType.Value = "auto"; } break; //default cell padding (margin) case SinglePropertyModifier.OperationCode.sprmTCellPadding: case SinglePropertyModifier.OperationCode.sprmTCellPaddingDefault: case SinglePropertyModifier.OperationCode.sprmTCellPaddingOuter: byte grfbrc = sprm.Arguments[2]; short wMar = System.BitConverter.ToInt16(sprm.Arguments, 4); if (Utils.BitmaskToBool((int)grfbrc, 0x01)) { appendDxaElement(tblCellMar, "top", wMar.ToString(), true); } if (Utils.BitmaskToBool((int)grfbrc, 0x02)) { marginLeft = wMar; } if (Utils.BitmaskToBool((int)grfbrc, 0x04)) { appendDxaElement(tblCellMar, "bottom", wMar.ToString(), true); } if (Utils.BitmaskToBool((int)grfbrc, 0x08)) { marginRight = wMar; } break; //row count case SinglePropertyModifier.OperationCode.sprmTCHorzBands: appendValueElement(this._tblPr, "tblStyleRowBandSize", sprm.Arguments[0].ToString(), true); break; //col count case SinglePropertyModifier.OperationCode.sprmTCVertBands: appendValueElement(this._tblPr, "tblStyleColBandSize", sprm.Arguments[0].ToString(), true); break; //overlap case SinglePropertyModifier.OperationCode.sprmTFNoAllowOverlap: bool noOverlap = Utils.ByteToBool(sprm.Arguments[0]); string tblOverlapVal = "overlap"; if (noOverlap) { tblOverlapVal = "never"; } appendValueElement(this._tblPr, "tblOverlap", tblOverlapVal, true); break; //shading case SinglePropertyModifier.OperationCode.sprmTSetShdTable: var desc = new ShadingDescriptor(sprm.Arguments); appendShading(this._tblPr, desc); break; //borders 80 exceptions case SinglePropertyModifier.OperationCode.sprmTTableBorders80: var brc80 = new byte[4]; //top border Array.Copy(sprm.Arguments, 0, brc80, 0, 4); this.brcTop = new BorderCode(brc80); //left Array.Copy(sprm.Arguments, 4, brc80, 0, 4); this.brcLeft = new BorderCode(brc80); //bottom Array.Copy(sprm.Arguments, 8, brc80, 0, 4); this.brcBottom = new BorderCode(brc80); //right Array.Copy(sprm.Arguments, 12, brc80, 0, 4); this.brcRight = new BorderCode(brc80); //inside H Array.Copy(sprm.Arguments, 16, brc80, 0, 4); this.brcHorz = new BorderCode(brc80); //inside V Array.Copy(sprm.Arguments, 20, brc80, 0, 4); this.brcVert = new BorderCode(brc80); break; //border exceptions case SinglePropertyModifier.OperationCode.sprmTTableBorders: var brc = new byte[8]; //top border Array.Copy(sprm.Arguments, 0, brc, 0, 8); this.brcTop = new BorderCode(brc); //left Array.Copy(sprm.Arguments, 8, brc, 0, 8); this.brcLeft = new BorderCode(brc); //bottom Array.Copy(sprm.Arguments, 16, brc, 0, 8); this.brcBottom = new BorderCode(brc); //right Array.Copy(sprm.Arguments, 24, brc, 0, 8); this.brcRight = new BorderCode(brc); //inside H Array.Copy(sprm.Arguments, 32, brc, 0, 8); this.brcHorz = new BorderCode(brc); //inside V Array.Copy(sprm.Arguments, 40, brc, 0, 8); this.brcVert = new BorderCode(brc); break; //floating table properties case SinglePropertyModifier.OperationCode.sprmTPc: byte flag = sprm.Arguments[0]; var pcVert = (Global.VerticalPositionCode)((flag & 0x30) >> 4); var pcHorz = (Global.HorizontalPositionCode)((flag & 0xC0) >> 6); appendValueAttribute(tblpPr, "horzAnchor", pcHorz.ToString()); appendValueAttribute(tblpPr, "vertAnchor", pcVert.ToString()); break; case SinglePropertyModifier.OperationCode.sprmTDxaFromText: appendValueAttribute(tblpPr, "leftFromText", System.BitConverter.ToInt16(sprm.Arguments, 0).ToString()); break; case SinglePropertyModifier.OperationCode.sprmTDxaFromTextRight: appendValueAttribute(tblpPr, "rightFromText", System.BitConverter.ToInt16(sprm.Arguments, 0).ToString()); break; case SinglePropertyModifier.OperationCode.sprmTDyaFromText: appendValueAttribute(tblpPr, "topFromText", System.BitConverter.ToInt16(sprm.Arguments, 0).ToString()); break; case SinglePropertyModifier.OperationCode.sprmTDyaFromTextBottom: appendValueAttribute(tblpPr, "bottomFromText", System.BitConverter.ToInt16(sprm.Arguments, 0).ToString()); break; case SinglePropertyModifier.OperationCode.sprmTDxaAbs: appendValueAttribute(tblpPr, "tblpX", System.BitConverter.ToInt16(sprm.Arguments, 0).ToString()); break; case SinglePropertyModifier.OperationCode.sprmTDyaAbs: appendValueAttribute(tblpPr, "tblpY", System.BitConverter.ToInt16(sprm.Arguments, 0).ToString()); break; } } //indent if (tblIndent != 0) { var tblInd = this._nodeFactory.CreateElement("w", "tblInd", OpenXmlNamespaces.WordprocessingML); var tblIndW = this._nodeFactory.CreateAttribute("w", "w", OpenXmlNamespaces.WordprocessingML); tblIndW.Value = tblIndent.ToString(); tblInd.Attributes.Append(tblIndW); var tblIndType = this._nodeFactory.CreateAttribute("w", "type", OpenXmlNamespaces.WordprocessingML); tblIndType.Value = "dxa"; tblInd.Attributes.Append(tblIndType); this._tblPr.AppendChild(tblInd); } //append floating props if (tblpPr.Attributes.Count > 0) { this._tblPr.AppendChild(tblpPr); } //set borders if (this.brcTop != null) { var topBorder = this._nodeFactory.CreateNode(XmlNodeType.Element, "w", "top", OpenXmlNamespaces.WordprocessingML); appendBorderAttributes(this.brcTop, topBorder); addOrSetBorder(this._tblBorders, topBorder); } if (this.brcLeft != null) { var leftBorder = this._nodeFactory.CreateNode(XmlNodeType.Element, "w", "left", OpenXmlNamespaces.WordprocessingML); appendBorderAttributes(this.brcLeft, leftBorder); addOrSetBorder(this._tblBorders, leftBorder); } if (this.brcBottom != null) { var bottomBorder = this._nodeFactory.CreateNode(XmlNodeType.Element, "w", "bottom", OpenXmlNamespaces.WordprocessingML); appendBorderAttributes(this.brcBottom, bottomBorder); addOrSetBorder(this._tblBorders, bottomBorder); } if (this.brcRight != null) { var rightBorder = this._nodeFactory.CreateNode(XmlNodeType.Element, "w", "right", OpenXmlNamespaces.WordprocessingML); appendBorderAttributes(this.brcRight, rightBorder); addOrSetBorder(this._tblBorders, rightBorder); } if (this.brcHorz != null) { var insideHBorder = this._nodeFactory.CreateNode(XmlNodeType.Element, "w", "insideH", OpenXmlNamespaces.WordprocessingML); appendBorderAttributes(this.brcHorz, insideHBorder); addOrSetBorder(this._tblBorders, insideHBorder); } if (this.brcVert != null) { var insideVBorder = this._nodeFactory.CreateNode(XmlNodeType.Element, "w", "insideV", OpenXmlNamespaces.WordprocessingML); appendBorderAttributes(this.brcVert, insideVBorder); addOrSetBorder(this._tblBorders, insideVBorder); } if (this._tblBorders.ChildNodes.Count > 0) { this._tblPr.AppendChild(this._tblBorders); } //append layout type tblLayout.Attributes.Append(layoutType); this._tblPr.AppendChild(tblLayout); //append margins if (marginLeft == 0 && gabHalf != 0) { appendDxaElement(tblCellMar, "left", gabHalf.ToString(), true); } else { appendDxaElement(tblCellMar, "left", marginLeft.ToString(), true); } if (marginRight == 0 && gabHalf != 0) { appendDxaElement(tblCellMar, "right", gabHalf.ToString(), true); } else { appendDxaElement(tblCellMar, "right", marginRight.ToString(), true); } this._tblPr.AppendChild(tblCellMar); //write Properties if (this._tblPr.ChildNodes.Count > 0 || this._tblPr.Attributes.Count > 0) { this._tblPr.WriteTo(this._writer); } //append the grid this._tblGrid = this._nodeFactory.CreateElement("w", "tblGrid", OpenXmlNamespaces.WordprocessingML); foreach (short colW in this._grid) { var gridCol = this._nodeFactory.CreateElement("w", "gridCol", OpenXmlNamespaces.WordprocessingML); var gridColW = this._nodeFactory.CreateAttribute("w", "w", OpenXmlNamespaces.WordprocessingML); gridColW.Value = colW.ToString(); gridCol.Attributes.Append(gridColW); this._tblGrid.AppendChild(gridCol); } this._tblGrid.WriteTo(this._writer); }