Class Length

  • All Implemented Interfaces:
    java.lang.Comparable<Length>

    public class Length
    extends java.lang.Object
    implements java.lang.Comparable<Length>

    Class that represents a measurable length. Provides conversion between units, parsing of string representations of lengths, and basic mathematical operations.

    The unit used in PDF documents is Units.POINT, which is also used internally by the Length object to store length values. For that reason, when converting units to and from the internally used unit, minor numerical differences may occur due to floating-point arithmetic.

    • Field Detail

      • ZERO

        public static final Length ZERO
      • MAX_VALUE

        public static final Length MAX_VALUE
    • Constructor Detail

      • Length

        public Length​(double value,
                      Length.Units unit)
        Constructs a newly allocated Length object using the specified value and unit.
        Parameters:
        value - the numerical value.
        unit - the unit.
    • Method Detail

      • to

        public double to​(Length.Units unit)
        Get the numerical length value of the specified unit.
        Parameters:
        unit - the unit.
        Returns:
      • parse

        public static Length parse​(java.lang.String value)
        Creates a Length object by parsing a String representation of a length with its unit.
        Parameters:
        value - Value-unit pair of the form "<value><unit>". Examples: "12.3cm" or "23.9mm". Allowed units are "um", "mm", "cm", "m", "km", "pt", and "in".
        Throws:
        java.lang.IllegalArgumentException - if unit given in value is invalid.
      • toString

        public java.lang.String toString()
        Creates a string representation with an associated suitable metric unit, "m", "cm" or "mm".
        Overrides:
        toString in class java.lang.Object
      • toString

        public java.lang.String toString​(Length.Units unit)
        Creates a string representation with the specified unit.
      • parseArray

        public static Length[] parseArray​(java.lang.String value,
                                          int size)
        Creates an array of Length by parsing a String representation of lengths with units.
        Parameters:
        value - A group of value-unit pairs of the form "<value1><unit1> <value2><unit2>...". Example: "12.3cm 23.9mm 0.25in" etc. Allowed units are "um", "mm", "cm", "m", "km", "pt" and "in".
        size - Number of value-unit pairs represented by the value parameter.
        Throws:
        java.lang.IllegalArgumentException - if parameter size doesn't fit the number of values in value.
        java.lang.IllegalArgumentException - if a unit is invalid.
      • parseArray

        public static Length[] parseArray​(java.lang.String value)
        Creates an array of Length by parsing a String representation of lengths with units.
        Parameters:
        value - A group of value-unit pairs of the form "<value1><unit1> <value2><unit2>...". Example: "12.3cm 23.9mm 0.25in" etc. Allowed units are "um", "mm", "cm", "m", "km", "pt" and "in".
        Throws:
        java.lang.IllegalArgumentException - if a unit is invalid.
      • multiply

        public Length multiply​(double multiplicand)
      • divide

        public Length divide​(double divisor)
      • divide

        public double divide​(Length divisor)
      • equals

        public boolean equals​(java.lang.Object obj)
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • compareTo

        public int compareTo​(Length o)
        Specified by:
        compareTo in interface java.lang.Comparable<Length>