- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
是否可以将文本粘贴到富文本框中,同时保留富文本框中用于粘贴内容的字体?
换句话说,我想从格式化的 Word 中复制一些内容(即:使用字体 X 并带有下划线和蓝色的文本),然后将其粘贴到我的 RichTextBox 中。
我希望粘贴的内容具有与我的 RichTextBox 相同的字体,但保留其原始颜色和下划线。
这样的事情可能吗?
我用的是winforms。
谢谢
最佳答案
开箱即用是不可能的。但是你可以这样做:
public void SpecialPaste()
{
var helperRichTextBox = new RichTextBox();
helperRichTextBox.Paste();
for(int i=0;i<helperRichTextBox.TextLength;++i)
{
helperRichTextBox.SelectionStart = i;
helperRichTextBox.SelectionLength = 1;
helperRichTextBox.SelectionFont = new Font(richTextBox1.SelectionFont.FontFamily, richTextBox1.SelectionFont.Size,helperRichTextBox.SelectionFont.Style);
}
richTextBox1.SelectedRtf = helperRichTextBox.Rtf;
}
这会将粘贴的 RTF 的字体更改为粘贴时插入符号位置之前的字符的字体。
如果您粘贴的文本很大(呃),我认为这会很快出现问题。此外,这可以在某种程度上进行优化,它只为一行中的所有字符设置一次字体,使用与 Hans 建议的相同的基本字体。
更新:
这是优化版本,它为一组连接的字符设置相同原始字体的字体:
public void SpecialPaste()
{
var helperRichTextBox = new RichTextBox();
helperRichTextBox.Paste();
helperRichTextBox.SelectionStart = 0;
helperRichTextBox.SelectionLength = 1;
Font lastFont = helperRichTextBox.SelectionFont;
int lastFontChange = 0;
for (int i = 0; i < helperRichTextBox.TextLength; ++i)
{
helperRichTextBox.SelectionStart = i;
helperRichTextBox.SelectionLength = 1;
if (!helperRichTextBox.SelectionFont.Equals(lastFont))
{
lastFont = helperRichTextBox.SelectionFont;
helperRichTextBox.SelectionStart = lastFontChange;
helperRichTextBox.SelectionLength = i - lastFontChange;
helperRichTextBox.SelectionFont = new Font(richTextBox1.SelectionFont.FontFamily, richTextBox1.SelectionFont.Size, helperRichTextBox.SelectionFont.Style);
lastFontChange = i;
}
}
helperRichTextBox.SelectionStart = helperRichTextBox.TextLength-1;
helperRichTextBox.SelectionLength = 1;
helperRichTextBox.SelectionFont = new Font(richTextBox1.Font.FontFamily, richTextBox1.Font.Size, helperRichTextBox.SelectionFont.Style);
richTextBox1.SelectedRtf = helperRichTextBox.Rtf;
}
这是非常难看的代码,我相信它可以改进和清理。但它做了它应该做的。
关于C#:在 RichTextBox 中粘贴 RTF,但保留颜色和格式(即:粗体、下划线等),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5477676/
有谁知道将背景颜色应用于表格单元格的 rtf 标签? 我知道表结构: {\rtf1\ansi\deff0 \trowd \clshdng10000\cellx1000 \clshdng10000
MS Exchange/Outlook 消息使用 MAPI 存储数据。一个常见的 MAPI 属性包含消息正文的富文本版本(0x1009、PR_RTF_COMPRESSED、PidTagRtfCompr
我需要以编程方式生成一个 RTF 文档,其中包含一个垂直合并列的表格,例如 ______________________________ | merged | foo | hello |
我正在尝试增加 柜台 使用以下代码在 BIP 报告的 for-each 循环中将变量增加 1。 然而,柜台 无论有多少循环,变量都不会增加。有人可以指导我如何做到这一点。 我也知道 position(
我需要在 RTF 文件中创建一个表。但是我不熟悉 RΤF。以下是这些 RTF 文件应该替换的文本文件示例: GENERAL JOURNAL
我正在尝试手动生成 RTF 文档,最终会以编程方式完成。我计划改进 pyRTF,使其可以生成“目录”,但我认为它不能。 我无法使用 RTF 控制字(\tc、\tcf 和\tcl.)生成目录。 http
如何在 rtf 中的单元格内制作表格?这是我所知道的最简单的表格: {\rtf\ansi\deff0 \trowd \intbl \cellx1000 Item 1\cell \cellx2000 I
我正在研究 RTF 解析器,但在处理 unicode 时遇到了一些困难。 RTF 规范规定“大于 32767 的 Unicode 值必须表示为负数”( http://www.biblioscape.c
我想更改 RTF 文档的内容,而不是将其另存为 RTF 文档: $defaultRtfFile>> "C:\Users\user\Desktop\Outlokk-Signature\Test.rtf"
我需要一个简单的 HTML2RTF 转换器我尝试运行以下代码...但我收到此代码示例的错误 代码: import java.io.ByteArrayInputStream; import java.i
我想编写一个 python 脚本,将文件编码从 cp949 转换为 utf8。该文件最初以 cp949 编码。我的脚本如下: cpstr = open('terms.rtf').read() utf
有哪些方法可以使用 Java 将 RTF 转换为包含 Windows 或 Unix 文档中表格的 PDF? 我们在这里尝试过的选项是: ITEXT - 但 rtf 文档中的表格在转换为 PDF 后无法
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
似乎不可能为 RTF 项目符号列表找到一个非常简单的工作示例,如下所示: 您好 世界 嵌套(但请以正方形作为项目符号) 也嵌套(也是方形项目符号) 再次定期 我尝试使用 Word 或 TextEdit
我有一个数据框 df 用于 RTF 格式的报告,如下所示: df <- ATRSLBL POPUL CENTRE BAGE BAGEC1 SEX Red PPS 3720
我正在处理由其他人在未知平台上制作的 RTF 文件,并且所有内容都被正确解释,除了一些字符,无论我在 openoffice 中打开它们的任何字符集。这是经过解释的纯文本: "Même taille q
有人知道RTF文件的linter吗? 我正在为文字处理器编写一个简单的 RTF 导出工具。我已经到了我生成的文件现在加载到 OpenOffice 并且似乎可以工作的阶段——除了我刚刚发现如果我从 Op
有人可以帮我将特殊字符转换为可以在 RTF 文件中正确表示的字符吗? 我正在获取 iPad 上存储在字符串中的文本,并使用 NSASCIIStringEncoding 将其输出为 RTF 文件。到目前
我对 RichTextBox.Rtf 属性有点困惑。我正在输入一个混合字体大小的字符串:Test 字体大小为 20,string 字体大小为 10。但是,当我查看 Rtf 时 字符串,我得到以下内容:
我正试图让一个欧元符号显示在 RTF 文档中,但我不太走运。 RTF 文档是一个模板,其中填充了来自 .Net 的数据(通过读取文本流并替换其中的标记)。这些标记之一与欧元符号有关。我的代码按预期工作
我是一名优秀的程序员,十分优秀!