Unit Conversion

Different users use various measurement systems. For convenience, DocumentCore has a class for converting values in different measurement units.

LengthUnitConverter can convert betwen:

  • Point - 1 point is equal of 1/72 inch.
  • Pixel - 1 pixel is equal of 1/96 pixels.
  • Inch - 1 inch is equal of 72 points or 25.4 mm.
  • Millimeter - 1 millimeter is equal of 1/10 centimeter.
  • Centimeter - 1 centimeter is equal of 10 millimeter or ≈ 0.3937008 inches.
  • Pica - 1 pica is equal of 12 points.
  • Twip - 1 twip is equal as 1/20 point.
  • English Metric Unit - 1 point is equal as 12700 EMUs.

Let's see an example of using LengthUnitConverter. Say, we need to specify page margins. These values must be in points, but for us is comfortable to use centimeters and millimeters:

            DocumentCore dc = new DocumentCore();
            // Add new section.
            Section sectFirst = new Section(dc);
            sectFirst.PageSetup.PageMargins = new PageMargins()
            {
                Top = LengthUnitConverter.Convert(50, LengthUnit.Millimeter, LengthUnit.Point),
                Right = LengthUnitConverter.Convert(1, LengthUnit.Inch, LengthUnit.Point),
                Bottom = LengthUnitConverter.Convert(10, LengthUnit.Millimeter, LengthUnit.Point),
                Left = LengthUnitConverter.Convert(2, LengthUnit.Centimeter, LengthUnit.Point)
            };

Complete code

using System;
using SautinSoft.Document;

namespace Sample
{
    class Sample
    {
        static void Main(string[] args)
        {
            ConvertPointsTo();
            UsingUnitConversion();
        }

        public static void ConvertPointsTo()
        {
            foreach (LengthUnit unit in Enum.GetValues(typeof(LengthUnit)))
            {
                string s = string.Format("1 point = {0} {1}",
                    LengthUnitConverter.Convert(1, LengthUnit.Point, unit),
                    unit.ToString().ToLowerInvariant());

                Console.WriteLine(s);
            }
            Console.ReadLine();
        }

        public static void UsingUnitConversion()
        {
            DocumentCore dc = new DocumentCore();

            // Add new section.
            Section sectFirst = new Section(dc);
            sectFirst.PageSetup.PageMargins = new PageMargins()
            {
                Top = LengthUnitConverter.Convert(50, LengthUnit.Millimeter, LengthUnit.Point),
                Right = LengthUnitConverter.Convert(1, LengthUnit.Inch, LengthUnit.Point),
                Bottom = LengthUnitConverter.Convert(10, LengthUnit.Millimeter, LengthUnit.Point),
                Left = LengthUnitConverter.Convert(2, LengthUnit.Centimeter, LengthUnit.Point)
            };
        }
    }
}

Download.

        
            Imports SautinSoft.Document

Module ExampleVB
    Sub Main()
        ConvertPointsTo()
        UsingUnitConversion()
    End Sub

    Public Sub ConvertPointsTo()
        For Each unit As LengthUnit In System.Enum.GetValues(GetType(LengthUnit))
            Dim s As String = String.Format("1 point = {0} {1}", LengthUnitConverter.Convert(1, LengthUnit.Point, unit), unit.ToString().ToLowerInvariant())
            Console.WriteLine(s)
        Next unit
        Console.ReadLine()
    End Sub

    Public Sub UsingUnitConversion()
        Dim dc As New DocumentCore()

        ' Add new section.
        Dim sectFirst As New Section(dc)
        sectFirst.PageSetup.PageMargins = New PageMargins() With {
                .Top = LengthUnitConverter.Convert(50, LengthUnit.Millimeter, LengthUnit.Point),
                .Right = LengthUnitConverter.Convert(1, LengthUnit.Inch, LengthUnit.Point),
                .Bottom = LengthUnitConverter.Convert(10, LengthUnit.Millimeter, LengthUnit.Point),
                .Left = LengthUnitConverter.Convert(2, LengthUnit.Centimeter, LengthUnit.Point)
            }
    End Sub
End Module

Download.

© SautinSoft 2020