Unit Conversion

All dimensional properties in GemBox.Document are measured in points unit.

Utility class LengthUnitConverter can be used to convert values between various measurement units.

Following sample demonstrates how to convert points to all other measurement units.

Screenshot

Unit Conversion Screenshot

See the full code below, use RUN EXAMPLE to execute.

Upload your file (Drag files here)

1using System;
2using System.IO;
3using System.Text;
4using GemBox.Document;
5
6class Sample
7{
8    [STAThread]
9    static void Main(string[] args)
10    {
11        // If using Professional version, put your serial key below.
12        ComponentInfo.SetLicense("FREE-LIMITED-KEY");
13
14        DocumentModel document = DocumentModel.Load("Reading.docx");
15
16        StringBuilder sb = new StringBuilder();
17
18        sb.AppendLine("Page size (width X height):");
19
20        double width = document.Sections[0].PageSetup.PageWidth;
21        double height = document.Sections[0].PageSetup.PageHeight;
22
23        foreach (LengthUnit unit in Enum.GetValues(typeof(LengthUnit)))
24        {
25            sb.AppendFormat(
26                "{0} X {1} {2}",
27                LengthUnitConverter.Convert(width, LengthUnit.Point, unit),
28                LengthUnitConverter.Convert(height, LengthUnit.Point, unit),
29                unit.ToString().ToLowerInvariant());
30
31            sb.AppendLine();
32        }
33
34        Console.WriteLine(sb.ToString());
35    }
36}
1Imports System
2Imports System.IO
3Imports System.Text
4Imports GemBox.Document
5
6Module Samples
7
8    Sub Main()
9
10        ' If using Professional version, put your serial key below.
11        ComponentInfo.SetLicense("FREE-LIMITED-KEY")
12
13        Dim document As DocumentModel = DocumentModel.Load("Reading.docx")
14
15        Dim sb As New StringBuilder()
16
17        sb.AppendLine("Page size (width X height):")
18
19        Dim width As Double = document.Sections(0).PageSetup.PageWidth
20        Dim height As Double = document.Sections(0).PageSetup.PageHeight
21
22        For Each unit As LengthUnit In [Enum].GetValues(GetType(LengthUnit))
23
24            sb.AppendFormat("{0} X {1} {2}",
25                            LengthUnitConverter.Convert(width, LengthUnit.Point, unit),
26                            LengthUnitConverter.Convert(height, LengthUnit.Point, unit),
27                            unit.ToString().ToLowerInvariant())
28
29            sb.AppendLine()
30
31        Next
32
33        Console.WriteLine(sb.ToString())
34
35    End Sub
36
37End Module

Check next sample or find out more about GemBox.Document and GemBox Software.