ARTICLE UNDER CONSTRUCTION
Unit
Version 6.1.0
In this article
This article introduces the Unit class.
Introduction
The Unit class gives you the ability to create measurements as numbers combined with one of a set of typical measures. Whenever you work with distances on a page or font sizes, Unit will be the type to be used for those values. The UnitType enum defines the measures supported by Unit: Centimeter, Millimeter, Point, Inch and Pica.
Creating a Unit value
For each UnitType value there is a static method to create a Unit instance of the respective measure. You could for example set an indent with:
format.LeftIndent = Unit.FromCentimeter(1);
See full CreatingUnitValue sample in Unit in PDFsharp.Sample repository. Show resulting PDF
To create a unit of another type, you could call FromMillimeter, FromPoint, FromInch, or FromPica instead.
Alternatively, you can assign a string value to a property of the type Unit. There is an implicit conversion from String to Unit which parses the given string. The string shall therefore contain the numeric value, followed by the abbreviation for the measure to be used:
format.LeftIndent = "1cm";
See full CreatingUnitValue sample in Unit in PDFsharp.Sample repository. Show resulting PDF
You can use the following measure abbreviations: "cm" for Centimeter, "mm" for Millimeter, "pt" for Point, "in" for Inch and "pc" for Pica. If no abbreviation is given, Point will be used as the default unit type.
Getting other measure values
For each UnitType value, there is a property in Unit to get the value in the respective measure. Those are simply named like the unit types: Centimeter, Millimeter, Point, Inch and Pica.
See GettingOtherMeasureValues sample in Unit in PDFsharp.Sample repository. Show resulting PDF
You can use those properties to assign a new value, too. This will also change the measure of the unit to the respective UnitType value.
Changing the unit type
If you change the unit type of a Unit instance by setting its Type property, its numeric value won’t change:
var unit = Unit.FromInch(1);
unit.Type = UnitType.Centimeter;
Due to using the same numeric value with a different unit type, the described distance or size will change.
To preserve the described distance or size, call the ConvertType method instead:
var unit = Unit.FromInch(1);
unit.ConvertType(UnitType.Centimeter);
This will update the numeric value to keep the described distance or size unchanged.
See full ChangingUnitType sample in Unit in PDFsharp.Sample repository. Show resulting PDF
Comparing
When comparing two Unit instances with the CompareTo method, actually its Point values are compared. Due to that, you can compare units of different unit types. But you have to mind, that rounding differences may occur, that avoid determining equal values with CompareTo. For example, when comparing a 1 centimeter Unit instance with a 10 millimeter Unit instance, CompareTo won’t return 0, as there will be a slight rounding difference. If finding equal values in a comparison is necessary, you could call IsSameValue first (see below).
To check, if two units describe the same distance or size, you can use the IsSameValue method. It compares the Point values of the Units, while suppressing rounding errors. Therefore, calling IsSameValue for a 1 centimeter Unit instance and a 10 millimeter Unit instance will return true.
The Equals method considers also the unit type, so units of a different measure can never be the same.
See Comparing sample in Unit in PDFsharp.Sample repository. Show resulting PDF