public void Apply(ListTable rglst)
        {
            _writer.WriteStartElement("w", "numbering", OpenXmlNamespaces.WordprocessingML);

            for (int i = 0; i < rglst.Count; i++)
            {
                ListData lstf = rglst[i];

                //start abstractNum
                _writer.WriteStartElement("w", "abstractNum", OpenXmlNamespaces.WordprocessingML);

                _writer.WriteAttributeString("w", "abstractNumId", OpenXmlNamespaces.WordprocessingML, i.ToString());

                //nsid
                _writer.WriteStartElement("w", "nsid", OpenXmlNamespaces.WordprocessingML);
                _writer.WriteAttributeString("w", "val", OpenXmlNamespaces.WordprocessingML, String.Format("{0:X8}", lstf.lsid));
                _writer.WriteEndElement();

                //multiLevelType
                _writer.WriteStartElement("w", "multiLevelType", OpenXmlNamespaces.WordprocessingML);
                if (lstf.fHybrid)
                {
                    _writer.WriteAttributeString("w", "val", OpenXmlNamespaces.WordprocessingML, "hybridMultilevel");
                }
                else if (lstf.fSimpleList)
                {
                    _writer.WriteAttributeString("w", "val", OpenXmlNamespaces.WordprocessingML, "singleLevel");
                }
                else
                {
                    _writer.WriteAttributeString("w", "val", OpenXmlNamespaces.WordprocessingML, "multilevel");
                }
                _writer.WriteEndElement();

                //template
                _writer.WriteStartElement("w", "tmpl", OpenXmlNamespaces.WordprocessingML);
                _writer.WriteAttributeString("w", "val", OpenXmlNamespaces.WordprocessingML, String.Format("{0:X8}", lstf.tplc));
                _writer.WriteEndElement();

                //writes the levels
                for (int j = 0; j < lstf.rglvl.Length; j++)
                {
                    ListLevel lvl = lstf.rglvl[j];

                    _writer.WriteStartElement("w", "lvl", OpenXmlNamespaces.WordprocessingML);
                    _writer.WriteAttributeString("w", "ilvl", OpenXmlNamespaces.WordprocessingML, j.ToString());

                    //starts at
                    _writer.WriteStartElement("w", "start", OpenXmlNamespaces.WordprocessingML);
                    _writer.WriteAttributeString("w", "val", OpenXmlNamespaces.WordprocessingML, lvl.iStartAt.ToString());
                    _writer.WriteEndElement();

                    //number format
                    _writer.WriteStartElement("w", "numFmt", OpenXmlNamespaces.WordprocessingML);
                    _writer.WriteAttributeString("w", "val", OpenXmlNamespaces.WordprocessingML, GetNumberFormat(lvl.nfc));
                    _writer.WriteEndElement();

                    //suffix
                    _writer.WriteStartElement("w", "suff", OpenXmlNamespaces.WordprocessingML);
                    _writer.WriteAttributeString("w", "val", OpenXmlNamespaces.WordprocessingML, lvl.ixchFollow.ToString());
                    _writer.WriteEndElement();

                    //style
                    //The style id is used for a reverse reference.
                    //It can happen that the reference points to the wrong style.
                    Int16 styleIndex = lstf.rgistd[j];
                    if (styleIndex != ListData.ISTD_NIL)
                    {
                        _writer.WriteStartElement("w", "pStyle", OpenXmlNamespaces.WordprocessingML);
                        _writer.WriteAttributeString("w", "val", OpenXmlNamespaces.WordprocessingML, StyleSheetMapping.MakeStyleId(_ctx.Doc.Styles.Styles[styleIndex]));
                        _writer.WriteEndElement();
                    }

                    //Number level text
                    _writer.WriteStartElement("w", "lvlText", OpenXmlNamespaces.WordprocessingML);
                    _writer.WriteAttributeString("w", "val", OpenXmlNamespaces.WordprocessingML, getLvlText(lvl.xst));
                    _writer.WriteEndElement();

                    //jc
                    _writer.WriteStartElement("w", "lvlJc", OpenXmlNamespaces.WordprocessingML);
                    _writer.WriteAttributeString("w", "val", OpenXmlNamespaces.WordprocessingML, ((LevelJustification)lvl.jc).ToString());
                    _writer.WriteEndElement();

                    //pPr
                    lvl.grpprlPapx.Convert(new ParagraphPropertiesMapping(_writer, _ctx, _parentDoc, null));

                    //rPr
                    lvl.grpprlChpx.Convert(new CharacterPropertiesMapping(_writer, _parentDoc, new RevisionData(lvl.grpprlChpx), lvl.grpprlPapx, false));

                    _writer.WriteEndElement();
                }

                //end abstractNum
                _writer.WriteEndElement();
            }

            //write the overrides
            for (int i = 0; i < _ctx.Doc.ListFormatOverrideTable.Count; i++)
            {
                ListFormatOverride lfo = _ctx.Doc.ListFormatOverrideTable[i];

                //start num
                _writer.WriteStartElement("w", "num", OpenXmlNamespaces.WordprocessingML);
                _writer.WriteAttributeString("w", "numId", OpenXmlNamespaces.WordprocessingML, (i + 1).ToString());

                int index = FindIndexbyId(rglst, lfo.lsid);

                _writer.WriteStartElement("w", "abstractNumId", OpenXmlNamespaces.WordprocessingML);
                _writer.WriteAttributeString("w", "val", OpenXmlNamespaces.WordprocessingML, index.ToString());

                _writer.WriteEndElement();
                _writer.WriteEndElement();
            }

            _writer.WriteEndElement();
            _writer.Flush();
        }
