Click here to Skip to main content
15,885,216 members
Articles / Desktop Programming / WPF

Writing Your Own RTF Converter

Rate me:
Please Sign up or sign in to vote.
4.95/5 (234 votes)
1 Aug 2013CPOL14 min read 2.4M   40.4K   632  
An article on how to write a custom RTF parser and converter.
// -- FILE ------------------------------------------------------------------
// name       : RtfParser.cs
// project    : RTF Framelet
// created    : Leon Poyyayil - 2008.05.20
// language   : c#
// environment: .NET 2.0
// copyright  : (c) 2004-2010 by Itenso GmbH, Switzerland
// --------------------------------------------------------------------------
using System;
using Itenso.Rtf.Model;
using Itenso.Rtf.Support;
using Itenso.Rtf.Interpreter;

namespace Itenso.Rtf.Parser
{

	// ------------------------------------------------------------------------
	public class RtfParserDump
	{
		static void Main( string[] args )
		{
			RtfSympleParser();
			RtfSympleInterpreter();
			Console.ReadKey();
		} // Main

		// ----------------------------------------------------------------------
		static void RtfSympleParser()
		{
			RtfParser parser = new RtfParser();
			RtfParserListenerStructureBuilder structureBuilder = new RtfParserListenerStructureBuilder();
			parser.AddParserListener( structureBuilder );
			parser.Parse( new RtfSource( @"{\rtf1foobar}" ) );
			RtfDumpElement( structureBuilder.StructureRoot );
		} // RtfSympleParser
  
		// ----------------------------------------------------------------------
		static void RtfDumpElement( IRtfElement rtfElement )
		{
			switch ( rtfElement.Kind )
			{
				case RtfElementKind.Group:
					Console.WriteLine( "Group: " + ((IRtfGroup)rtfElement).Destination );
					foreach ( IRtfElement rtfChildElement in ((IRtfGroup)rtfElement).Contents )
					{
						RtfDumpElement( rtfChildElement );
					}
					break;
				case RtfElementKind.Tag:
					Console.WriteLine( "Tag: " + ((IRtfTag)rtfElement).FullName );
					break;
				case RtfElementKind.Text:
					Console.WriteLine( "Text: " + ((IRtfText)rtfElement).Text );
					break;
			}
		} // RtfDumpElement

		// ----------------------------------------------------------------------
		static void RtfSympleInterpreter()
		{
			RtfInterpreterListenerFileLogger logger = new RtfInterpreterListenerFileLogger( @"c:\temp\RtfInterpreter.log" );
			IRtfDocument document = RtfInterpreterTool.BuildDoc( @"{\rtf1foobar}", logger );
			RtfWriteDocument( document );
		} // RtfSympleInterpreter

		// ----------------------------------------------------------------------
		static void RtfWriteDocument( IRtfDocument document )
		{
			Console.WriteLine( "RTF Version: " + document.RtfVersion.ToString() );

			// document info
			Console.WriteLine( "Title: " + document.DocumentInfo.Title );
			Console.WriteLine( "Subject: " + document.DocumentInfo.Subject );
			Console.WriteLine( "Author: " + document.DocumentInfo.Author );
			// ...

			// fonts
			foreach ( IRtfFont font in document.FontTable )
			{
			//	Console.WriteLine( "Font: " + font.Name );
			}

			// colors
			foreach ( IRtfColor color in document.ColorTable )
			{
			//	Console.WriteLine( "Color: " + color.AsDrawingColor.ToString() );
			}

			// user properties
			foreach ( IRtfDocumentProperty documentProperty in document.UserProperties )
			{
				Console.WriteLine( "User property: " + documentProperty.Name );
			}

			// visuals
			foreach ( IRtfVisual visual in document.VisualContent )
			{
				switch ( visual.Kind )
				{
					case RtfVisualKind.Text:
						Console.WriteLine( "Text: " + ( (IRtfVisualText)visual ).Text );
						break;
					case RtfVisualKind.Break:
						Console.WriteLine( "Tag: " + ( (IRtfVisualBreak)visual ).BreakKind.ToString() );
						break;
					case RtfVisualKind.Special:
						Console.WriteLine( "Text: " + ( (IRtfVisualSpecialChar)visual ).CharKind.ToString() );
						break;
					case RtfVisualKind.Image:
						IRtfVisualImage image = (IRtfVisualImage)visual;
						Console.WriteLine( "Text: " + image.Format.ToString() +
						 " " + image.Width.ToString() + "x" + image.Height.ToString() );
						break;
				}
			}
		} // RtfWriteElement

	} // class RtfParser

} // namespace Itenso.Rtf.Parser
// -- EOF -------------------------------------------------------------------

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Switzerland Switzerland
👨 Senior .NET Software Engineer

🚀 My Open Source Projects
- Time Period Library 👉 GitHub
- Payroll Engine 👉 GitHub

Feedback and contributions are welcome.



Comments and Discussions