Click here to Skip to main content
15,895,746 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       : RtfDocument.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;

namespace Itenso.Rtf.Model
{

	// ------------------------------------------------------------------------
	public sealed class RtfDocument : IRtfDocument
	{

		// ----------------------------------------------------------------------
		public RtfDocument( IRtfInterpreterContext context, IRtfVisualCollection visualContent ) :
			this( context.RtfVersion,
				context.DefaultFont,
				context.FontTable,
				context.ColorTable,
				context.Generator,
				context.UniqueTextFormats,
				context.DocumentInfo,
				context.UserProperties,
				visualContent
			)
		{
		} // RtfDocument

		// ----------------------------------------------------------------------
		public RtfDocument(
			int rtfVersion,
			IRtfFont defaultFont,
			IRtfFontCollection fontTable,
			IRtfColorCollection colorTable,
			string generator,
			IRtfTextFormatCollection uniqueTextFormats,
			IRtfDocumentInfo documentInfo,
			IRtfDocumentPropertyCollection userProperties,
			IRtfVisualCollection visualContent
		)
		{
			if ( rtfVersion != RtfSpec.RtfVersion1 )
			{
				throw new RtfUnsupportedStructureException( Strings.UnsupportedRtfVersion( rtfVersion ) );
			}
			if ( defaultFont == null )
			{
				throw new ArgumentNullException( "defaultFont" );
			}
			if ( fontTable == null )
			{
				throw new ArgumentNullException( "fontTable" );
			}
			if ( colorTable == null )
			{
				throw new ArgumentNullException( "colorTable" );
			}
			if ( uniqueTextFormats == null )
			{
				throw new ArgumentNullException( "uniqueTextFormats" );
			}
			if ( documentInfo == null )
			{
				throw new ArgumentNullException( "documentInfo" );
			}
			if ( userProperties == null )
			{
				throw new ArgumentNullException( "userProperties" );
			}
			if ( visualContent == null )
			{
				throw new ArgumentNullException( "visualContent" );
			}
			this.rtfVersion = rtfVersion;
			this.defaultFont = defaultFont;
			defaultTextFormat = new RtfTextFormat( defaultFont, RtfSpec.DefaultFontSize );
			this.fontTable = fontTable;
			this.colorTable = colorTable;
			this.generator = generator;
			this.uniqueTextFormats = uniqueTextFormats;
			this.documentInfo = documentInfo;
			this.userProperties = userProperties;
			this.visualContent = visualContent;
		} // RtfDocument

		// ----------------------------------------------------------------------
		public int RtfVersion
		{
			get { return rtfVersion; }
		} // RtfVersion

		// ----------------------------------------------------------------------
		public IRtfFont DefaultFont
		{
			get { return defaultFont; }
		} // DefaultFont

		// ----------------------------------------------------------------------
		public IRtfTextFormat DefaultTextFormat
		{
			get { return defaultTextFormat; }
		} // DefaultTextFormat

		// ----------------------------------------------------------------------
		public IRtfFontCollection FontTable
		{
			get { return fontTable; }
		} // FontTable

		// ----------------------------------------------------------------------
		public IRtfColorCollection ColorTable
		{
			get { return colorTable; }
		} // ColorTable

		// ----------------------------------------------------------------------
		public string Generator
		{
			get { return generator; }
		} // Generator

		// ----------------------------------------------------------------------
		public IRtfTextFormatCollection UniqueTextFormats
		{
			get { return uniqueTextFormats; }
		} // UniqueTextFormats

		// ----------------------------------------------------------------------
		public IRtfDocumentInfo DocumentInfo
		{
			get { return documentInfo; }
		} // DocumentInfo

		// ----------------------------------------------------------------------
		public IRtfDocumentPropertyCollection UserProperties
		{
			get { return userProperties; }
		} // UserProperties

		// ----------------------------------------------------------------------
		public IRtfVisualCollection VisualContent
		{
			get { return visualContent; }
		} // VisualContent

		// ----------------------------------------------------------------------
		public override string ToString()
		{
			return "RTFv" + rtfVersion;
		} // ToString

		// ----------------------------------------------------------------------
		// members
		private readonly int rtfVersion;
		private readonly IRtfFont defaultFont;
		private readonly IRtfTextFormat defaultTextFormat;
		private readonly IRtfFontCollection fontTable;
		private readonly IRtfColorCollection colorTable;
		private readonly string generator;
		private readonly IRtfTextFormatCollection uniqueTextFormats;
		private readonly IRtfDocumentInfo documentInfo;
		private readonly IRtfDocumentPropertyCollection userProperties;
		private readonly IRtfVisualCollection visualContent;

	} // class RtfDocument

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