В первом приближении:
Код
program Txt2Rtf;
var
NeedSpace: Boolean;
procedure WriteRTFHeader(var f: Text);
begin
Write(f,
'{\rtf\ansi\ansicp1251\deff0',
'{\fonttbl{\f0\fmodern\charset204 Courier New;}}'
);
end;
procedure WriteRTFFooter(var f: Text);
begin
Write(f, '}');
end;
function WriteRTF(var f: Text; c: Char): String;
procedure WriteKeyword(const Keyword: String);
begin
Write(f, '\', Keyword);
NeedSpace := True;
end;
const
Hex: array[0..15] of Char = '0123456789abcdef';
begin
case c of
#10:
begin
WriteKeyword('line');
Exit;
end;
#9:
begin
WriteKeyword('tab');
Exit;
end;
'\', '{', '}':
Write(f, '\', c);
else
if c > #127 then
Write(f, '\''', Hex[Ord(c) shr 4], Hex[Ord(c) and $F])
else
begin
if NeedSpace then Write(f, ' ');
if c < ' ' then
Write(f, '?')
else
Write(f, c);
end;
end;
NeedSpace := False;
end;
var
fi, fo: Text;
c: Char;
begin
if ParamCount <> 2 then
begin
Writeln('Usage: txt2rtf <input.txt> <output.rtf>');
Halt(1);
end;
Assign(fi, ParamStr(1));
Reset(fi);
Assign(fo, ParamStr(2));
Rewrite(fo);
NeedSpace := False;
WriteRTFHeader(fo);
while not Eof(fi) do
begin
Read(fi, c);
if c <> #13 then WriteRTF(fo, c);
end;
WriteRTFFooter(fo);
Close(fo);
Close(fi);
end.
Рекомендую почитать спецификацию RTF ("выдрана" с
http://www.microsoft.com)