Document .Net - Measure Unit Conversion

Measure 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:

Complete code

using System;
using SautinSoft.Document;

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

        /// <summary>
        /// Shows what is the one point is equal in different units.
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/examples/unit-conversion.php
        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.WriteLine("Press any key...");
            Console.ReadKey();
        }

        /// <summary>
        /// How to convert between different units.
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/examples/unit-conversion.php
        public static void UsingUnitConversion()
        {
            DocumentCore dc = new DocumentCore();

            // Add new section.
            Section s1 = new Section(dc);
            s1.PageSetup.PageWidth = LengthUnitConverter.Convert(100, LengthUnit.Millimeter, LengthUnit.Point);
            s1.PageSetup.PageHeight = LengthUnitConverter.Convert(1, LengthUnit.Inch, LengthUnit.Point);
            s1.PageSetup.PageMargins.Left = LengthUnitConverter.Convert(0.3, LengthUnit.Centimeter, LengthUnit.Point);
            s1.PageSetup.PageMargins.Top = LengthUnitConverter.Convert(7, LengthUnit.Pixel, LengthUnit.Point);
            s1.PageSetup.PageMargins.Right = LengthUnitConverter.Convert(100, LengthUnit.Twip, LengthUnit.Point);
            s1.PageSetup.PageMargins.Bottom = LengthUnitConverter.Convert(5, LengthUnit.Millimeter, LengthUnit.Point);

            dc.Sections.Add(s1);
            s1.Content.Start.Insert("You are welcome!", new CharacterFormat() { FontColor = Color.Orange, Size = 36 });

            dc.Save("Result.docx");

            // Open the result for demonstration purposes.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo("Result.docx") { UseShellExecute = true });
        }
    }
}

Download.

        
            Imports System
Imports System.IO
Imports SautinSoft.Document

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

    ''' <summary>
    ''' Shows what is the one point is equal in different units.
    ''' </summary>
    ''' <remarks>
    ''' Details: https://sautinsoft.com/products/document/examples/unit-conversion.php
    ''' </remarks>
    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.WriteLine("Press any key...")
        Console.ReadKey()
    End Sub

    ''' <summary>
    ''' How to convert between different units.
    ''' </summary>
    ''' <remarks>
    ''' Details: https://sautinsoft.com/products/document/examples/unit-conversion.php
    ''' </remarks>
    Sub UsingUnitConversion()
        Dim dc As New DocumentCore()

        ' Add new section.
        Dim s1 As New Section(dc)
        s1.PageSetup.PageWidth = LengthUnitConverter.Convert(100, LengthUnit.Millimeter, LengthUnit.Point)
        s1.PageSetup.PageHeight = LengthUnitConverter.Convert(1, LengthUnit.Inch, LengthUnit.Point)
        s1.PageSetup.PageMargins.Left = LengthUnitConverter.Convert(0.3, LengthUnit.Centimeter, LengthUnit.Point)
        s1.PageSetup.PageMargins.Top = LengthUnitConverter.Convert(7, LengthUnit.Pixel, LengthUnit.Point)
        s1.PageSetup.PageMargins.Right = LengthUnitConverter.Convert(100, LengthUnit.Twip, LengthUnit.Point)
        s1.PageSetup.PageMargins.Bottom = LengthUnitConverter.Convert(5, LengthUnit.Millimeter, LengthUnit.Point)

        dc.Sections.Add(s1)
        s1.Content.Start.Insert("You are welcome!", New CharacterFormat() With {
                .FontColor = Color.Orange,
                .Size = 36
            })

        dc.Save("Result.docx")

        ' Open the result for demonstration purposes.
        System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo("Result.docx") With {.UseShellExecute = True})
    End Sub
End Module

Download.


If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below:



Questions and suggestions from you are always welcome!

We are developing .Net components since 2002. We know PDF, DOCX, RTF, HTML, XLSX and Images formats. If you need any assistance with creating, modifying or converting documents in various formats, we can help you. We will write any code example for you absolutely free.

© SautinSoft 2002 - 2019