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
Points, inches and Emus: Measuring units in Office Open XML – Lars Corneliussen
[译]Points、inches和EMUs:Office Open XML中的度量单位 - 知乎
我搭建了自己的博客 https://blog.lindexi.com/ 欢迎大家访问,里面有很多新的博客。
如果在博客看到有任何不懂的,欢迎交流
标签:dontet,XML,return,Converter,Value,Emu,static,new,public 来源: https://blog.51cto.com/u_11283245/2955549