English 中文(简体)
Edit and render RichText
原标题:

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).

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.

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.

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?

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)?

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.

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?

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 ...

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

问题回答

暂无回答




相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签