转换TXT到rtf [英] converting txt to rtf

查看:193
本文介绍了转换TXT到rtf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆的,我想转换为RTF文本文件。只是改变代码扩展不工作,底层文件是一样的。我需要的文本是RTF格式。任何人都知道我能做到这一点?

I have a bunch of text files that I want to convert to rtf. Just changing the extension in code doesn't work, the underlying file is the same. I need the text to be in rtf format. Anyone know how I can do this?

的问题是,当我加载一个纯文本文件,以便它加载它作为一个连续RichTextBox的不格式化新线文本块,而不是插入新行。

The issue is when I load up a plain text file the RichTextBox isn't formatting the new lines so it loads it as one continuous block of text as opposed to inserting the new lines.

唯一的解决方案已经打开纯文本文件和另存为的RTF。

The only solution has been to open the plain text file and "Save As" an rtf.

推荐答案

只需添加文本空RTF模板,纯文本没有任何格式化,无论如何,让我们说,RTF模板看起来像这样(维基百科为例):

Just add text into empty RTF template, plain text doesn't have any formating anyway, so let's say that rtf template looks like this (from wikipedia example):

{\rtf1\ansi{\fonttbl\f0\fswiss Helvetica;}\f0\pard _TEXT_CONTENT_HERE_ }

更新:我忘了关于新的生产线,括号和反斜杠:)

Update: I forgot about new lines, braces and backslashes :)

public static string PlainTextToRtf(string plainText)
{
  string escapedPlainText = plainText.Replace(@"\", @"\\").Replace("{", @"\{").Replace("}", @"\}");
  string rtf = @"{\rtf1\ansi{\fonttbl\f0\fswiss Helvetica;}\f0\pard ";
  rtf += escapedPlainText.Replace(Environment.NewLine, @" \par ");
  rtf += " }";
  return rtf;
}

这篇关于转换TXT到rtf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