Пример #2
0
        public void Apply(ParagraphPropertyExceptions papx)
        {
            XmlElement ind     = _nodeFactory.CreateElement("w", "ind", OpenXmlNamespaces.WordprocessingML);
            XmlElement numPr   = _nodeFactory.CreateElement("w", "numPr", OpenXmlNamespaces.WordprocessingML);
            XmlElement pBdr    = _nodeFactory.CreateElement("w", "pBdr", OpenXmlNamespaces.WordprocessingML);
            XmlElement spacing = _nodeFactory.CreateElement("w", "spacing", OpenXmlNamespaces.WordprocessingML);
            XmlElement jc      = null;

            //append style id , do not append "Normal" style (istd 0)
            XmlElement   pStyle  = _nodeFactory.CreateElement("w", "pStyle", OpenXmlNamespaces.WordprocessingML);
            XmlAttribute styleId = _nodeFactory.CreateAttribute("w", "val", OpenXmlNamespaces.WordprocessingML);

            styleId.Value = StyleSheetMapping.MakeStyleId(_parentDoc.Styles.Styles[papx.istd]);
            pStyle.Attributes.Append(styleId);
            _pPr.AppendChild(pStyle);

            //append formatting of paragraph end mark
            if (_paraEndChpx != null)
            {
                XmlElement rPr = _nodeFactory.CreateElement("w", "rPr", OpenXmlNamespaces.WordprocessingML);

                //append properties
                _paraEndChpx.Convert(new CharacterPropertiesMapping(rPr, _ctx.Doc, new RevisionData(_paraEndChpx), papx, false));

                RevisionData rev = new RevisionData(_paraEndChpx);
                //append delete infos
                if (rev.Type == RevisionData.RevisionType.Deleted)
                {
                    XmlElement del = _nodeFactory.CreateElement("w", "del", OpenXmlNamespaces.WordprocessingML);
                    rPr.AppendChild(del);
                }

                if (rPr.ChildNodes.Count > 0)
                {
                    _pPr.AppendChild(rPr);
                }
            }

            foreach (SinglePropertyModifier sprm in papx.grpprl)
            {
                switch (sprm.OpCode)
                {
                //rsid for paragraph property enditing (write to parent element)
                case SinglePropertyModifier.OperationCode.sprmPRsid:
                    string rsid = String.Format("{0:x8}", System.BitConverter.ToInt32(sprm.Arguments, 0));
                    _ctx.AddRsid(rsid);
                    _writer.WriteAttributeString("w", "rsidP", OpenXmlNamespaces.WordprocessingML, rsid);
                    break;

                //attributes
                case SinglePropertyModifier.OperationCode.sprmPIpgp:
                    XmlAttribute divId = _nodeFactory.CreateAttribute("w", "divId", OpenXmlNamespaces.WordprocessingML);
                    divId.Value = System.BitConverter.ToUInt32(sprm.Arguments, 0).ToString();
                    _pPr.Attributes.Append(divId);
                    break;

                case SinglePropertyModifier.OperationCode.sprmPFAutoSpaceDE:
                    appendFlagAttribute(_pPr, sprm, "autoSpaceDE");
                    break;

                case SinglePropertyModifier.OperationCode.sprmPFAutoSpaceDN:
                    appendFlagAttribute(_pPr, sprm, "autoSpaceDN");
                    break;

                case SinglePropertyModifier.OperationCode.sprmPFBiDi:
                    appendFlagAttribute(_pPr, sprm, "bidi");
                    break;

                case SinglePropertyModifier.OperationCode.sprmPFContextualSpacing:
                    appendFlagAttribute(_pPr, sprm, "contextualSpacing");
                    break;

                //element flags
                case SinglePropertyModifier.OperationCode.sprmPFKeep:
                    appendFlagElement(_pPr, sprm, "keepLines", true);
                    break;

                case SinglePropertyModifier.OperationCode.sprmPFKeepFollow:
                    appendFlagElement(_pPr, sprm, "keepNext", true);
                    break;

                case SinglePropertyModifier.OperationCode.sprmPFKinsoku:
                    appendFlagElement(_pPr, sprm, "kinsoku", true);
                    break;

                case SinglePropertyModifier.OperationCode.sprmPFOverflowPunct:
                    appendFlagElement(_pPr, sprm, "overflowPunct", true);
                    break;

                case SinglePropertyModifier.OperationCode.sprmPFPageBreakBefore:
                    appendFlagElement(_pPr, sprm, "pageBreakBefore", true);
                    break;

                case SinglePropertyModifier.OperationCode.sprmPFNoAutoHyph:
                    appendFlagElement(_pPr, sprm, "su_pPressAutoHyphens", true);
                    break;

                case SinglePropertyModifier.OperationCode.sprmPFNoLineNumb:
                    appendFlagElement(_pPr, sprm, "su_pPressLineNumbers", true);
                    break;

                case SinglePropertyModifier.OperationCode.sprmPFNoAllowOverlap:
                    appendFlagElement(_pPr, sprm, "su_pPressOverlap", true);
                    break;

                case SinglePropertyModifier.OperationCode.sprmPFTopLinePunct:
                    appendFlagElement(_pPr, sprm, "topLinePunct", true);
                    break;

                case SinglePropertyModifier.OperationCode.sprmPFWidowControl:
                    appendFlagElement(_pPr, sprm, "widowControl", true);
                    break;

                case SinglePropertyModifier.OperationCode.sprmPFWordWrap:
                    appendFlagElement(_pPr, sprm, "wordWrap", true);
                    break;

                //indentation
                case SinglePropertyModifier.OperationCode.sprmPDxaLeft:
                case SinglePropertyModifier.OperationCode.sprmPDxaLeft80:
                case SinglePropertyModifier.OperationCode.sprmPNest:
                case SinglePropertyModifier.OperationCode.sprmPNest80:
                    appendValueAttribute(ind, "left", System.BitConverter.ToInt16(sprm.Arguments, 0).ToString());
                    break;

                case SinglePropertyModifier.OperationCode.sprmPDxcLeft:
                    appendValueAttribute(ind, "leftChars", System.BitConverter.ToInt16(sprm.Arguments, 0).ToString());
                    break;

                case SinglePropertyModifier.OperationCode.sprmPDxaLeft1:
                case SinglePropertyModifier.OperationCode.sprmPDxaLeft180:
                    Int16  flValue = System.BitConverter.ToInt16(sprm.Arguments, 0);
                    string flName;
                    if (flValue >= 0)
                    {
                        flName = "firstLine";
                    }
                    else
                    {
                        flName   = "hanging";
                        flValue *= -1;
                    }
                    appendValueAttribute(ind, flName, flValue.ToString());
                    break;

                case SinglePropertyModifier.OperationCode.sprmPDxcLeft1:
                    appendValueAttribute(ind, "firstLineChars", System.BitConverter.ToInt16(sprm.Arguments, 0).ToString());
                    break;

                case SinglePropertyModifier.OperationCode.sprmPDxaRight:
                case SinglePropertyModifier.OperationCode.sprmPDxaRight80:
                    appendValueAttribute(ind, "right", System.BitConverter.ToInt16(sprm.Arguments, 0).ToString());
                    break;

                case SinglePropertyModifier.OperationCode.sprmPDxcRight:
                    appendValueAttribute(ind, "rightChars", System.BitConverter.ToInt16(sprm.Arguments, 0).ToString());
                    break;

                //spacing
                case SinglePropertyModifier.OperationCode.sprmPDyaBefore:
                    XmlAttribute before = _nodeFactory.CreateAttribute("w", "before", OpenXmlNamespaces.WordprocessingML);
                    before.Value = System.BitConverter.ToUInt16(sprm.Arguments, 0).ToString();
                    spacing.Attributes.Append(before);
                    break;

                case SinglePropertyModifier.OperationCode.sprmPDyaAfter:
                    XmlAttribute after = _nodeFactory.CreateAttribute("w", "after", OpenXmlNamespaces.WordprocessingML);
                    after.Value = System.BitConverter.ToUInt16(sprm.Arguments, 0).ToString();
                    spacing.Attributes.Append(after);
                    break;

                case SinglePropertyModifier.OperationCode.sprmPFDyaAfterAuto:
                    XmlAttribute afterAutospacing = _nodeFactory.CreateAttribute("w", "afterAutospacing", OpenXmlNamespaces.WordprocessingML);
                    afterAutospacing.Value = sprm.Arguments[0].ToString();
                    spacing.Attributes.Append(afterAutospacing);
                    break;

                case SinglePropertyModifier.OperationCode.sprmPFDyaBeforeAuto:
                    XmlAttribute beforeAutospacing = _nodeFactory.CreateAttribute("w", "beforeAutospacing", OpenXmlNamespaces.WordprocessingML);
                    beforeAutospacing.Value = sprm.Arguments[0].ToString();
                    spacing.Attributes.Append(beforeAutospacing);
                    break;

                case SinglePropertyModifier.OperationCode.sprmPDyaLine:
                    LineSpacingDescriptor lspd = new LineSpacingDescriptor(sprm.Arguments);
                    XmlAttribute          line = _nodeFactory.CreateAttribute("w", "line", OpenXmlNamespaces.WordprocessingML);
                    line.Value = Math.Abs(lspd.dyaLine).ToString();
                    spacing.Attributes.Append(line);
                    XmlAttribute lineRule = _nodeFactory.CreateAttribute("w", "lineRule", OpenXmlNamespaces.WordprocessingML);
                    if (!lspd.fMultLinespace && lspd.dyaLine < 0)
                    {
                        lineRule.Value = "exact";
                    }
                    else if (!lspd.fMultLinespace && lspd.dyaLine > 0)
                    {
                        lineRule.Value = "atLeast";
                    }
                    //no line rule means auto
                    spacing.Attributes.Append(lineRule);
                    break;

                //justification code
                case SinglePropertyModifier.OperationCode.sprmPJc:
                case SinglePropertyModifier.OperationCode.sprmPJc80:
                    jc = _nodeFactory.CreateElement("w", "jc", OpenXmlNamespaces.WordprocessingML);
                    XmlAttribute jcVal = _nodeFactory.CreateAttribute("w", "val", OpenXmlNamespaces.WordprocessingML);
                    jcVal.Value = ((Global.JustificationCode)sprm.Arguments[0]).ToString();
                    jc.Attributes.Append(jcVal);
                    break;

                //borders
                //case 0x461C:
                case SinglePropertyModifier.OperationCode.sprmPBrcTop:
                //case 0x4424:
                case SinglePropertyModifier.OperationCode.sprmPBrcTop80:
                    XmlNode topBorder = _nodeFactory.CreateNode(XmlNodeType.Element, "w", "top", OpenXmlNamespaces.WordprocessingML);
                    appendBorderAttributes(new BorderCode(sprm.Arguments), topBorder);
                    addOrSetBorder(pBdr, topBorder);
                    break;

                //case 0x461D:
                case SinglePropertyModifier.OperationCode.sprmPBrcLeft:
                //case 0x4425:
                case SinglePropertyModifier.OperationCode.sprmPBrcLeft80:
                    XmlNode leftBorder = _nodeFactory.CreateNode(XmlNodeType.Element, "w", "left", OpenXmlNamespaces.WordprocessingML);
                    appendBorderAttributes(new BorderCode(sprm.Arguments), leftBorder);
                    addOrSetBorder(pBdr, leftBorder);
                    break;

                //case 0x461E:
                case SinglePropertyModifier.OperationCode.sprmPBrcBottom:
                //case 0x4426:
                case SinglePropertyModifier.OperationCode.sprmPBrcBottom80:
                    XmlNode bottomBorder = _nodeFactory.CreateNode(XmlNodeType.Element, "w", "bottom", OpenXmlNamespaces.WordprocessingML);
                    appendBorderAttributes(new BorderCode(sprm.Arguments), bottomBorder);
                    addOrSetBorder(pBdr, bottomBorder);
                    break;

                //case 0x461F:
                case SinglePropertyModifier.OperationCode.sprmPBrcRight:
                //case 0x4427:
                case SinglePropertyModifier.OperationCode.sprmPBrcRight80:
                    XmlNode rightBorder = _nodeFactory.CreateNode(XmlNodeType.Element, "w", "right", OpenXmlNamespaces.WordprocessingML);
                    appendBorderAttributes(new BorderCode(sprm.Arguments), rightBorder);
                    addOrSetBorder(pBdr, rightBorder);
                    break;

                //case 0x4620:
                case SinglePropertyModifier.OperationCode.sprmPBrcBetween:
                //case 0x4428:
                case SinglePropertyModifier.OperationCode.sprmPBrcBetween80:
                    XmlNode betweenBorder = _nodeFactory.CreateNode(XmlNodeType.Element, "w", "between", OpenXmlNamespaces.WordprocessingML);
                    appendBorderAttributes(new BorderCode(sprm.Arguments), betweenBorder);
                    addOrSetBorder(pBdr, betweenBorder);
                    break;

                //case 0x4621:
                case SinglePropertyModifier.OperationCode.sprmPBrcBar:
                //case 0x4629:
                case SinglePropertyModifier.OperationCode.sprmPBrcBar80:
                    XmlNode barBorder = _nodeFactory.CreateNode(XmlNodeType.Element, "w", "bar", OpenXmlNamespaces.WordprocessingML);
                    appendBorderAttributes(new BorderCode(sprm.Arguments), barBorder);
                    addOrSetBorder(pBdr, barBorder);
                    break;

                //shading
                case SinglePropertyModifier.OperationCode.sprmPShd80:
                case SinglePropertyModifier.OperationCode.sprmPShd:
                    ShadingDescriptor desc = new ShadingDescriptor(sprm.Arguments);
                    appendShading(_pPr, desc);
                    break;

                //numbering
                case SinglePropertyModifier.OperationCode.sprmPIlvl:
                    appendValueElement(numPr, "ilvl", sprm.Arguments[0].ToString(), true);
                    break;

                case SinglePropertyModifier.OperationCode.sprmPIlfo:
                    UInt16 val = System.BitConverter.ToUInt16(sprm.Arguments, 0);
                    appendValueElement(numPr, "numId", val.ToString(), true);

                    ////check if there is a ilvl reference, if not, check the count of LVLs.
                    ////if only one LVL exists in the referenced list, create a hard reference to that LVL
                    //if (containsLvlReference(papx.grpprl) == false)
                    //{
                    //    ListFormatOverride lfo = _ctx.Doc.ListFormatOverrideTable[val];
                    //    int index = NumberingMapping.FindIndexbyId(_ctx.Doc.ListTable, lfo.lsid);
                    //    ListData lst = _ctx.Doc.ListTable[index];
                    //    if (lst.rglvl.Length == 1)
                    //    {
                    //        appendValueElement(numPr, "ilvl", "0", true);
                    //    }
                    //}
                    break;

                //tabs
                case SinglePropertyModifier.OperationCode.sprmPChgTabsPapx:
                case SinglePropertyModifier.OperationCode.sprmPChgTabs:
                    XmlElement tabs = _nodeFactory.CreateElement("w", "tabs", OpenXmlNamespaces.WordprocessingML);
                    int        pos  = 0;
                    //read the removed tabs
                    byte itbdDelMax = sprm.Arguments[pos];
                    pos++;
                    for (int i = 0; i < itbdDelMax; i++)
                    {
                        XmlElement tab = _nodeFactory.CreateElement("w", "tab", OpenXmlNamespaces.WordprocessingML);
                        //clear
                        XmlAttribute tabsVal = _nodeFactory.CreateAttribute("w", "val", OpenXmlNamespaces.WordprocessingML);
                        tabsVal.Value = "clear";
                        tab.Attributes.Append(tabsVal);
                        //position
                        XmlAttribute tabsPos = _nodeFactory.CreateAttribute("w", "pos", OpenXmlNamespaces.WordprocessingML);
                        tabsPos.Value = System.BitConverter.ToInt16(sprm.Arguments, pos).ToString();
                        tab.Attributes.Append(tabsPos);
                        tabs.AppendChild(tab);

                        //skip the tolerence array in sprm 0xC615
                        if (sprm.OpCode == SinglePropertyModifier.OperationCode.sprmPChgTabs)
                        {
                            pos += 4;
                        }
                        else
                        {
                            pos += 2;
                        }
                    }
                    //read the added tabs
                    byte itbdAddMax = sprm.Arguments[pos];
                    pos++;
                    for (int i = 0; i < itbdAddMax; i++)
                    {
                        TabDescriptor tbd = new TabDescriptor(sprm.Arguments[pos + (itbdAddMax * 2) + i]);
                        XmlElement    tab = _nodeFactory.CreateElement("w", "tab", OpenXmlNamespaces.WordprocessingML);
                        //justification
                        XmlAttribute tabsVal = _nodeFactory.CreateAttribute("w", "val", OpenXmlNamespaces.WordprocessingML);
                        tabsVal.Value = ((Global.JustificationCode)tbd.jc).ToString();
                        tab.Attributes.Append(tabsVal);
                        //tab leader type
                        XmlAttribute leader = _nodeFactory.CreateAttribute("w", "leader", OpenXmlNamespaces.WordprocessingML);
                        leader.Value = ((Global.TabLeader)tbd.tlc).ToString();
                        tab.Attributes.Append(leader);
                        //position
                        XmlAttribute tabsPos = _nodeFactory.CreateAttribute("w", "pos", OpenXmlNamespaces.WordprocessingML);
                        tabsPos.Value = System.BitConverter.ToInt16(sprm.Arguments, pos + (i * 2)).ToString();
                        tab.Attributes.Append(tabsPos);
                        tabs.AppendChild(tab);
                    }
                    _pPr.AppendChild(tabs);
                    break;

                //frame properties

                case SinglePropertyModifier.OperationCode.sprmPPc:
                    //position code
                    byte flag = sprm.Arguments[0];
                    Global.VerticalPositionCode   pcVert = (Global.VerticalPositionCode)((flag & 0x30) >> 4);
                    Global.HorizontalPositionCode pcHorz = (Global.HorizontalPositionCode)((flag & 0xC0) >> 6);
                    appendValueAttribute(_framePr, "hAnchor", pcHorz.ToString());
                    appendValueAttribute(_framePr, "vAnchor", pcVert.ToString());
                    break;

                case SinglePropertyModifier.OperationCode.sprmPWr:
                    Global.TextFrameWrapping wrapping = (Global.TextFrameWrapping)sprm.Arguments[0];
                    appendValueAttribute(_framePr, "wrap", wrapping.ToString());
                    break;

                case SinglePropertyModifier.OperationCode.sprmPDxaAbs:
                    Int16 frameX = System.BitConverter.ToInt16(sprm.Arguments, 0);
                    appendValueAttribute(_framePr, "x", frameX.ToString());
                    break;

                case SinglePropertyModifier.OperationCode.sprmPDyaAbs:
                    Int16 frameY = System.BitConverter.ToInt16(sprm.Arguments, 0);
                    appendValueAttribute(_framePr, "y", frameY.ToString());
                    break;

                case SinglePropertyModifier.OperationCode.sprmPWHeightAbs:
                    Int16 frameHeight = System.BitConverter.ToInt16(sprm.Arguments, 0);
                    appendValueAttribute(_framePr, "h", frameHeight.ToString());
                    break;

                case SinglePropertyModifier.OperationCode.sprmPDxaWidth:
                    Int16 frameWidth = System.BitConverter.ToInt16(sprm.Arguments, 0);
                    appendValueAttribute(_framePr, "w", frameWidth.ToString());
                    break;

                case SinglePropertyModifier.OperationCode.sprmPDxaFromText:
                    Int16 frameSpaceH = System.BitConverter.ToInt16(sprm.Arguments, 0);
                    appendValueAttribute(_framePr, "hSpace", frameSpaceH.ToString());
                    break;

                case SinglePropertyModifier.OperationCode.sprmPDyaFromText:
                    Int16 frameSpaceV = System.BitConverter.ToInt16(sprm.Arguments, 0);
                    appendValueAttribute(_framePr, "vSpace", frameSpaceV.ToString());
                    break;

                //outline level
                case SinglePropertyModifier.OperationCode.sprmPOutLvl:
                    appendValueElement(_pPr, "outlineLvl", sprm.Arguments[0].ToString(), false);
                    break;

                default:
                    break;
                }
            }

            //append frame properties
            if (_framePr.Attributes.Count > 0)
            {
                _pPr.AppendChild(_framePr);
            }

            //append section properties
            if (_sepx != null)
            {
                XmlElement sectPr = _nodeFactory.CreateElement("w", "sectPr", OpenXmlNamespaces.WordprocessingML);
                _sepx.Convert(new SectionPropertiesMapping(sectPr, _ctx, _sectionNr));
                _pPr.AppendChild(sectPr);
            }

            //append indent
            if (ind.Attributes.Count > 0)
            {
                _pPr.AppendChild(ind);
            }

            //append spacing
            if (spacing.Attributes.Count > 0)
            {
                _pPr.AppendChild(spacing);
            }

            //append justification
            if (jc != null)
            {
                _pPr.AppendChild(jc);
            }

            //append numPr
            if (numPr.ChildNodes.Count > 0)
            {
                _pPr.AppendChild(numPr);
            }

            //append borders
            if (pBdr.ChildNodes.Count > 0)
            {
                _pPr.AppendChild(pBdr);
            }

            //write Properties
            if (_pPr.ChildNodes.Count > 0 || _pPr.Attributes.Count > 0)
            {
                _pPr.WriteTo(_writer);
            }
        }
