VSTO - Word/Excel单位转换
来源:互联网 发布:java 获取项目名称 编辑:程序博客网 时间:2024/06/06 19:33
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
- VSTO - Word/Excel单位转换
- HOW TO : EXCEL/WORD @ VSTO 2005
- VSTO WORD C# 备忘
- VSTO word操作
- 如何简单部署用VSTO SE 2005开发的Excel,Word插件
- VSTO 为Office已有右键菜单添加自己的菜单项(word,Excel)
- VSTO 得到Office文档的选中内容(Word、Excel、PPT、Outlook)
- 如何简单部署用VSTO SE 2005开发的Excel,Word插件
- 如何简单部署用VSTO SE 2005开发的Excel,Word插件
- word&excel转换成html
- Word,Excel大小写批量转换
- VSTO 添加Word右键菜单
- 怎么将Word转为Excel,Excel转换成Word?
- Excel 2007 VSTO 部署问题
- VSTO---EXCEL常用处理方法
- WORD及EXCEL行列转换实现方法
- WORD及EXCEL行列转换实现方法
- 运用 Jacob转换Word,Excel为HTML
- 嵌入式开发 gooogleman blog url
- iPhone开发经典语录集锦
- 创业计划书模板(周鸿祎口述)
- 搜狐:周鸿祎分享创业经验教训 10页融资PPT受追捧
- [独家]周鸿祎对话南方创业草根 总结自身功过
- VSTO - Word/Excel单位转换
- 最伤陔子的十句话
- 人争什么呢,写的太好了
- 委托(Delegate)机制
- Windows 关闭端口8080 的方法(8080端口被占用)
- 使用Pencil进行UI草图设计
- [独家]周鸿祎对话南方创业草根 总结自身功过
- 协议(Protocol)用法专题总结
- 周鸿祎:创业团队如何散财聚人