ComponentOne PDF for .NET
Rendering RTF Text
Using ComponentOne PDF for .NET > Rendering RTF Text

Another powerful feature of PDF for .NET is the ability to render rich text format (RTF) text using the DrawStringRtf method. This is an extremely useful feature mainly because there are so many tools for creating RTF, and it's easy to create templates in RTF and have your application customize, then render the RTF strings. This makes it easy to create richly formatted documents practically without writing any code.

For example, the code below uses an RTF template created with WordPad. It scans a directory and builds an RTF string based on the template, then renders the RTF using the DrawStringRtf method.

To write code in Visual Basic

Visual Basic
Copy Code
' Get RTF templates.
Dim rtfHdr As String = "" & _
   "{\rtf1\ansi\ansicpg1252\deff0\deflang1033" & _
   "{\fonttbl{\f0\fswiss\fcharset0 " & _
   "Arial;}{\f1\froman\fprq2\fcharset0 Book Antiqua;}}" & _
   "{\colortbl ;\red0\green0\blue0;}" & _
   "\viewkind4\uc1\pard\f0\fs20\par" & _
   "\pard\tx1440\tx2880\tx4320\tx5760\cf1\b\f1\fs24 " & _
   "Directory Report created on <>\par" & _
   "\ul\par Name\tab Extension\tab Size\tab Date\tab " & _
   "Attributes\par"
Dim rtfEntry As String = "" & _
   "\cf0\ulnone\b0\f0\fs16 <>\tab " & _
   "<>\tab <>\tab <>\tab <>\par"
 ' Build RTF string.
Dim sb As New StringBuilder()
sb.Append(rtfHdr.Replace("<>", DateTime.Today.ToShortDateString()))
Dim file As String
For Each file in Directory.GetFiles(@"c:\winnt", "*.bmp")
   Dim s As String = rtfEntry
   Dim fi As FileInfo = New FileInfo(file)
   s = s.Replace("<>", Path.GetFileNameWithoutExtension(file))
   s = s.Replace("<>", fi.Extension)
   s = s.Replace("<>", String.Format("{0:#,##0}", fi.Length))
   s = s.Replace("<>", fi.LastWriteTime.ToShortDateString())
   s = s.Replace("<>", fi.Attributes.ToString())
   sb.Append(s)
Next
sb.Append("}")
 ' Render it.
Dim pdf As New C1PdfDocument()
Dim rect As RectangleF = _c1pdf.PageRectangle
rect.Inflate(-72, -72)
_c1pdf.DrawStringRtf(sb.ToString(), Font, Brushes.Black, rect)
 ' Save.
_c1pdf.Save("c:\temp\dir.pdf")

To write code in C#

C#
Copy Code
// Get RTF templates.
string rtfHdr = @"{\rtf1\ansi\ansicpg1252\deff0\deflang1033" +
       @"{\fonttbl{\f0\fswiss\fcharset0 " +
       @"Arial;}{\f1\froman\fprq2\fcharset0 Book Antiqua;}}" +
       @"{\colortbl ;\red0\green0\blue0;}" + 
       @"\viewkind4\uc1\pard\f0\fs20\par" +
       @"\pard\tx1440\tx2880\tx4320\tx5760\cf1\b\f1\fs24 " + 
       @"Directory Report created on <>\par" +
       @"\ul\par Name\tab Extension\tab Size\tab Date\tab " +
       @"Attributes\par";
string rtfEntry = @"\cf0\ulnone\b0\f0\fs16 <>\tab " +
       @"<>\tab <>\tab <>\tab <>\par";
 // Build RTF string.
StringBuilder sb = new StringBuilder();
sb.Append(rtfHdr.Replace("<>", DateTime.Today.ToShortDateString()));
foreach (string file in Directory.GetFiles(@"c:\winnt", "*.bmp"))
{
       string s = rtfEntry;
       FileInfo fi = new FileInfo(file);
       s = s.Replace("<>", Path.GetFileNameWithoutExtension(file));
       s = s.Replace("<>",  fi.Extension);
       s = s.Replace("<>", string.Format("{0:#,##0}", fi.Length));
       s = s.Replace("<>", fi.LastWriteTime.ToShortDateString());
       s = s.Replace("<>", fi.Attributes.ToString());
       sb.Append(s);
}
sb.Append("}");
 // Render it.
C1.C1Pdf.C1PdfDocument pdf = new C1.C1Pdf.C1PdfDocument();
RectangleF rect = pdf.PageRectangle;
rect.Inflate(-72, -72);
_c1pdf.DrawStringRtf(sb.ToString(), Font, Brushes.Black, rect);
 // Save.
_c1pdf.Save(@"c:\temp\dir.pdf");

The code is very simple. The only complicated part is the RTF definition, but that was copied and pasted from an RTF file created with WordPad. The RTF template string contains tags (for example, "<<NAME>>") that are then replaced with the actual contents.

When the RTF string is built, it is rendered into the PDF document using the DrawStringRtf command. The result looks like this: