Click here to Skip to main content
15,891,184 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       : RtfConverterDemo.cs
// project    : RTF Framelet
// created    : Jani Giannoudis - 2013.02.16
// language   : c#
// environment: .NET 2.0
// copyright  : (c) 2004-2013 by Jani Giannoudis, Switzerland
// --------------------------------------------------------------------------
using System.Drawing.Imaging;
using System.IO;
using Itenso.Rtf;
using Itenso.Rtf.Converter.Html;
using Itenso.Rtf.Converter.Image;
using Itenso.Rtf.Converter.Text;
using Itenso.Rtf.Interpreter;
using Itenso.Rtf.Parser;
using Itenso.Rtf.Support;

namespace Itenso.Solutions.Community.Rtf2Html
{

	// ------------------------------------------------------------------------
	public class RtfConverterDemo
	{

		// ----------------------------------------------------------------------
		public string ParserLogFileName { get; set; }

		// ----------------------------------------------------------------------
		public string InterpreterLogFileName { get; set; }

		// ----------------------------------------------------------------------
		public string ConvertRtf2Txt( string fileName )
		{
			// parser
			IRtfGroup rtfStructure = ParseRtf( fileName );
			if ( rtfStructure == null )
			{
				return string.Empty;
			}

			// interpreter logger
			RtfInterpreterListenerFileLogger interpreterLogger = null;
			if ( !string.IsNullOrEmpty( InterpreterLogFileName ) )
			{
				interpreterLogger = new RtfInterpreterListenerFileLogger( InterpreterLogFileName );
			}

			// text converter
			RtfTextConvertSettings textConvertSettings = new RtfTextConvertSettings();
			textConvertSettings.BulletText = "-";
			RtfTextConverter textConverter = new RtfTextConverter( textConvertSettings );

			// text interpreter
			RtfInterpreterTool.Interpret( rtfStructure, interpreterLogger, textConverter );
			return textConverter.PlainText;
		} // ConvertRtf2Txt

		// ----------------------------------------------------------------------
		public string ConvertRtf2Html( string fileName )
		{
			// parser
			IRtfGroup rtfStructure = ParseRtf( fileName );
			if ( rtfStructure == null )
			{
				return string.Empty;
			}

			// interpreter logger
			RtfInterpreterListenerFileLogger interpreterLogger = null;
			if ( !string.IsNullOrEmpty( InterpreterLogFileName ) )
			{
				interpreterLogger = new RtfInterpreterListenerFileLogger( InterpreterLogFileName );
			}

			// image converter
			RtfVisualImageAdapter imageAdapter = new RtfVisualImageAdapter( ImageFormat.Jpeg );
			RtfImageConvertSettings imageConvertSettings = new RtfImageConvertSettings( imageAdapter );
			imageConvertSettings.ScaleImage = true; // scale images
			RtfImageConverter imageConverter = new RtfImageConverter( imageConvertSettings );

			// rtf interpreter
			RtfInterpreterSettings interpreterSettings = new RtfInterpreterSettings();
			IRtfDocument rtfDocument = RtfInterpreterTool.BuildDoc( rtfStructure, interpreterSettings, interpreterLogger, imageConverter );

			// html converter
			RtfHtmlConvertSettings htmlConvertSettings = new RtfHtmlConvertSettings();
			htmlConvertSettings.ConvertScope = RtfHtmlConvertScope.Content;
			RtfHtmlConverter htmlConverter = new RtfHtmlConverter( rtfDocument );
			return htmlConverter.Convert();
		} // ConvertRtf2Html

		// ----------------------------------------------------------------------
		private IRtfGroup ParseRtf( string fileName )
		{
			IRtfGroup rtfStructure;

			using ( FileStream stream = File.Open( fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite ) )
			{
				RtfParserListenerStructureBuilder structureBuilder = new RtfParserListenerStructureBuilder();
				RtfParser parser = new RtfParser( structureBuilder );
				parser.IgnoreContentAfterRootGroup = true; // support WordPad documents
				if ( !string.IsNullOrEmpty( ParserLogFileName ) )
				{
					parser.AddParserListener( new RtfParserListenerFileLogger( ParserLogFileName ) );
				}
				parser.Parse( new RtfSource( stream ) );
				rtfStructure = structureBuilder.StructureRoot;
			}
			return rtfStructure;
		} // ParseRtf

	} // class RtfConverterDemo

} // namespace Itenso.Solutions.Community.Rtf2Html
// -- 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