Skip to main content Skip to footer

How To: View Text or RichText Files in C1 PreviewControl

Many of our customers have asked quite a few times if there is any control that can be used to load and preview 'Text files'(.txt, .rtf). As of now, there is no control like this offered by C1. However, we do have a workaround to achieve this using C1Report's PreviewControl, and this blog deals with the same. In order to preview the .txt/.rtf file, one must follow the undermentioned steps : 1. Create an instance of StreamReader 2. Read the whole text of the .txt/.rtf file using this stream reader 3. Create an instance of C1PrintDocument 4. Write this text to this C1PrintDocument using the respective RenderObjects, RenderText or RenderRichText 5. Add these RenderObjects to the C1PrintDocument. 6. Now you can preview this C1PrintDocument in C1PrintPreviewControl/ C1PreviewPane/ C1PrintPreviewDialog. Code : Preview Text File :


sr = New StreamReader("TextFile.txt")  
str = sr.ReadToEnd  
Dim rendertext As New RenderText  
rendertext.Text = str  
C1PrintDocument1.Body.Children.Add(rendertext)  
C1PrintDocument1.Generate()  
C1PrintPreviewControl1.Document = C1PrintDocument1

Preview RTF File :


sr = New StreamReader("RichTextFile.rtf")  
str = sr.ReadToEnd  
Dim rendertext As New RenderRichText  
rendertext.Rtf = str  
C1PrintDocument1.Body.Children.Add(rendertext)  
C1PrintDocument1.Generate()  
C1PrintPreviewControl1.Document = C1PrintDocument1

For complete implementation, refer to the attached sample. Download Sample_VB Download Sample_CSharp

MESCIUS inc.

comments powered by Disqus