C# dontet Office Open XML Unit Converter
2021/6/29 20:25:27
本文主要是介绍C# dontet Office Open XML Unit Converter,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
Here is my code to conver between OpenXML units.
Define
If you use csharp language version lower than 7.0, please remove the readonly keyword from the C# code below
public readonly struct Cm { public Cm(double value) { Value = value; } public double Value { get; } } public readonly struct Dxa { public Dxa(double value) { Value = value; } public double Value { get; } } public readonly struct Emu { public Emu(double value) { Value = value; } public double Value { get; } } public readonly struct FiftiethsOfAPercent { public FiftiethsOfAPercent(double value) { Value = value; } public double Value { get; } } public readonly struct HalfPoint { public HalfPoint(double value) { Value = value; } public double Value { get; } } public readonly struct Inch { public Inch(double value) { Value = value; } public double Value { get; } } public readonly struct Mm { public Mm(double value) { Value = value; } public double Value { get; } } public readonly struct Pixel { public Pixel(double value) { Value = value; } public double Value { get; } } public readonly struct Pt { public Pt(double value) { Value = value; } public double Value { get; } }
Convert
This is the code for converting different units of OpenXML
public static class UnitConverter { public const double DefaultDpi = 96; #region Pixel public static Pixel ToPixel(this Inch inch) => inch.ToEmu().ToPixel(); public static Inch ToInch(this Pixel pixel) => pixel.ToEmu().ToInch(); #endregion #region Dxa public static Pt ToPt(this Dxa dxa) { return new Pt(dxa.Value / 20); } public static Dxa ToDxa(this Pt pt) { return new Dxa(pt.Value * 20); } public static Inch ToInch(this Dxa dxa) { return new Inch(dxa.Value / 72); } public static Dxa ToDxa(this Inch inch) { return new Dxa(inch.Value * 72); } #endregion #region Mm public static Cm ToCm(this Mm mm) { return new Cm(mm.Value / 10); } public static Mm ToMm(this Cm cm) { return new Mm(cm.Value * 10); } #endregion #region Pt public static Cm ToCm(this Pt pt) { return pt.ToEmu().ToCm(); } public static Pt ToPt(this Cm cm) { return cm.ToEmu().ToPt(); } public static Mm ToMm(this Pt pt) { return pt.ToCm().ToMm(); } public static Pt ToPt(this Mm mm) { return mm.ToCm().ToPt(); } public static Pt ToPt(this HalfPoint halfPoint) { return new Pt(halfPoint.Value / 2); } public static HalfPoint ToHalfPoint(this Pt pt) { return new HalfPoint(pt.Value * 2); } public static Pixel ToPixel(this Pt pt) { return new Pixel(pt.Value / 72 * DefaultDpi); } public static Pt ToPoint(this Pixel px) { return new Pt(px.Value * 72 / DefaultDpi); } #endregion #region Emu public static Emu ToEmu(this Inch inch) { return new Emu(inch.Value * 914400); } public static Inch ToInch(this Emu emu) { return new Inch(emu.Value / 914400); } public static Emu ToEmu(this Cm cm) { return new Emu(cm.Value * 360000); } public static Cm ToCm(this Emu emu) { return new Cm(emu.Value / 360000); } public static Emu ToEmu(this Mm cm) { return new Emu(cm.Value * 36000); } public static Dxa ToDxa(this Emu emu) { return new Dxa(emu.Value / 635); } public static Emu ToEmu(this Dxa dxa) { return new Emu(dxa.Value * 635); } public static Mm ToMm(this Emu emu) { return new Mm(emu.Value / 36000); } public static Emu ToEmu(this Pixel px) { return new Emu(px.Value * 914400 / DefaultDpi); } public static Pixel ToPixel(this Emu emu) { return new Pixel(emu.Value / 914400 * DefaultDpi); } public static Emu ToEmu(this Pt pt) { return new Emu(pt.Value * 12700); } public static Pt ToPt(this Emu emu) { return new Pt(emu.Value / 12700); } #endregion }
Test
The code in this article is not technical, but if you copy the code, it will reduce your time.
private void AssertDoubleEqual(double a, double b) { Assert.AreEqual(true, Math.Abs(a - b) < 0.001); } var pt = new Pt(1); AssertDoubleEqual(20, pt.ToDxa().Value); AssertDoubleEqual(12700, pt.ToEmu().Value); AssertDoubleEqual(0.3527777777777777, pt.ToMm().Value); AssertDoubleEqual(0.035277777777777776, pt.ToCm().Value); AssertDoubleEqual(0.01388888888, pt.ToEmu().ToInch().Value); var mm = new Mm(1000); AssertDoubleEqual(100, mm.ToCm().Value); AssertDoubleEqual(39.370078740157, mm.ToEmu().ToInch().Value); AssertDoubleEqual(2834.64566929133, mm.ToPt().Value); AssertDoubleEqual(56692.913385826, mm.ToEmu().ToDxa().Value); AssertDoubleEqual(36000000, mm.ToEmu().Value);
See Office Open XML file formats - Wikipedia
English Metric Units and Open XML
Office Open XML Dashboard
Points, inches and Emus: Measuring units in Office Open XML – Lars Corneliussen
[译]Points、inches和EMUs:Office Open XML中的度量单位 - 知乎
Office Open XML 的测量单位
我搭建了自己的博客 https://blog.lindexi.com/ 欢迎大家访问,里面有很多新的博客。
如果在博客看到有任何不懂的,欢迎交流
这篇关于C# dontet Office Open XML Unit Converter的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2022-03-01沐雪多租宝商城源码从.NetCore3.1升级到.Net6的步骤
- 2024-11-18微软研究:RAG系统的四个层次提升理解与回答能力
- 2024-11-15C#中怎么从PEM格式的证书中提取公钥?-icode9专业技术文章分享
- 2024-11-14云架构设计——如何用diagrams.net绘制专业的AWS架构图?
- 2024-05-08首个适配Visual Studio平台的国产智能编程助手CodeGeeX正式上线!C#程序员必备效率神器!
- 2024-03-30C#设计模式之十六迭代器模式(Iterator Pattern)【行为型】
- 2024-03-29c# datetime tryparse
- 2024-02-21list find index c#
- 2024-01-24convert toint32 c#
- 2024-01-24Advanced .Net Debugging 1:你必须知道的调试工具