Click here to Skip to main content
15,891,567 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       : RtfImageBuilder.cs
// project    : RTF Framelet
// created    : Leon Poyyayil - 2008.05.23
// language   : c#
// environment: .NET 2.0
// copyright  : (c) 2004-2010 by Itenso GmbH, Switzerland
// --------------------------------------------------------------------------
using Itenso.Rtf.Support;

namespace Itenso.Rtf.Interpreter
{

	// ------------------------------------------------------------------------
	public sealed class RtfImageBuilder : RtfElementVisitorBase
	{

		// ----------------------------------------------------------------------
		public RtfImageBuilder()
			: base( RtfElementVisitorOrder.DepthFirst )
		{
			Reset();
		} // RtfImageBuilder

		// ----------------------------------------------------------------------
		public void Reset()
		{
			format = RtfVisualImageFormat.Bmp;
			width = 0;
			height = 0;
			desiredWidth = 0;
			desiredHeight = 0;
			scaleWidthPercent = 100;
			scaleHeightPercent = 100;
			imageDataHex = null;
		} // Reset

		// ----------------------------------------------------------------------
		public RtfVisualImageFormat Format
		{
			get { return format; }
		} // Format

		// ----------------------------------------------------------------------
		public int Width
		{
			get { return width; }
		} // Width

		// ----------------------------------------------------------------------
		public int Height
		{
			get { return height; }
		} // Height

		// ----------------------------------------------------------------------
		public int DesiredWidth
		{
			get { return desiredWidth; }
		} // DesiredWidth

		// ----------------------------------------------------------------------
		public int DesiredHeight
		{
			get { return desiredHeight; }
		} // DesiredHeight

		// ----------------------------------------------------------------------
		public int ScaleWidthPercent
		{
			get { return scaleWidthPercent; }
		} // ScaleWidthPercent

		// ----------------------------------------------------------------------
		public int ScaleHeightPercent
		{
			get { return scaleHeightPercent; }
		} // ScaleHeightPercent

		// ----------------------------------------------------------------------
		public string ImageDataHex
		{
			get { return imageDataHex; }
		} // ImageDataHex

		// ----------------------------------------------------------------------
		protected override void DoVisitGroup( IRtfGroup group )
		{
			switch ( group.Destination )
			{
				case RtfSpec.TagPicture:
					Reset();
					VisitGroupChildren( group );
					break;
			}
		} // DoVisitGroup

		// ----------------------------------------------------------------------
		protected override void DoVisitTag( IRtfTag tag )
		{
			switch ( tag.Name )
			{
				case RtfSpec.TagPictureFormatWinDib:
				case RtfSpec.TagPictureFormatWinBmp:
					format = RtfVisualImageFormat.Bmp;
					break;
				case RtfSpec.TagPictureFormatEmf:
					format = RtfVisualImageFormat.Emf;
					break;
				case RtfSpec.TagPictureFormatJpg:
					format = RtfVisualImageFormat.Jpg;
					break;
				case RtfSpec.TagPictureFormatPng:
					format = RtfVisualImageFormat.Png;
					break;
				case RtfSpec.TagPictureFormatWmf:
					format = RtfVisualImageFormat.Wmf;
					break;
				case RtfSpec.TagPictureWidth:
					width = tag.ValueAsNumber;
					desiredWidth = width;
					break;
				case RtfSpec.TagPictureHeight:
					height = tag.ValueAsNumber;
					desiredHeight = height;
					break;
				case RtfSpec.TagPictureWidthGoal:
					desiredWidth = tag.ValueAsNumber;
					if ( width == 0 )
					{
						// hack to prevent WordPad documents which lack the \picw and \pich tags
						// from resulting in an exception due to undefined width/height
						width = desiredWidth;
					}
					break;
				case RtfSpec.TagPictureHeightGoal:
					desiredHeight = tag.ValueAsNumber;
					if ( height == 0 )
					{
						// hack to prevent WordPad documents which lack the \picw and \pich tags
						// from resulting in an exception due to undefined width/height
						height = desiredHeight;
					}
					break;
				case RtfSpec.TagPictureWidthScale:
					scaleWidthPercent = tag.ValueAsNumber;
					break;
				case RtfSpec.TagPictureHeightScale:
					scaleHeightPercent = tag.ValueAsNumber;
					break;
			}
		} // DoVisitTag

		// ----------------------------------------------------------------------
		protected override void DoVisitText( IRtfText text )
		{
			imageDataHex = text.Text;
		} // DoVisitText

		// ----------------------------------------------------------------------
		// members
		private RtfVisualImageFormat format;
		private int width;
		private int height;
		private int desiredWidth;
		private int desiredHeight;
		private int scaleWidthPercent;
		private int scaleHeightPercent;
		private string imageDataHex;

	} // class RtfImageBuilder

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