rtf格式 C#设置字间距 CharacterSpacing

richtextbox空间中操作行间距段间距都可以用发送消息解决,但是字间距却鲜有人关注,无法通过PARAFORMAT2消息解决,只能直接操作rtf格式

字间距主要就是要控制 expand expandtw 这些rtf属性

下面给出一个插入代码,只能对简单点的rtf进行操作,word保存出来的rtf不一定可以

操作rtf格式的类库使用RtfTree

 

        public void SetCharacterSpacing(int spacing)
        {
            spacing--;
            if (spacing < 0)
            {
                return;
            }

            RtfTree doc = new RtfTree();
            doc.LoadRtfText(this.Rtf);
            RtfNodeCollection fileds = doc.MainGroup.ChildNodes;
            bool hasltrch = false;
            bool hasrtlch = false;
            bool hasText = !string.IsNullOrEmpty(this.Text);
            foreach (RtfTreeNode f in fileds)
            {
                if (f.NodeType == RtfNodeType.Keyword)
                {
                    if (f.NodeKey == "expnd")
                    {
                        f.Parameter = spacing;
                    }
                    if (f.NodeKey == "expndtw")
                    {
                        f.Parameter = spacing * 5;
                    }
                    if (f.NodeKey == "ltrch")
                    {
                        hasltrch = true;
                    }
                    if (f.NodeKey == "rtlch")
                    {
                        hasrtlch = true;
                    }
                }

            }

            if (spacing > 0)
            {
                if (hasText)
                {
                    bool ispar = false;

                    for (int i = fileds.Count - 1; i >= 0; i--)
                    {
                        var f = fileds[i];
                        if (f.NodeKey == "par")
                        {
                            ispar = true;

                        }
                        if (f.NodeKey == "u" || f.NodeKey == "'" || f.NodeType == RtfNodeType.Text)
                        {
                            continue;
                        }
                        else
                        {
                            if (ispar)
                            {
                                if (fileds[i - 1].NodeKey.Contains("expnd"))
                                {
                                    continue;
                                }
                                InsertLetterExpand(spacing, doc, f.Index);
                                ispar = false;
                            }
                        }
                        if (f.NodeType == RtfNodeType.Group)
                        {
                            if (f.ChildNodes[0].NodeKey == "colortbl" || f.ChildNodes[0].NodeKey == "fonttbl")
                            {
                                if (!doc.MainGroup[f.Index + 1].NodeKey.Contains("expnd"))
                                {
                                    InsertLetterExpand(spacing, doc, f.Index + 1);
                                    i -= 2;
                                }
                                else
                                {
                                    continue;
                                }

                            }
                        }
                        if (f.NodeKey == "viewkind")
                        {
                            if (!doc.MainGroup[f.Index - 1].NodeKey.Contains("expnd"))
                            {
                                InsertLetterExpand(spacing, doc, f.Index);
                                i--;
                            }
                            continue;
                        }
                    }
                }
                else
                {
                    for (int i = fileds.Count - 1; i >= 0; i--)
                    {
                        var f = fileds[i];
                        if (f.NodeKey == "par")
                        {
                            InsertLetterExpand(spacing, doc, f.Index - 1);
                            break;
                        }
                    }
                }
            }



            if (!hasltrch)
            {
                doc.MainGroup.InsertChild(1, new RtfTreeNode(RtfNodeType.Keyword, "ltrch", false, 0));
            }
            if (!hasrtlch)
            {
                doc.MainGroup.InsertChild(1, new RtfTreeNode(RtfNodeType.Keyword, "rtlch", false, 0));
            }

            this.Rtf = doc.Rtf;
        }

        private void InsertLetterExpand(int spacing, RtfTree doc, int index)
        {
            doc.MainGroup.InsertChild(index, new RtfTreeNode(RtfNodeType.Keyword, "expnd", true, spacing));
            doc.MainGroup.InsertChild(index, new RtfTreeNode(RtfNodeType.Keyword, "expndtw", true, spacing * 5));
        }

  

你可能感兴趣的:(rtf格式 C#设置字间距 CharacterSpacing)