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

  

原文地址:https://www.cnblogs.com/gxrsprite/p/9480120.html

时间: 2024-07-29 17:08:08

rtf格式 C#设置字间距 CharacterSpacing的相关文章

Android TextView 设置字间距和行间距

1.如果您是要设置TextView的行间距 请使用(不用看2了) android:lineSpacingMultiplier="1.8" 2.如果您是要设置字间距需要自定义控件 <pre name="code" class="java">import android.content.Context; import android.graphics.*; import android.text.TextUtils; import and

UILabel设置行间距和字间距并计算高度-b

#define UILABEL_LINE_SPACE 6 #define HEIGHT [ [ UIScreen mainScreen ] bounds ].size.height //给UILabel设置行间距和字间距 -(void)setLabelSpace:(UILabel*)label withValue:(NSString*)str withFont:(UIFont*)font { NSMutableParagraphStyle *paraStyle = [[NSMutablePara

CSS缩进,CSS字间距

CSS缩进: ? 1 text-indent:2em; 代码意思就是缩进2个字.可以可换成px单位来使用.text-indent:10px;完整一点的写法如下,也可用在<a><p>标签里面,熟能生巧: ? 1 2 <divstyle="width:300px; text-indent:2em;">CSS学习网,Javasrcipt学习及经验分享,圆点网. </div> CSS字间距: letter-spacing字与字间距,例子: <

css 字间距

接下来我们来介绍下css 字间距,使用css来控制字与字之间距离,也叫css字间距方法. 使用到的css样式属性单词text-indent抬头距离,letter-spacing字与字间距. Css字间距.div css字符间距样式实例1.text-indent设置抬头距离css缩进即对,对应div设置css样式text-indent : 20px; 缩进了20px 2.letter-spacing来设置字与字间距_字符间距离,字体间距css样式即对对应div设置css属性样式为letter-sp

使用CSS设置行间距,字间距

原链接:http://blog.sina.com.cn/s/blog_a03d702f01013u3q.html 字间距1.text-indent设置抬头距离css缩进即对,对应div设置css样式text-indent : 20px; 缩进了20px 2.letter-spacing来设置字与字间距_字符间距离,字体间距css样式即对对应div设置css属性样式为letter-spacing:8px;,字间距为8px 行间距 在CSS中,可以设置行间距,格式如下:标签名{line-height

C#-ColorDialog,FontDialog-文字格式的设置---ShinePans

文字格式设置: 字体颜色设置: 测试: Program.cs: using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; namespace ColorDialog { static class Program { /// <summary> /// 应用程序的主入口点. /// </summary> [STAThread] static void M

label 设置行间距 字间距

#import "MyLabel.h" #import <CoreText/CoreText.h> @implementation MyLabel +(void)setLabel:(UILabel *)custonLabel andContenStr:(NSString *)content andlineSpace:(CGFloat)lineSpace andHeightSpace:(long)fontSpace{ NSMutableAttributedString *st

rtf格式的一些说明,转载的

RTF是Rich TextFormat的缩写,意即多文本格式.这是一种类似DOC格式(Word文档)的文件,有很好的兼容性,使用Windows"附件"中的"写字板"就能打开并进行编辑.RTF是一种非常流行的文件结构,很多文字编辑器都支持它.一般的格式设置,比如字体和段落设置,页面设置等等信息都可以存在RTF格式中,它能在一定程度上实现word与wps文件之间的互访. 一个RTF文件由未格式化文本.控制字.控制符号和组组成.为了更容易的转换,一个标准的RTF文件应该仅

报表XML导出rtf格式,结果在浏览器中打开XML文件。用360浏览器下载rtf文件打开后出现Authentication failed 问题

报表XML导出rtf格式,结果在浏览器中打开XML文件.用360浏览器下载rtf文件打开后出现Authentication failed 问题 直接上问题图: 问题描述:在Oracle EBS中执行"资源事务处理 XML"请求,选择输出rtf格式,完成后查看输出,却在浏览器中打开了XML文件.        提示:需要检查一下是否有对应的模板文件和模板定义有效时间.        解决方案:1. 查看日志. 从中可以看出出错原因,以及模板代码.2. 添加Oracle XML Publi