编辑和呈现 RichText [英] Edit and render RichText

查看:59
本文介绍了编辑和呈现 RichText的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个应用程序(用于楼宇自动化的自定义网络管理工具)支持打印标签,您可以剪下这些标签并将其插入设备的前显示屏.在该工具的早期版本(不是我公司开发的)中,应用程序只是将字符串推送到现场技术人员可以操作的 Excel 文件中(如格式化文本).我们在新版本中没有这样做,因为很难(不可能)保持 Excel 文件同步,并避免绑定到外部应用程序(更不用说不同版本的 Excel)了.

We have an application (a custom network management tool for building automation) that supports printing labels that you can cut out and insert into the devices' front displays. In earlier versions of the tool (not developed in my company), the application just pushed the strings into an Excel file that the field technician could then manipulate (like formatting text). We didn't do this in the new version because it was hard (impossible) to keep the Excel file in sync, and to avoid a binding to an external application (let alone different versions of Excel).

我们使用 PDFSharp 来渲染标签.它有一个类似 System.Drawing 的界面,但可以输出到 System.Drawing.Graphics(屏幕/打印机)以及 PDF 文件,这是一个要求.

We're using PDFSharp for rendering the labels. It has a System.Drawing-like interface, but can output to a System.Drawing.Graphics (screen / printer) as well as to a PDF file, which is a requirement.

后来,引入了基本格式,如字体系列、样式、大小、颜色,它们将应用于一个标签(即恰好一个字符串).现在,客户希望能够将这些格式应用于字符串中的单个字符.我认为最简单的方法是支持 RichText 的一个子集.然而,这并不像我想象的那么容易.

Later, basic formatting was introduced like Font Family, Style, Size, Color which would apply to one label (i.e. to exactly one string). Now the customer wants to be able to apply these formats to single characters in a string. I think the easiest way would be to support a subset of RichText. It's not as easy as I thought though.

目前编辑器只为您要编辑的标签显示一个文本框,字体设置为标签的字体.我以为我只是用 RichTextBox 替换它,然后更新格式按钮以使用 RichTextBox 格式属性.相当容易.但是,我需要绘制文本.我知道你可以让 RichTextBox 绘制到 HDC 或 System.Drawing.Graphics - 但如前所述,我需要它来使用 PDFSharp.渲染到位图不是一种选择,因为 PDF 不能很大,而且有很多标签.不幸的是,我无法让 RichTextBox 告诉我文本的布局 - 只要我知道在哪里绘制什么,我就可以手动进行实际渲染.这是第一个问题:如何从 RichTextBox 中获取格式正确的富文本指标?或者有什么方法可以将富文本转换为可以轻松手动绘制的矢量图形格式?

Currently the editor just displays a TextBox for the label you want to edit, with the font set to the label's font. I thought I'd just replace it with RichTextBox, and update the formatting buttons to use the RichTextBox formatting properties. Fairly easy. However, I need to draw the text. I know you can get the RichTextBox to draw to a HDC or System.Drawing.Graphics - but as already said, I need it to use PDFSharp. Rendering to bitmaps is not an option, since the PDF must not be huge, and it's a lot of labels. Unfortunately I couldn't get the RichTextBox to tell me the layout of the text - I'm fine with doing the actual rendering by hand, as long as I know where to draw what. This is the first question: How can I get the properly layouted metrics of the rich text out of a RichTextBox? Or is there any way to convert the rich text to a vector graphics format that can be easily drawn manually?

我知道 NRTFTree 可用于解析和操作 RichText.文档很糟糕(实际上我不知道,它是西班牙语),但我想我可以让它工作.据我了解,它也不会提供布局.因此,我想我必须编写一个自定义编辑控件(请记住,它基本上只是具有基本 RTF 格式的一两个行标签,而不是成熟的编辑控件 - 更像是在 PowerPoint 中编辑文本框)并编写使用 PDFSharp 而不是 System.Drawing 进行绘图的自定义文本布局逻辑.是否有任何现有的,即使部分可用的解决方案,用于编辑或手动进行布局(或两者)?

I know about NRTFTree which can be used to parse and manipulate RichText. The documentation is bad (actually I don't know, it's Spanish), but I think I can get it to work. As far as I understood, it won't provide layouting as well. Because of this, I think I'll have to write a custom edit control (remember, it's basically just one or two line labels with basic RTF formatting, not a full-fledged edit control - more like editing a textbox in PowerPoint) and write custom text layout logic that used PDFSharp rather than System.Drawing for drawing. Is there any existing, even if partial, solution available, either for the editing or for doing the layout manually (or both)?

或者是否有我没有看到的完全不同的方法?如果将标签文本以 RTF 格式导出到 CSV 文件中,然后在 Excel 中导入保留格式,则加分.

Or is there an entirely different approach I'm just not seeing? Bonus points if exporting the label texts as RTF into a CSV file, and then importing in Excel retains the formatting.

对于编辑部分,我需要它在 Windows 窗体中工作.除此之外,我认为它与 Windows 窗体无关.

For the editing part, I need it to work in Windows Forms. Other than that it's not Windows-Forms-related, I think.

推荐答案

或者有没有我没有看到的完全不同的方法?

Or is there an entirely different approach I'm just not seeing?

要从剪贴板导入 RTF,我们使用隐藏的 RichEntryField,将整个文本粘贴到其中,选择第一个字符并查询我们支持的格式(字体、大小、粗体、斜体、下划线等),选择第二个字符并查询格式...

To import RTF from the clipboard, we use a hidden RichEntryField, paste the whole text into it, select the first character and query the formats we support (Font, size, bold, italic, underline, ...), select the second character and query the formats ...

这不是您正在寻找的简单解决方案,但您不必自己解析 RTF.

It's not the easy solution you are looking for, but you don't have to parse RTF yourself.

这篇关于编辑和呈现 RichText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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