Пример #3
0
        private void convertSprms(List <SinglePropertyModifier> sprms, XmlElement parent)
        {
            XmlElement   shd      = _nodeFactory.CreateElement("w", "shd", OpenXmlNamespaces.WordprocessingML);
            XmlElement   rFonts   = _nodeFactory.CreateElement("w", "rFonts", OpenXmlNamespaces.WordprocessingML);
            XmlElement   color    = _nodeFactory.CreateElement("w", "color", OpenXmlNamespaces.WordprocessingML);
            XmlAttribute colorVal = _nodeFactory.CreateAttribute("w", "val", OpenXmlNamespaces.WordprocessingML);
            XmlElement   lang     = _nodeFactory.CreateElement("w", "lang", OpenXmlNamespaces.WordprocessingML);

            foreach (SinglePropertyModifier sprm in sprms)
            {
                switch ((int)sprm.OpCode)
                {
                //style id
                case 0x4A30:
                    _currentIstd = System.BitConverter.ToUInt16(sprm.Arguments, 0);
                    appendValueElement(parent, "rStyle", StyleSheetMapping.MakeStyleId(_doc.Styles.Styles[_currentIstd]), true);
                    break;

                //Element flags
                case 0x085A:
                    appendFlagElement(parent, sprm, "rtl", true);
                    break;

                case 0x0835:
                    appendFlagElement(parent, sprm, "b", true);
                    break;

                case 0x085C:
                    appendFlagElement(parent, sprm, "bCs", true);
                    break;

                case 0x083B:
                    appendFlagElement(parent, sprm, "caps", true);;
                    break;

                case 0x0882:
                    appendFlagElement(parent, sprm, "cs", true);
                    break;

                case 0x2A53:
                    appendFlagElement(parent, sprm, "dstrike", true);
                    break;

                case 0x0858:
                    appendFlagElement(parent, sprm, "emboss", true);
                    break;

                case 0x0854:
                    appendFlagElement(parent, sprm, "imprint", true);
                    break;

                case 0x0836:
                    appendFlagElement(parent, sprm, "i", true);
                    break;

                case 0x085D:
                    appendFlagElement(parent, sprm, "iCs", true);
                    break;

                case 0x0875:
                    appendFlagElement(parent, sprm, "noProof", true);
                    break;

                case 0x0838:
                    appendFlagElement(parent, sprm, "outline", true);
                    break;

                case 0x0839:
                    appendFlagElement(parent, sprm, "shadow", true);
                    break;

                case 0x083A:
                    appendFlagElement(parent, sprm, "smallCaps", true);
                    break;

                case 0x0818:
                    appendFlagElement(parent, sprm, "specVanish", true);
                    break;

                case 0x0837:
                    appendFlagElement(parent, sprm, "strike", true);
                    break;

                case 0x083C:
                    appendFlagElement(parent, sprm, "vanish", true);
                    break;

                case 0x0811:
                    appendFlagElement(parent, sprm, "webHidden", true);
                    break;

                case 0x2A48:
                    SuperscriptIndex iss = (SuperscriptIndex)sprm.Arguments[0];
                    appendValueElement(parent, "vertAlign", iss.ToString(), true);
                    break;

                //language
                case 0x486D:
                case 0x4873:
                    //latin
                    LanguageId langid = new LanguageId(System.BitConverter.ToInt16(sprm.Arguments, 0));
                    langid.Convert(new LanguageIdMapping(lang, LanguageIdMapping.LanguageType.Default));
                    break;

                case 0x486E:
                case 0x4874:
                    //east asia
                    langid = new LanguageId(System.BitConverter.ToInt16(sprm.Arguments, 0));
                    langid.Convert(new LanguageIdMapping(lang, LanguageIdMapping.LanguageType.EastAsian));
                    break;

                case 0x485F:
                    //bidi
                    langid = new LanguageId(System.BitConverter.ToInt16(sprm.Arguments, 0));
                    langid.Convert(new LanguageIdMapping(lang, LanguageIdMapping.LanguageType.Complex));
                    break;

                //borders
                case 0x6865:
                case 0xCA72:
                    XmlNode bdr = _nodeFactory.CreateElement("w", "bdr", OpenXmlNamespaces.WordprocessingML);
                    appendBorderAttributes(new BorderCode(sprm.Arguments), bdr);
                    parent.AppendChild(bdr);
                    break;

                //shading
                case 0x4866:
                case 0xCA71:
                    ShadingDescriptor desc = new ShadingDescriptor(sprm.Arguments);
                    appendShading(parent, desc);
                    break;

                //color
                case 0x2A42:
                case 0x4A60:
                    colorVal.Value = ((Global.ColorIdentifier)(sprm.Arguments[0])).ToString();
                    break;

                case 0x6870:
                    //R
                    colorVal.Value = String.Format("{0:x2}", sprm.Arguments[0]);
                    //G
                    colorVal.Value += String.Format("{0:x2}", sprm.Arguments[1]);
                    //B
                    colorVal.Value += String.Format("{0:x2}", sprm.Arguments[2]);
                    break;

                //highlightning
                case 0x2A0C:
                    appendValueElement(parent, "highlight", ((Global.ColorIdentifier)sprm.Arguments[0]).ToString(), true);
                    break;

                //spacing
                case 0x8840:
                    appendValueElement(parent, "spacing", System.BitConverter.ToInt16(sprm.Arguments, 0).ToString(), true);
                    break;

                //font size
                case 0x4A43:
                    appendValueElement(parent, "sz", sprm.Arguments[0].ToString(), true);
                    break;

                case 0x484B:
                    appendValueElement(parent, "kern", System.BitConverter.ToInt16(sprm.Arguments, 0).ToString(), true);
                    break;

                case 0x4A61:
                    appendValueElement(parent, "szCs", System.BitConverter.ToInt16(sprm.Arguments, 0).ToString(), true);
                    break;

                //font family
                case 0x4A4F:
                    XmlAttribute   ascii = _nodeFactory.CreateAttribute("w", "ascii", OpenXmlNamespaces.WordprocessingML);
                    FontFamilyName ffn   = (FontFamilyName)_doc.FontTable.Data[System.BitConverter.ToUInt16(sprm.Arguments, 0)];
                    ascii.Value = ffn.xszFtn;
                    rFonts.Attributes.Append(ascii);
                    break;

                case 0x4A50:
                    XmlAttribute   eastAsia = _nodeFactory.CreateAttribute("w", "eastAsia", OpenXmlNamespaces.WordprocessingML);
                    FontFamilyName ffnAsia  = (FontFamilyName)_doc.FontTable.Data[System.BitConverter.ToUInt16(sprm.Arguments, 0)];
                    eastAsia.Value = ffnAsia.xszFtn;
                    rFonts.Attributes.Append(eastAsia);
                    break;

                case 0x4A51:
                    XmlAttribute   ansi    = _nodeFactory.CreateAttribute("w", "hAnsi", OpenXmlNamespaces.WordprocessingML);
                    FontFamilyName ffnAnsi = (FontFamilyName)_doc.FontTable.Data[System.BitConverter.ToUInt16(sprm.Arguments, 0)];
                    ansi.Value = ffnAnsi.xszFtn;
                    rFonts.Attributes.Append(ansi);
                    break;

                //Underlining
                case 0x2A3E:
                    appendValueElement(parent, "u", lowerFirstChar(((Global.UnderlineCode)sprm.Arguments[0]).ToString()), true);
                    break;

                //char width
                case 0x4852:
                    appendValueElement(parent, "w", System.BitConverter.ToInt16(sprm.Arguments, 0).ToString(), true);
                    break;

                //animation
                case 0x2859:
                    appendValueElement(parent, "effect", ((Global.TextAnimation)sprm.Arguments[0]).ToString(), true);
                    break;

                default:
                    break;
                }
            }

            //apend lang
            if (lang.Attributes.Count > 0)
            {
                parent.AppendChild(lang);
            }

            //append fonts
            if (rFonts.Attributes.Count > 0)
            {
                parent.AppendChild(rFonts);
            }

            //append color
            if (colorVal.Value != "")
            {
                color.Attributes.Append(colorVal);
                parent.AppendChild(color);
            }
        }
