pdftools_toolbox.pdf.content.text_fragment

Classes

TextFragment(handle)

Text Fragment

class pdftools_toolbox.pdf.content.text_fragment.TextFragment(handle)[source]

Bases: _NativeObject, list

Text Fragment

A pdftools_toolbox.pdf.content.text_element.TextElement contains an arbitrary number of text fragments. Text can be partitioned arbibrarily into fragments without respecting word boundaries or reading order.

A text fragment provides iteration over all contained pdftools_toolbox.pdf.content.glyph.Glyph s. Removing, clearing, adding, and sub-ranges are not supported. While iterating, a pdftools_toolbox.corrupt_error.CorruptError may be generated if the text fragment contains glyphs with inconsistent Unicode information.

property bounding_box: Rectangle

the bounding box

This is a rectangle that encompasses all parts of the text fragment.

Returns:

pdftools_toolbox.geometry.real.rectangle.Rectangle

Raises:

StateError – the object has already been closed

property transform: AffineTransform

the transform to be applied to the bounding box rectangle

Use this transform matrix to compute the actual location of the text fragment’s bounding box.

Returns:

pdftools_toolbox.geometry.real.affine_transform.AffineTransform

Raises:

StateError – the object has already been closed

property text: str | None

The string painted by this text fragment

Returns:

Optional[str]

Raises:

StateError – the object has already been closed

property stroke: Stroke | None

This text fragment’s parameters for stroking the text or None if the text is not stroked

Returns:

Optional[pdftools_toolbox.pdf.content.stroke.Stroke]

Raises:

StateError – the object has already been closed

property fill: Fill | None

This text fragment’s parameters for filling the text or None if the text is not filled

Returns:

Optional[pdftools_toolbox.pdf.content.fill.Fill]

Raises:

StateError – the object has already been closed

property font_size: float

The font size

Returns:

float

Raises:

StateError – the object has already been closed

property character_spacing: float

The additional spacing between glyphs

When the glyph for each character in the text is rendered, the character spacing is added to the horizontal or vertical component of the glyph’s displacement, depending on the writing mode. It is subject to scaling by the horizontal scaling if the writing mode is horizontal.

Returns:

float

Raises:

StateError – the object has already been closed

property word_spacing: float

The additional spacing between words

Word spacing works the same way as character spacing, but applies only to the space character, code 32.

Returns:

float

Raises:

StateError – the object has already been closed

property horizontal_scaling: float

The horizontal scaling factor

The horizontal scaling parameter adjusts the width of glyphs by stretching or compressing them in the horizontal direction. Its value is specified relative to the normal width of the glyphs, with 1 being the normal width.

Returns:

float

Raises:

StateError – the object has already been closed

property rise: float

The rise of the baseline

The text rise specifies the distance to move the baseline up or down from its default location. Positive values of text rise move the baseline up. Adjustments to the baseline are useful for drawing superscripts or subscripts.

Returns:

float

Raises:

StateError – the object has already been closed

property writing_mode: WritingMode

The writing direction

This is the writing mode for the text fragment. It applies to all contained pdftools_toolbox.pdf.content.glyph.Glyph s.

Returns:

pdftools_toolbox.pdf.content.writing_mode.WritingMode

Raises:

StateError – the object has already been closed

property font: Font

The font

The returned pdftools_toolbox.pdf.content.font.Font can only be used for extraction purposes. Specifically, using this object in pdftools_toolbox.pdf.content.text_generator.TextGenerator.__init__ or in pdftools_toolbox.pdf.content.text_generator.TextGenerator.font results in a ValueError error.

Returns:

pdftools_toolbox.pdf.content.font.Font

Raises:

StateError – the object has already been closed

clear() None[source]

Remove all items from list.

remove(index: int) None[source]

Remove first occurrence of value.

Raises ValueError if the value is not present.

extend(items: TextFragment) None[source]

Extend list by appending elements from the iterable.

insert(index: int, value: Any) None[source]

Insert object before index.

pop(index: int = -1) Any[source]

Remove and return item at index (default last).

Raises IndexError if list is empty or index is out of range.

copy() TextFragment[source]

Return a shallow copy of the list.

sort(key=None, reverse=False) None[source]

Sort the list in ascending order and return None.

The sort is in-place (i.e. the list itself is modified) and stable (i.e. the order of two equal elements is maintained).

If a key function is given, apply it once to each list item and sort them, ascending or descending, according to their function values.

The reverse flag can be set to sort in descending order.

reverse() None[source]

Reverse IN PLACE.

append(value: Glyph) None[source]

Append object to the end of the list.

index(value: Glyph, start: int = 0, stop: int | None = None) int[source]

Return first index of value.

Raises ValueError if the value is not present.