Click here to Skip to main content
15,896,423 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.5M   40.4K   632  
An article on how to write a custom RTF parser and converter.
// -- FILE ------------------------------------------------------------------
// name       : RtfInterpreterBase.cs
// project    : RTF Framelet
// created    : Leon Poyyayil - 2008.05.21
// language   : c#
// environment: .NET 2.0
// copyright  : (c) 2004-2010 by Itenso GmbH, Switzerland
// --------------------------------------------------------------------------
using System;
using System.Collections;

namespace Itenso.Rtf.Interpreter
{

	// ------------------------------------------------------------------------
	public abstract class RtfInterpreterBase : IRtfInterpreter
	{

		// ----------------------------------------------------------------------
		protected RtfInterpreterBase(
			params IRtfInterpreterListener[] listeners
		)
		{
			if ( listeners != null )
			{
				foreach ( IRtfInterpreterListener listener in listeners )
				{
					AddInterpreterListener( listener );
				}
			}
		} // RtfInterpreterBase

		// ----------------------------------------------------------------------
		public void AddInterpreterListener( IRtfInterpreterListener listener )
		{
			if ( listener == null )
			{
				throw new ArgumentNullException( "listener" );
			}
			if ( listeners == null )
			{
				listeners = new ArrayList();
			}
			if ( !listeners.Contains( listener ) )
			{
				listeners.Add( listener );
			}
		} // AddInterpreterListener

		// ----------------------------------------------------------------------
		public void RemoveInterpreterListener( IRtfInterpreterListener listener )
		{
			if ( listener == null )
			{
				throw new ArgumentNullException( "listener" );
			}
			if ( listeners != null )
			{
				if ( listeners.Contains( listener ) )
				{
					listeners.Remove( listener );
				}
				if ( listeners.Count == 0 )
				{
					listeners = null;
				}
			}
		} // RemoveInterpreterListener

		// ----------------------------------------------------------------------
		public void Interpret( IRtfGroup rtfDocument )
		{
			if ( rtfDocument == null )
			{
				throw new ArgumentNullException( "rtfDocument" );
			}
			DoInterpret( rtfDocument );
		} // Interpret

		// ----------------------------------------------------------------------
		protected abstract void DoInterpret( IRtfGroup rtfDocument );

		// ----------------------------------------------------------------------
		protected void NotifyBeginDocument()
		{
			if ( listeners != null )
			{
				foreach ( IRtfInterpreterListener listener in listeners )
				{
					listener.BeginDocument( context );
				}
			}
		} // NotifyBeginDocument

		// ----------------------------------------------------------------------
		protected void NotifyInsertText( string text )
		{
			if ( listeners != null )
			{
				foreach ( IRtfInterpreterListener listener in listeners )
				{
					listener.InsertText( context, text );
				}
			}
		} // NotifyInsertText

		// ----------------------------------------------------------------------
		protected void NotifyInsertSpecialChar( RtfVisualSpecialCharKind kind )
		{
			if ( listeners != null )
			{
				foreach ( IRtfInterpreterListener listener in listeners )
				{
					listener.InsertSpecialChar( context, kind );
				}
			}
		} // NotifyInsertSpecialChar

		// ----------------------------------------------------------------------
		protected void NotifyInsertBreak( RtfVisualBreakKind kind )
		{
			if ( listeners != null )
			{
				foreach ( IRtfInterpreterListener listener in listeners )
				{
					listener.InsertBreak( context, kind );
				}
			}
		} // NotifyInsertBreak

		// ----------------------------------------------------------------------
		protected void NotifyInsertImage( RtfVisualImageFormat format,
			int width, int height, int desiredWidth, int desiredHeight,
			int scaleWidthPercent, int scaleHeightPercent, string imageDataHex
		)
		{
			if ( listeners != null )
			{
				foreach ( IRtfInterpreterListener listener in listeners )
				{
					listener.InsertImage(
						context,
						format,
						width,
						height,
						desiredWidth,
						desiredHeight,
						scaleWidthPercent,
						scaleHeightPercent,
						imageDataHex );
				}
			}
		} // NotifyInsertImage

		// ----------------------------------------------------------------------
		protected void NotifyEndDocument()
		{
			if ( listeners != null )
			{
				foreach ( IRtfInterpreterListener listener in listeners )
				{
					listener.EndDocument( context );
				}
			}
		} // NotifyEndDocument

		// ----------------------------------------------------------------------
		protected RtfInterpreterContext Context
		{
			get { return context; }
		} // Context

		// ----------------------------------------------------------------------
		// members
		private readonly RtfInterpreterContext context = new RtfInterpreterContext();
		private ArrayList listeners;

	} // class RtfInterpreterBase

} // namespace Itenso.Rtf.Interpreter
// -- 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