zoukankan      html  css  js  c++  java
  • 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));
            }
    

      

  • 相关阅读:
    python基础十一之装饰器进阶
    python基础十之装饰器
    python基础九之函数
    python基础八之文件操作
    python基础七之copy
    python基础七之集合
    python基础数据类型汇总
    python基础六之编码
    synchronized关键字的内存语义
    对于this和当前线程的一些理解
  • 原文地址:https://www.cnblogs.com/gxrsprite/p/9480120.html
Copyright © 2011-2022 走看看