Click here to Skip to main content
15,881,803 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       : RtfTextConverter.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.Text;
using System.Globalization;
using Itenso.Rtf.Interpreter;

namespace Itenso.Rtf.Converter.Text
{

	// ------------------------------------------------------------------------
	public class RtfTextConverter : RtfInterpreterListenerBase
	{

		// ----------------------------------------------------------------------
		public const string DefaultTextFileExtension = ".txt";

		// ----------------------------------------------------------------------
		public RtfTextConverter()
			: this( new RtfTextConvertSettings() )
		{
		} // RtfTextConverter

		// ----------------------------------------------------------------------
		public RtfTextConverter( RtfTextConvertSettings settings )
		{
			if ( settings == null )
			{
				throw new ArgumentNullException( "settings" );
			}

			this.settings = settings;
		} // RtfTextConverter

		// ----------------------------------------------------------------------
		public string PlainText
		{
			get { return plainText.ToString(); }
		} // PlainText

		// ----------------------------------------------------------------------
		public RtfTextConvertSettings Settings
		{
			get { return settings; }
		} // Settings

		// ----------------------------------------------------------------------
		public void Clear()
		{
			plainText.Remove( 0, plainText.Length );
		} // Clear

		// ----------------------------------------------------------------------
		protected override void DoBeginDocument( IRtfInterpreterContext context )
		{
			Clear();
		} // DoBeginDocument

		// ----------------------------------------------------------------------
		protected override void DoInsertText( IRtfInterpreterContext context, string text )
		{
			if ( context.CurrentTextFormat == null )
			{
				return;
			}
			if ( !context.CurrentTextFormat.IsHidden || settings.IsShowHiddenText )
			{
				plainText.Append( text );
			}
		} // DoInsertText

		// ----------------------------------------------------------------------
		protected override void DoInsertSpecialChar( IRtfInterpreterContext context, RtfVisualSpecialCharKind kind )
		{
			switch ( kind )
			{
				case RtfVisualSpecialCharKind.Tabulator:
					plainText.Append( settings.TabulatorText );
					break;
				case RtfVisualSpecialCharKind.NonBreakingSpace:
					plainText.Append( settings.NonBreakingSpaceText );
					break;
				case RtfVisualSpecialCharKind.EmSpace:
					plainText.Append( settings.EmSpaceText );
					break;
				case RtfVisualSpecialCharKind.EnSpace:
					plainText.Append( settings.EnSpaceText );
					break;
				case RtfVisualSpecialCharKind.QmSpace:
					plainText.Append( settings.QmSpaceText );
					break;
				case RtfVisualSpecialCharKind.EmDash:
					plainText.Append( settings.EmDashText );
					break;
				case RtfVisualSpecialCharKind.EnDash:
					plainText.Append( settings.EnDashText );
					break;
				case RtfVisualSpecialCharKind.OptionalHyphen:
					plainText.Append( settings.OptionalHyphenText );
					break;
				case RtfVisualSpecialCharKind.NonBreakingHyphen:
					plainText.Append( settings.NonBreakingHyphenText );
					break;
				case RtfVisualSpecialCharKind.Bullet:
					plainText.Append( settings.BulletText );
					break;
				case RtfVisualSpecialCharKind.LeftSingleQuote:
					plainText.Append( settings.LeftSingleQuoteText );
					break;
				case RtfVisualSpecialCharKind.RightSingleQuote:
					plainText.Append( settings.RightSingleQuoteText );
					break;
				case RtfVisualSpecialCharKind.LeftDoubleQuote:
					plainText.Append( settings.LeftDoubleQuoteText );
					break;
				case RtfVisualSpecialCharKind.RightDoubleQuote:
					plainText.Append( settings.RightDoubleQuoteText );
					break;
				default:
					plainText.Append( settings.UnknownSpecialCharText );
					break;
			}
		} // DoInsertSpecialChar

		// ----------------------------------------------------------------------
		protected override void DoInsertBreak( IRtfInterpreterContext context, RtfVisualBreakKind kind )
		{
			switch ( kind )
			{
				case RtfVisualBreakKind.Line:
					plainText.Append( settings.LineBreakText );
					break;
				case RtfVisualBreakKind.Page:
					plainText.Append( settings.PageBreakText );
					break;
				case RtfVisualBreakKind.Paragraph:
					plainText.Append( settings.ParagraphBreakText );
					break;
				case RtfVisualBreakKind.Section:
					plainText.Append( settings.SectionBreakText );
					break;
				default:
					plainText.Append( settings.UnknownBreakText );
					break;
			}
		} // DoInsertBreak

		// ----------------------------------------------------------------------
		protected override void DoInsertImage( IRtfInterpreterContext context,
			RtfVisualImageFormat format,
			int width, int height, int desiredWidth, int desiredHeight,
			int scaleWidthPercent, int scaleHeightPercent,
			string imageDataHex
		)
		{
			string imageFormatText = settings.ImageFormatText;
			if ( string.IsNullOrEmpty( imageFormatText ) )
			{
				return;
			}

			string imageText = string.Format(
				CultureInfo.InvariantCulture,
				imageFormatText,
				format,
				width,
				height,
				desiredWidth,
				desiredHeight,
				scaleWidthPercent,
				scaleHeightPercent,
				imageDataHex );

			plainText.Append( imageText );
		} // DoInsertImage

		// ----------------------------------------------------------------------
		// members
		private readonly StringBuilder plainText = new StringBuilder();
		private readonly RtfTextConvertSettings settings;

	} // class RtfTextConverter

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