[]
public int DrawStringRtf(string text, Font font, Brush brush, RectangleF rc)
Type | Name | Description |
---|---|---|
string | text | RTF string to draw. |
Font | font | Font object that defines the appearance and size of the drawn text. |
Brush | brush | Brush object that defines the color of the drawn text. |
RectangleF | rc | RectangleF structure that specifies the location of the drawn text, in points from the top left corner of the page. |
Type | Description |
---|---|
int | The index of first character that was not printed because it did not fit in the specified rectangle, or the value of
|
This method is similar to MeasureString(string, Font, float), except it recognizes Rtf (Rich Text Format) strings like those used in the RichTextBox control.
There are two types of RTF strings:
1) Complete RTF strings contain an Rtf header that specifies the fonts and colors
used within the string. These strings can be obtained from a RichTextBox control
using the Rtf property, or from Rtf files saved to disk.
In this case, the font
and brush
parameters are not used.
2) Partial RTF strings contain embedded Rtf tags but no Rtf header. These strings
are easy to build in code and can be used to render text with special attributes such as
bold and italics (for example: "this text contains {\b BOLD} and {\i ITALICS}".
In this case, the font
and brush
parameters are used
to build the Rtf header automatically.
The DrawStringRtf method returns the index of the first character that was not printed because it did not fit the output rectangle. You can use this value to make text flow from page to page, or from one frame to another within a page. To do this, use the overload that takes the starting character in the text as a parameter.
Draws an RTF string in the specified rectangle with the specified Brush and Font objects, starting at a given offset within the string.
public int DrawStringRtf(string text, Font font, Brush brush, RectangleF rc, int firstChar)
Type | Name | Description |
---|---|---|
string | text | RTF string to draw. |
Font | font | Font object that defines the appearance and size of the drawn text. |
Brush | brush | Brush object that defines the color of the drawn text. |
RectangleF | rc | RectangleF structure that specifies the location of the drawn text, in points from the top left corner of the page. |
int | firstChar | Index of the first character to draw (usually the return value of a previous call to DrawStringRtf). |
Type | Description |
---|---|
int | The index of first character that was not printed because it did not fit in the specified rectangle, or the value of
|
This method is similar to MeasureString(string, Font, float), except it recognizes Rtf (Rich Text Format) strings like those used in the RichTextBox control.
There are two types of RTF strings:
1) Complete RTF strings contain an Rtf header that specifies the fonts and colors
used within the string. These strings can be obtained from a RichTextBox control
using the Rtf property, or from Rtf files saved to disk.
In this case, the font
and brush
parameters are not used.
2) Partial RTF strings contain embedded Rtf tags but no Rtf header. These strings
are easy to build in code and can be used to render text with special attributes such as
bold and italics (for example: "this text contains {\b BOLD} and {\i ITALICS}".
In this case, the font
and brush
parameters are used
to build the Rtf header automatically.
The DrawStringRtf method returns the index of the first character that was not printed because it did not fit the output rectangle. You can use this value to make text flow from page to page, or from one frame to another within a page. Note that this value is not an index into the raw Rtf input, but into the text represented by the Rtf. See example below.
The code below renders a long string into several pages, using the return value from the DrawString(string, Font, Brush, RectangleF, StringFormat) method to determine where to continue printing.
// calculate page rectangle
RectangleF rcPage = _c1pdf.PageRectangle;
rcPage.Inflate(-72, -72);
// get Rtf to render
string text = richTextBox1.Rtf;
// print the RTF string spanning multiple pages
_c1pdf.Clear();
for (int start = 0; start < int.MaxValue; )
{
if (start > 0) _c1pdf.NewPage();
start = _c1pdf.DrawStringRtf(text, Font,
Brushes.Black, rcPage, start);
}
// show the result
string fn = @"c:\temp\test\rtf.pdf";
_c1pdf.Save(fn);
System.Diagnostics.Process.Start(fn);