VSTO - Word/Excel单位转换

Word 2007/2010中有各种显示尺寸单位,要想写好VSTO程序我们必须得了解在各种尺寸单位间的转换。

以下是我写的转换函数,希望对大家有帮助。

        /// <summary>
        /// Caculate Height in Pixel
        /// </summary>
        /// <param name="height">User Input Height Value</param>
        /// <param name="mode">Measurement Unit</param>
        /// <param name="dpi">Appliced DPI</param>
        /// <returns>Pixel value caculated</returns>
        public virtual int CaculateHeightInPixel(int height, MeasurementUnitType mode, int dpi)
        {
            int retVal = height;
            switch (mode)
            {
                case MeasurementUnitType.Inches:
                    retVal = height * dpi;
                    break;
                case MeasurementUnitType.Centimeters:
                    retVal = Convert.ToInt32((float)height / 2.54 * dpi);
                    break;
                case MeasurementUnitType.Millimeters:
                    retVal = Convert.ToInt32((float)height / 25.4 * dpi);
                    break;
                case MeasurementUnitType.Pixel:
                    retVal = height;
                    break;
                case MeasurementUnitType.Picas:
                    retVal = Convert.ToInt32((float)height / 6 * dpi);
                    break;
                case MeasurementUnitType.Points:
                    retVal = Convert.ToInt32((float)height / 72 * dpi);
                    break;
            }
            return retVal;
        }

        public virtual int CaculateHeightInMeasurementUnit(int heightInPixel, MeasurementUnitType mode, int dpi)
        {
            int retVal = heightInPixel;
            switch (mode)
            {
                case MeasurementUnitType.Inches:
                    retVal = heightInPixel / dpi;
                    break;
                case MeasurementUnitType.Centimeters:
                    retVal = Convert.ToInt32((float)heightInPixel/dpi * 2.54 );
                    break;
                case MeasurementUnitType.Millimeters:
                    retVal = Convert.ToInt32((float)heightInPixel/dpi * 25.4 );
                    break;
                case MeasurementUnitType.Pixel:
                    retVal = heightInPixel;
                    break;
                case MeasurementUnitType.Picas:
                    retVal = Convert.ToInt32((float)heightInPixel / dpi * 6);
                    break;
                case MeasurementUnitType.Points:
                    retVal = Convert.ToInt32((float)heightInPixel/ dpi * 72);
                    break;
            }
            return retVal;
        }


 

 

需要解决的问题:

1.如何得到当前的显示单位

a.Word 2007/2010中以Word AddIn项目为例,我们可以通过Application.Options.MeasurementUnit得到当前显示单位的设置

b.Excel 2007/2010中同样也已AddIn项目为例,我们可以通过Application.MeasurementUnit得到我们要的单位设置

注意:从Word和Excel中返回的单位设置类型是不一样的,我的建议是我们定义我们自己的单位类型去同时适应两种项目类型。

以下是我做的定义,

    /// <summary>
    /// Measurement Unit Used In Office
    /// </summary>
    public enum MeasurementUnitType
    {
        /// <summary>
        ///  Pixels = Inches * DPI
        /// </summary>
        /// <remarks>
        /// Minumum unit
        /// </remarks>
        Pixel = 1,
        /// <summary>
        /// Point = Inch / 72
        /// </summary>
        /// <remarks>
        /// Available in word
        /// </remarks>
        Points = 2,
        /// <summary>
        /// MM = Inch / 25.4
        /// </summary>
        /// <remarks>
        /// Available in word and excel
        /// </remarks>
        Millimeters = 3,
        /// <summary>
        /// Pica = Inch / 6
        /// </summary>
        /// <remarks>
        /// Available in word
        /// </remarks>
        Picas = 4,
        /// <summary>
        /// CM = Inch / 2.54
        /// </summary>
        /// <remarks>
        /// Available in word and excel
        /// </remarks>
        Centimeters = 5,
        /// <summary>
        /// Inch is comment measure unit in EU and US
        /// </summary>
        /// <remarks>
        /// Available in word and excel
        /// </remarks>
        Inches = 6,
    }


2.怎么得到对应的DPI

待续。。。

 

在ZInt条码系统中的应用,

//input_height: 用户输入的高度数值

//mod: 用户选择的显示单位

//border_pix_offset : 边框宽度

//shr: 是否显示可读文本, 9是内置文本的字体高度

symbology_pix_Height = caculate_pix_height(input_height,mod)/2 - 2*border_pix_offset - shr?9:0

 

参考:

1.http://auctionrepair.com/pixels.html

2.http://stackoverflow.com/questions/604203/twips-pixels-and-points-oh-my

3.http://en.wikipedia.org/wiki/Centimetre

 

你可能感兴趣的:(Excel,Office,input,float,border,2010)