Different users use various measurement systems. For convenience, DocumentCore has a class for converting values in different measurement units.
LengthUnitConverter can convert betwen:
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)
{
// Get your free 30-day key here:
// https://sautinsoft.com/start-for-free/
ConvertPointsTo();
UsingUnitConversion();
}
/// <summary>
/// Shows what is the one point is equal in different units.
/// </summary>
/// <remarks>
/// Details: https://sautinsoft.com/products/document/help/net/developer-guide/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/help/net/developer-guide/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 });
}
}
}
Imports System
Imports System.IO
Imports SautinSoft.Document
Module Sample
Sub Main()
ConvertPointsTo()
UsingUnitConversion()
End Sub
''' Get your free 30-day key here:
''' https://sautinsoft.com/start-for-free/
''' <summary>
''' Shows what is the one point is equal in different units.
''' </summary>
''' <remarks>
''' Details: https://sautinsoft.com/products/document/help/net/developer-guide/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/help/net/developer-guide/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
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: