博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
rtf格式 C#设置字间距 CharacterSpacing
阅读量:4330 次
发布时间:2019-06-06

本文共 4613 字,大约阅读时间需要 15 分钟。

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

  

转载于:https://www.cnblogs.com/gxrsprite/p/9480120.html

你可能感兴趣的文章
su 与 su - 区别
查看>>
C语言编程-9_4 字符统计
查看>>
在webconfig中写好连接后,在程序中如何调用?
查看>>
限制用户不能删除SharePoint列表中的条目(项目)
查看>>
【Linux网络编程】使用GDB调试程序
查看>>
feign调用spring clound eureka 注册中心服务
查看>>
ZT:Linux上安装JDK,最准确
查看>>
LimeJS指南3
查看>>
关于C++ const成员的一些细节
查看>>
《代码大全》学习摘要(五)软件构建中的设计(下)
查看>>
C#检测驱动是否安装的问题
查看>>
web-4. 装饰页面的图像
查看>>
微信测试账户
查看>>
Android ListView上拉获取下一页
查看>>
算法练习题
查看>>
学习使用Django一 安装虚拟环境
查看>>
Hibernate视频学习笔记(8)Lazy策略
查看>>
CSS3 结构性伪类选择器(1)
查看>>
IOS 杂笔-14(被人遗忘的owner)
查看>>
自动测试用工具
查看>>