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));
            }
    

      

  • 相关阅读:
    07noip 统计数字 解题报告
    07noip 矩阵取数游戏 解题报告
    10 noip 乌龟棋 解题报告
    10 noip 引水入城 解题报告
    让我们来看一看C++ 简短自序
    curl post请求封装
    array_merge与数组加
    composer设置autoload自己的代码
    mysql使用笔记
    android入门:zxing学习笔记(六)
  • 原文地址:https://www.cnblogs.com/gxrsprite/p/9480120.html
Copyright © 2011-2022 走看看