Click here to Skip to main content
15,881,882 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       : RtfInterpreterTool.cs
// project    : RTF Framelet
// created    : Leon Poyyayil - 2008.05.21
// language   : c#
// environment: .NET 2.0
// copyright  : (c) 2004-2013 by Jani Giannoudis, Switzerland
// --------------------------------------------------------------------------
using System.IO;
using Itenso.Rtf.Interpreter;

namespace Itenso.Rtf.Support
{

	// ------------------------------------------------------------------------
	public static class RtfInterpreterTool
	{

		// ----------------------------------------------------------------------
		public static IRtfDocument BuildDoc( string rtfText, params IRtfInterpreterListener[] listeners )
		{
			return BuildDoc( rtfText, new RtfInterpreterSettings(), listeners );
		} // BuildDoc

		// ----------------------------------------------------------------------
		public static IRtfDocument BuildDoc( string rtfText, IRtfInterpreterSettings settings,
			params IRtfInterpreterListener[] listeners )
		{
			return BuildDoc( RtfParserTool.Parse( rtfText ), settings, listeners );
		} // BuildDoc

		// ----------------------------------------------------------------------
		public static IRtfDocument BuildDoc( TextReader rtfTextSource, params IRtfInterpreterListener[] listeners )
		{
			return BuildDoc( rtfTextSource, new RtfInterpreterSettings(), listeners );
		} // BuildDoc

		// ----------------------------------------------------------------------
		public static IRtfDocument BuildDoc( TextReader rtfTextSource, IRtfInterpreterSettings settings,
			params IRtfInterpreterListener[] listeners )
		{
			return BuildDoc( RtfParserTool.Parse( rtfTextSource ), settings, listeners );
		} // BuildDoc

		// ----------------------------------------------------------------------
		public static IRtfDocument BuildDoc( Stream rtfTextSource, params IRtfInterpreterListener[] listeners )
		{
			return BuildDoc( rtfTextSource, new RtfInterpreterSettings(), listeners );
		} // BuildDoc

		// ----------------------------------------------------------------------
		public static IRtfDocument BuildDoc( Stream rtfTextSource, IRtfInterpreterSettings settings,
			params IRtfInterpreterListener[] listeners )
		{
			return BuildDoc( RtfParserTool.Parse( rtfTextSource ), settings, listeners );
		} // BuildDoc

		// ----------------------------------------------------------------------
		public static IRtfDocument BuildDoc( IRtfSource rtfTextSource, params IRtfInterpreterListener[] listeners )
		{
			return BuildDoc( rtfTextSource, new RtfInterpreterSettings(), listeners );
		} // BuildDoc

		// ----------------------------------------------------------------------
		public static IRtfDocument BuildDoc( IRtfSource rtfTextSource, IRtfInterpreterSettings settings,
			params IRtfInterpreterListener[] listeners )
		{
			return BuildDoc( RtfParserTool.Parse( rtfTextSource ), settings, listeners );
		} // BuildDoc

		// ----------------------------------------------------------------------
		public static IRtfDocument BuildDoc( IRtfGroup rtfDocument, params IRtfInterpreterListener[] listeners )
		{
			return BuildDoc( rtfDocument, new RtfInterpreterSettings(), listeners );
		} // BuildDoc

		// ----------------------------------------------------------------------
		public static IRtfDocument BuildDoc( IRtfGroup rtfDocument, IRtfInterpreterSettings settings,
			params IRtfInterpreterListener[] listeners )
		{
			RtfInterpreterListenerDocumentBuilder docBuilder = new RtfInterpreterListenerDocumentBuilder();
			IRtfInterpreterListener[] allListeners;
			if ( listeners == null )
			{
				allListeners = new IRtfInterpreterListener[] { docBuilder };
			}
			else
			{
				allListeners = new IRtfInterpreterListener[ listeners.Length + 1 ];
				allListeners[ 0 ] = docBuilder;
				listeners.CopyTo( allListeners, 1 );
			}
			Interpret( rtfDocument, settings, allListeners );
			return docBuilder.Document;
		} // BuildDoc

		// ----------------------------------------------------------------------
		public static void Interpret( string rtfText, params IRtfInterpreterListener[] listeners )
		{
			Interpret( rtfText, new RtfInterpreterSettings(), listeners );
		} // Interpret

		// ----------------------------------------------------------------------
		public static void Interpret( string rtfText, IRtfInterpreterSettings settings,
			params IRtfInterpreterListener[] listeners )
		{
			Interpret( RtfParserTool.Parse( rtfText ), settings, listeners );
		} // Interpret

		// ----------------------------------------------------------------------
		public static void Interpret( TextReader rtfTextSource, params IRtfInterpreterListener[] listeners )
		{
			Interpret( rtfTextSource, new RtfInterpreterSettings(), listeners );
		} // Interpret

		// ----------------------------------------------------------------------
		public static void Interpret( TextReader rtfTextSource, IRtfInterpreterSettings settings,
			params IRtfInterpreterListener[] listeners )
		{
			Interpret( RtfParserTool.Parse( rtfTextSource ), settings, listeners );
		} // Interpret

		// ----------------------------------------------------------------------
		public static void Interpret( Stream rtfTextSource, params IRtfInterpreterListener[] listeners )
		{
			Interpret( rtfTextSource, new RtfInterpreterSettings(), listeners );
		} // Interpret

		// ----------------------------------------------------------------------
		public static void Interpret( Stream rtfTextSource, IRtfInterpreterSettings settings,
			params IRtfInterpreterListener[] listeners )
		{
			Interpret( RtfParserTool.Parse( rtfTextSource ), settings, listeners );
		} // Interpret

		// ----------------------------------------------------------------------
		public static void Interpret( IRtfSource rtfTextSource, params IRtfInterpreterListener[] listeners )
		{
			Interpret( rtfTextSource, new RtfInterpreterSettings(), listeners );
		} // Interpret

		// ----------------------------------------------------------------------
		public static void Interpret( IRtfSource rtfTextSource, IRtfInterpreterSettings settings,
			params IRtfInterpreterListener[] listeners )
		{
			Interpret( RtfParserTool.Parse( rtfTextSource ), settings, listeners );
		} // Interpret

		// ----------------------------------------------------------------------
		public static void Interpret( IRtfGroup rtfDocument, params IRtfInterpreterListener[] listeners )
		{
			Interpret( rtfDocument, new RtfInterpreterSettings(), listeners );
		} // Interpret

		// ----------------------------------------------------------------------
		public static void Interpret( IRtfGroup rtfDocument, IRtfInterpreterSettings settings,
			params IRtfInterpreterListener[] listeners )
		{
			RtfInterpreter interpreter = new RtfInterpreter( settings );
			if ( listeners != null )
			{
				foreach ( IRtfInterpreterListener listener in listeners )
				{
					if ( listener != null )
					{
						interpreter.AddInterpreterListener( listener );
					}
				}
			}
			interpreter.Interpret( rtfDocument );
		} // Interpret

	} // class RtfInterpreterTool

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