Click here to Skip to main content
15,886,833 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       : RtfInterpreterListenerDocumentBuilder.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.Text;
using Itenso.Rtf.Model;

namespace Itenso.Rtf.Interpreter
{

	// ------------------------------------------------------------------------
	public sealed class RtfInterpreterListenerDocumentBuilder : RtfInterpreterListenerBase
	{

		// ----------------------------------------------------------------------
		public bool CombineTextWithSameFormat
		{
			get { return combineTextWithSameFormat; }
			set { combineTextWithSameFormat = value; }
		} // CombineTextWithSameFormat

		// ----------------------------------------------------------------------
		public IRtfDocument Document
		{
			get { return document; }
		} // Document

		// ----------------------------------------------------------------------
		protected override void DoBeginDocument( IRtfInterpreterContext context )
		{
			document = null;
			visualDocumentContent = new RtfVisualCollection();
		} // DoBeginDocument

		// ----------------------------------------------------------------------
		protected override void DoInsertText( IRtfInterpreterContext context, string text )
		{
			if ( combineTextWithSameFormat )
			{
				IRtfTextFormat newFormat = context.GetSafeCurrentTextFormat();
				if ( !newFormat.Equals( pendingTextFormat ) )
				{
					FlushPendingText();
				}
				pendingTextFormat = newFormat;
				pendingText.Append( text );
			}
			else
			{
				AppendAlignedVisual( new RtfVisualText( text, context.GetSafeCurrentTextFormat() ) );
			}
		} // DoInsertText

		// ----------------------------------------------------------------------
		protected override void DoInsertSpecialChar( IRtfInterpreterContext context, RtfVisualSpecialCharKind kind )
		{
			FlushPendingText();
			visualDocumentContent.Add( new RtfVisualSpecialChar( kind ) );
		} // DoInsertSpecialChar

		// ----------------------------------------------------------------------
		protected override void DoInsertBreak( IRtfInterpreterContext context, RtfVisualBreakKind kind )
		{
			FlushPendingText();
			visualDocumentContent.Add( new RtfVisualBreak( kind ) );
			switch ( kind )
			{
				case RtfVisualBreakKind.Paragraph:
				case RtfVisualBreakKind.Section:
					EndParagraph( context );
					break;
			}
		} // DoInsertBreak

		// ----------------------------------------------------------------------
		protected override void DoInsertImage( IRtfInterpreterContext context,
			RtfVisualImageFormat format,
			int width, int height, int desiredWidth, int desiredHeight,
			int scaleWidthPercent, int scaleHeightPercent,
			string imageDataHex
		)
		{
			FlushPendingText();
			AppendAlignedVisual( new RtfVisualImage( format,
				context.GetSafeCurrentTextFormat().Alignment,
				width, height, desiredWidth, desiredHeight,
				scaleWidthPercent, scaleHeightPercent, imageDataHex ) );
		} // DoInsertImage

		// ----------------------------------------------------------------------
		protected override void DoEndDocument( IRtfInterpreterContext context )
		{
			FlushPendingText();
			EndParagraph( context );
			document = new RtfDocument( context, visualDocumentContent );
			visualDocumentContent = null;
			visualDocumentContent = null;
		} // DoEndDocument

		// ----------------------------------------------------------------------
		private void EndParagraph( IRtfInterpreterContext context )
		{
			RtfTextAlignment finalParagraphAlignment = context.GetSafeCurrentTextFormat().Alignment;
			foreach ( IRtfVisual alignedVisual in pendingParagraphContent )
			{
				switch ( alignedVisual.Kind )
				{
					case RtfVisualKind.Image:
						RtfVisualImage image = (RtfVisualImage)alignedVisual;
						// ReSharper disable RedundantCheckBeforeAssignment
						if ( image.Alignment != finalParagraphAlignment )
						// ReSharper restore RedundantCheckBeforeAssignment
						{
							image.Alignment = finalParagraphAlignment;
						}
						break;
					case RtfVisualKind.Text:
						RtfVisualText text = (RtfVisualText)alignedVisual;
						if ( text.Format.Alignment != finalParagraphAlignment )
						{
							IRtfTextFormat correctedFormat = ( (RtfTextFormat)text.Format ).DeriveWithAlignment( finalParagraphAlignment );
							IRtfTextFormat correctedUniqueFormat = context.GetUniqueTextFormatInstance( correctedFormat );
							text.Format = correctedUniqueFormat;
						}
						break;
				}
			}
			pendingParagraphContent.Clear();
		} // EndParagraph

		// ----------------------------------------------------------------------
		private void FlushPendingText()
		{
			if ( pendingTextFormat != null )
			{
				AppendAlignedVisual( new RtfVisualText( pendingText.ToString(), pendingTextFormat ) );
				pendingTextFormat = null;
				pendingText.Remove( 0, pendingText.Length );
			}
		} // FlushPendingText

		// ----------------------------------------------------------------------
		private void AppendAlignedVisual( RtfVisual visual )
		{
			visualDocumentContent.Add( visual );
			pendingParagraphContent.Add( visual );
		} // AppendAlignedVisual

		// ----------------------------------------------------------------------
		// members
		private bool combineTextWithSameFormat = true;

		private RtfDocument document;
		private RtfVisualCollection visualDocumentContent;
		private readonly RtfVisualCollection pendingParagraphContent = new RtfVisualCollection();

		private IRtfTextFormat pendingTextFormat;
		private readonly StringBuilder pendingText = new StringBuilder();

	} // class RtfInterpreterListenerDocumentBuilder

} // 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