Пример #4
0
        public void Apply(TablePropertyExceptions tapx)
        {
            XmlElement   tblBorders = _nodeFactory.CreateElement("w", "tblBorders", OpenXmlNamespaces.WordprocessingML);
            XmlElement   tblCellMar = _nodeFactory.CreateElement("w", "tblCellMar", OpenXmlNamespaces.WordprocessingML);
            XmlElement   tblLayout  = _nodeFactory.CreateElement("w", "tblLayout", OpenXmlNamespaces.WordprocessingML);
            XmlElement   tblpPr     = _nodeFactory.CreateElement("w", "tblpPr", OpenXmlNamespaces.WordprocessingML);
            XmlAttribute layoutType = _nodeFactory.CreateAttribute("w", "type", OpenXmlNamespaces.WordprocessingML);

            layoutType.Value = "fixed";
            Int16 tblIndent   = 0;
            Int16 gabHalf     = 0;
            Int16 marginLeft  = 0;
            Int16 marginRight = 0;

            foreach (SinglePropertyModifier 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:
                    SprmTDefTable 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:
                    WidthType    fts   = (WidthType)sprm.Arguments[0];
                    Int16        width = System.BitConverter.ToInt16(sprm.Arguments, 1);
                    XmlElement   tblW  = _nodeFactory.CreateElement("w", "tblW", OpenXmlNamespaces.WordprocessingML);
                    XmlAttribute w     = _nodeFactory.CreateAttribute("w", "w", OpenXmlNamespaces.WordprocessingML);
                    w.Value = width.ToString();
                    XmlAttribute type = _nodeFactory.CreateAttribute("w", "type", OpenXmlNamespaces.WordprocessingML);
                    type.Value = fts.ToString();
                    tblW.Attributes.Append(type);
                    tblW.Attributes.Append(w);
                    _tblPr.AppendChild(tblW);
                    break;

                //justification
                case SinglePropertyModifier.OperationCode.sprmTJc:
                case  SinglePropertyModifier.OperationCode.sprmTJcRow:
                    appendValueElement(_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:
                    Int16 styleIndex = System.BitConverter.ToInt16(sprm.Arguments, 0);
                    if (_styles.Styles.Count > styleIndex)
                    {
                        string id = StyleSheetMapping.MakeStyleId(_styles.Styles[styleIndex]);
                        if (id != "TableNormal")
                        {
                            appendValueElement(_tblPr, "tblStyle", id, true);
                        }
                    }
                    break;

                //bidi
                case SinglePropertyModifier.OperationCode.sprmTFBiDi:
                case SinglePropertyModifier.OperationCode.sprmTFBiDi90:
                    appendValueElement(_tblPr, "bidiVisual", System.BitConverter.ToInt16(sprm.Arguments, 0).ToString(), true);
                    break;

                //table look
                case SinglePropertyModifier.OperationCode.sprmTTlp:
                    appendValueElement(_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];
                    Int16 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(_tblPr, "tblStyleRowBandSize", sprm.Arguments[0].ToString(), true);
                    break;

                //col count
                case SinglePropertyModifier.OperationCode.sprmTCVertBands:
                    appendValueElement(_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(_tblPr, "tblOverlap", tblOverlapVal, true);
                    break;

                //shading
                case SinglePropertyModifier.OperationCode.sprmTSetShdTable:
                    ShadingDescriptor desc = new ShadingDescriptor(sprm.Arguments);
                    appendShading(_tblPr, desc);
                    break;

                //borders 80 exceptions
                case SinglePropertyModifier.OperationCode.sprmTTableBorders80:
                    byte[] brc80 = new byte[4];
                    //top border
                    Array.Copy(sprm.Arguments, 0, brc80, 0, 4);
                    brcTop = new BorderCode(brc80);
                    //left
                    Array.Copy(sprm.Arguments, 4, brc80, 0, 4);
                    brcLeft = new BorderCode(brc80);
                    //bottom
                    Array.Copy(sprm.Arguments, 8, brc80, 0, 4);
                    brcBottom = new BorderCode(brc80);
                    //right
                    Array.Copy(sprm.Arguments, 12, brc80, 0, 4);
                    brcRight = new BorderCode(brc80);
                    //inside H
                    Array.Copy(sprm.Arguments, 16, brc80, 0, 4);
                    brcHorz = new BorderCode(brc80);
                    //inside V
                    Array.Copy(sprm.Arguments, 20, brc80, 0, 4);
                    brcVert = new BorderCode(brc80);
                    break;

                //border exceptions
                case SinglePropertyModifier.OperationCode.sprmTTableBorders:
                    byte[] brc = new byte[8];
                    //top border
                    Array.Copy(sprm.Arguments, 0, brc, 0, 8);
                    brcTop = new BorderCode(brc);
                    //left
                    Array.Copy(sprm.Arguments, 8, brc, 0, 8);
                    brcLeft = new BorderCode(brc);
                    //bottom
                    Array.Copy(sprm.Arguments, 16, brc, 0, 8);
                    brcBottom = new BorderCode(brc);
                    //right
                    Array.Copy(sprm.Arguments, 24, brc, 0, 8);
                    brcRight = new BorderCode(brc);
                    //inside H
                    Array.Copy(sprm.Arguments, 32, brc, 0, 8);
                    brcHorz = new BorderCode(brc);
                    //inside V
                    Array.Copy(sprm.Arguments, 40, brc, 0, 8);
                    brcVert = new BorderCode(brc);
                    break;

                //floating table properties
                case SinglePropertyModifier.OperationCode.sprmTPc:
                    byte flag = sprm.Arguments[0];
                    Global.VerticalPositionCode   pcVert = (Global.VerticalPositionCode)((flag & 0x30) >> 4);
                    Global.HorizontalPositionCode 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)
            {
                XmlElement   tblInd  = _nodeFactory.CreateElement("w", "tblInd", OpenXmlNamespaces.WordprocessingML);
                XmlAttribute tblIndW = _nodeFactory.CreateAttribute("w", "w", OpenXmlNamespaces.WordprocessingML);
                tblIndW.Value = tblIndent.ToString();
                tblInd.Attributes.Append(tblIndW);
                XmlAttribute tblIndType = _nodeFactory.CreateAttribute("w", "type", OpenXmlNamespaces.WordprocessingML);
                tblIndType.Value = "dxa";
                tblInd.Attributes.Append(tblIndType);
                _tblPr.AppendChild(tblInd);
            }

            //append floating props
            if (tblpPr.Attributes.Count > 0)
            {
                _tblPr.AppendChild(tblpPr);
            }

            //set borders
            if (brcTop != null)
            {
                XmlNode topBorder = _nodeFactory.CreateNode(XmlNodeType.Element, "w", "top", OpenXmlNamespaces.WordprocessingML);
                appendBorderAttributes(brcTop, topBorder);
                addOrSetBorder(_tblBorders, topBorder);
            }
            if (brcLeft != null)
            {
                XmlNode leftBorder = _nodeFactory.CreateNode(XmlNodeType.Element, "w", "left", OpenXmlNamespaces.WordprocessingML);
                appendBorderAttributes(brcLeft, leftBorder);
                addOrSetBorder(_tblBorders, leftBorder);
            }
            if (brcBottom != null)
            {
                XmlNode bottomBorder = _nodeFactory.CreateNode(XmlNodeType.Element, "w", "bottom", OpenXmlNamespaces.WordprocessingML);
                appendBorderAttributes(brcBottom, bottomBorder);
                addOrSetBorder(_tblBorders, bottomBorder);
            }
            if (brcRight != null)
            {
                XmlNode rightBorder = _nodeFactory.CreateNode(XmlNodeType.Element, "w", "right", OpenXmlNamespaces.WordprocessingML);
                appendBorderAttributes(brcRight, rightBorder);
                addOrSetBorder(_tblBorders, rightBorder);
            }
            if (brcHorz != null)
            {
                XmlNode insideHBorder = _nodeFactory.CreateNode(XmlNodeType.Element, "w", "insideH", OpenXmlNamespaces.WordprocessingML);
                appendBorderAttributes(brcHorz, insideHBorder);
                addOrSetBorder(_tblBorders, insideHBorder);
            }
            if (brcVert != null)
            {
                XmlNode insideVBorder = _nodeFactory.CreateNode(XmlNodeType.Element, "w", "insideV", OpenXmlNamespaces.WordprocessingML);
                appendBorderAttributes(brcVert, insideVBorder);
                addOrSetBorder(_tblBorders, insideVBorder);
            }
            if (_tblBorders.ChildNodes.Count > 0)
            {
                _tblPr.AppendChild(_tblBorders);
            }

            //append layout type
            tblLayout.Attributes.Append(layoutType);
            _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);
            }
            _tblPr.AppendChild(tblCellMar);

            //write Properties
            if (_tblPr.ChildNodes.Count > 0 || _tblPr.Attributes.Count > 0)
            {
                _tblPr.WriteTo(_writer);
            }

            //append the grid
            _tblGrid = _nodeFactory.CreateElement("w", "tblGrid", OpenXmlNamespaces.WordprocessingML);
            foreach (Int16 colW in _grid)
            {
                XmlElement   gridCol  = _nodeFactory.CreateElement("w", "gridCol", OpenXmlNamespaces.WordprocessingML);
                XmlAttribute gridColW = _nodeFactory.CreateAttribute("w", "w", OpenXmlNamespaces.WordprocessingML);
                gridColW.Value = colW.ToString();
                gridCol.Attributes.Append(gridColW);
                _tblGrid.AppendChild(gridCol);
            }
            _tblGrid.WriteTo(_writer);
        }