Click here to Skip to main content
12,621,492 members (34,956 online)
Click here to Skip to main content

Tagged as

Stats

103.7K views
3.6K downloads
100 bookmarked
Posted

RTF Document Constructor Library

, 16 Aug 2010 CPOL
Create Rich Text Format documents programatically.
using System;

namespace ESCommon
{
    public enum MetricUnit { Point, Millimeter, Centimeter, Inch }
    
    /// <summary>
    /// Converts twips to metric units, and metric units to twips
    /// </summary>
    public class TwipConverter
    {
        public const float 
            PointsInTwip = .05F,
            MillimetersInTwip = .0176389F,
            CentimetersInTwip = .0017639F,
            InchsInTwip = 1F / 1440,
 
            TwipsInPoint = 20,
            TwipsInMillimeter = 56.6929134F,
            TwipsInCentimeter = 566.9291339F,
            TwipsInInch = 1440F;

        private static float[]
            ToUnitConversion = new float[] {
                PointsInTwip,
                MillimetersInTwip,
                CentimetersInTwip,
                InchsInTwip,
            },
            ToTwipConversion = new float[] {
                TwipsInPoint,
                TwipsInMillimeter,
                TwipsInCentimeter,
                TwipsInInch,
            };

        /// <summary>
        /// Converts metric values to twips
        /// </summary>
        /// <param name="value">Value in metric units</param>
        /// <param name="unit">Unit which is used to specify the value</param>
        /// <returns>Value in twips</returns>
        public static int ToTwip(float value, MetricUnit unit)
        {   
            return (int)Math.Round(value * ToTwipConversion[(int)unit]);
        }

        /// <summary>
        /// Converts twips to points
        /// </summary>
        /// <param name="value">Value in twips</param>
        /// <returns>Value in points</returns>
        public static float ToPoint(int value)
        {
            return ToMetricUnit(value, MetricUnit.Point);
        }

        /// <summary>
        /// Converts twips to millimeters
        /// </summary>
        /// <param name="value">Value in twips</param>
        /// <returns>Value in millimeters</returns>
        public static float ToMillimeter(int value)
        {
            return ToMetricUnit(value, MetricUnit.Millimeter);
        }

        /// <summary>
        /// Converts twips to centimeters
        /// </summary>
        /// <param name="value">Value in twips</param>
        /// <returns>Value in centimeters</returns>
        public static float ToCentimeter(int value)
        {
            return ToMetricUnit(value, MetricUnit.Centimeter);
        }

        /// <summary>
        /// Converts twips to metric values
        /// </summary>
        /// <param name="value">Value in twips</param>
        /// <param name="unit">Metric unit to convert to</param>
        /// <returns>Value in specified metric units</returns>
        public static float ToMetricUnit(int value, MetricUnit unit)
        {
            return (float)Math.Round(value * ToUnitConversion[(int)unit]);
        }
    }
}

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)

Share

About the Author

Dima Popov
Software Developer Energoservice
Russian Federation Russian Federation
Dmitry lives in Arkhangelsk, Russia. He has developed C# applications since 2007.

You may also be interested in...

Pro
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161128.1 | Last Updated 16 Aug 2010
Article Copyright 2010 by Dima Popov
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid