将 RTF 文本从数据库加载到 TRichEdit

发布于 2024-09-30 09:01:19 字数 556 浏览 9 评论 0原文

我目前正在将我们的软件解决方案从 Delphi 7 迁移到 2010。大部分更改都很简单,只剩下少量障碍。

在表单上,​​我们使用 TRichEdit,它显示从 MSSQL 数据库中的 blob 字段抓取的 rtf 文本。这就是它在 Delphi 7 中的工作方式:

//Get RTF text from Blob field using TADOQuery
rtfStream := sql.CreateBlobStream(sql.FieldByName('rtftext'), BmRead) as TMemoryStream;

//Load into TRichEdit
RichEdit.PlainText := False;
RichEdit.Lines.LoadFromStream(rtfStream);

这将按照 TRichEdit 组件中的预期显示 RTF,但 Delphi 2010 中的相同代码将 RTF 显示为纯文本,每个字符之间带有制表符。我认为这与从 Ansi 到 Unicode 的更改有很大关系,但我没有运气纠正这个问题。

任何帮助使其发挥作用将不胜感激。谢谢

I am currently in the process of migrating our software solution from Delphi 7 to 2010. Mostly the changes have been simple and there are only a small amount of hurdles left to go.

On a form we use a TRichEdit which displayed rtf text grabbed from a blob field in a MSSQL db. This is how it worked in Delphi 7:

//Get RTF text from Blob field using TADOQuery
rtfStream := sql.CreateBlobStream(sql.FieldByName('rtftext'), BmRead) as TMemoryStream;

//Load into TRichEdit
RichEdit.PlainText := False;
RichEdit.Lines.LoadFromStream(rtfStream);

This would display the RTF as expected in the TRichEdit component, but the same code in Delphi 2010 displays the RTF as plain text with tabs between each character. I assume this has a lot to do with the change from Ansi to Unicode, but I haven't had any luck rectifying the problem.

Any help getting this to work would be much appreciated. Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

━╋う一瞬間旳綻放 2024-10-07 09:01:19

好吧,我想通了。

用于加载 rtf 文本:

//Get the data from the database as AnsiString
rtfString := sql.FieldByName('rtftext').AsAnsiString;

//Write the string into a stream
stream := TMemoryStream.Create;
stream.Clear;
stream.Write(PAnsiChar(rtfString)^, Length(rtfString));
stream.Position := 0;

//Load the stream into the RichEdit
RichEdit.PlainText := False;
RichEdit.Lines.LoadFromStream(stream);

stream.Free;

用于保存 rtf 文本:

//Save to stream
stream := TMemoryStream.Create;
stream.Clear;

RichEdit.Lines.SaveToStream(stream);
stream.Position := 0;

//Read from the stream into an AnsiString (rtfString)
if (stream.Size > 0) then begin
    SetLength(rtfString, stream.Size);
    if (stream.Read(rtfString[1], stream.Size) <= 0) then
        raise EStreamError.CreateFmt('End of stream reached with %d bytes left to read.', [stream.Size]);
end;

stream.Free;

//Save to database
sql.FieldByName('rtftext').AsAnsiString := rtfString;

这花了我太长时间才弄清楚:)我想我已经学到了一件事......如果有的话Delphi 2010 中出错,通常与 unicode 有关;)

Okay I figured it out.

For loading the rtf text:

//Get the data from the database as AnsiString
rtfString := sql.FieldByName('rtftext').AsAnsiString;

//Write the string into a stream
stream := TMemoryStream.Create;
stream.Clear;
stream.Write(PAnsiChar(rtfString)^, Length(rtfString));
stream.Position := 0;

//Load the stream into the RichEdit
RichEdit.PlainText := False;
RichEdit.Lines.LoadFromStream(stream);

stream.Free;

For saving the rtf text:

//Save to stream
stream := TMemoryStream.Create;
stream.Clear;

RichEdit.Lines.SaveToStream(stream);
stream.Position := 0;

//Read from the stream into an AnsiString (rtfString)
if (stream.Size > 0) then begin
    SetLength(rtfString, stream.Size);
    if (stream.Read(rtfString[1], stream.Size) <= 0) then
        raise EStreamError.CreateFmt('End of stream reached with %d bytes left to read.', [stream.Size]);
end;

stream.Free;

//Save to database
sql.FieldByName('rtftext').AsAnsiString := rtfString;

This took me way too long to figure out :) I guess I have learned one thing though... if something goes wrong in Delphi 2010, its usually related to unicode ;)

英雄似剑 2024-10-07 09:01:19

当 PlainText 为 False 时,LoadFromStream() 首先尝试加载 RTF 代码,如果失败,则 LoadFromStream() 尝试再次以纯文本形式加载流。所有 Delphi 版本都是如此。随着 Unicode 的引入,我认为 LoadFromStream() 的 EM_STREAMIN 回调处理程序中可能会出现某些问题。我建议您使用调试器进入 LoadFromStream() 的实际源代码,看看到底发生了什么。

When PlainText is False, LoadFromStream() first attempts to load the RTF code, and if that fails then LoadFromStream() attempts to load the stream again as plain text. That has always been the case in all Delphi versions. With the introduction of Unicode, I suppose something could have broken in LoadFromStream()'s EM_STREAMIN callback handler. I suggest you step into LoadFromStream()'s actual source code with the debugger and see what is really happening.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文