1919class TextStreamAppearance (DecodedStreamObject ):
2020 """
2121 A class representing the appearance stream for a text-based form field.
22- This class is similar in form to the FreeText class in pypdf.
22+
23+ This class generates the content stream (the `ap_stream_data`) that dictates
24+ how text is rendered within a form field's bounding box. It handles properties
25+ like font, font size, color, multiline text, and text selection highlighting.
2326 """
2427 def _appearance_stream_data (
2528 self ,
@@ -32,6 +35,31 @@ def _appearance_stream_data(
3235 font_color : str = "0 g" ,
3336 multiline : bool = False
3437 ) -> bytes :
38+ """
39+ Generates the raw bytes of the PDF appearance stream for a text field.
40+
41+ This private method assembles the PDF content stream operators to draw
42+ the provided text within the specified rectangle. It handles text positioning,
43+ font application, color, and special formatting like selected text.
44+
45+ Args:
46+ text: The text to be rendered in the form field.
47+ selection: An optional list of strings that should be highlighted as selected.
48+ font_glyph_byte_map: An optional dictionary mapping characters to their
49+ byte representation for glyph encoding.
50+ rect: The bounding box of the form field. Can be a `RectangleObject`
51+ or a tuple of four floats (x1, y1, x2, y2).
52+ font_name: The name of the font resource to use (e.g., "/Helv").
53+ font_size: The font size. If 0, it is automatically calculated
54+ based on whether the field is multiline or not.
55+ font_color: The color to apply to the font, represented as a PDF
56+ graphics state string (e.g., "0 g" for black).
57+ multiline: A boolean indicating if the text field is multiline.
58+
59+ Returns:
60+ A byte string containing the PDF content stream data.
61+
62+ """
3563 font_glyph_byte_map = font_glyph_byte_map or {}
3664 if isinstance (rect , tuple ):
3765 rect = RectangleObject (rect )
@@ -80,6 +108,25 @@ def __init__(
80108 font_color : str = "0 g" ,
81109 multiline : bool = False
82110 ) -> None :
111+ """
112+ Initializes a TextStreamAppearance object.
113+
114+ This constructor creates a new PDF stream object configured as an XObject
115+ of subtype Form. It uses the `_appearance_stream_data` method to generate
116+ the content for the stream.
117+
118+ Args:
119+ text: The text to be rendered in the form field.
120+ selection: An optional list of strings that should be highlighted as selected.
121+ rect: The bounding box of the form field. Can be a `RectangleObject`
122+ or a tuple of four floats (x1, y1, x2, y2).
123+ font_resource: An optional variable that represents a PDF font dictionary.
124+ font_name: The name of the font resource, e.g., "/Helv".
125+ font_size: The font size. If 0, it's auto-calculated.
126+ font_color: The font color string.
127+ multiline: A boolean indicating if the text field is multiline.
128+
129+ """
83130 # If a font resource was added, get the font character map
84131 if font_resource :
85132 font_resource = cast (DictionaryObject , font_resource .get_object ())
@@ -137,7 +184,27 @@ def from_text_annotation(
137184 user_font_name : str = "" ,
138185 user_font_size : float = - 1 ,
139186 ) -> "TextStreamAppearance" :
140- """Creates a TextStreamAppearance object from a given text field annotation."""
187+ """
188+ Creates a TextStreamAppearance object from a text field annotation.
189+
190+ This class method is a factory for creating a `TextStreamAppearance`
191+ instance by extracting all necessary information (bounding box, font,
192+ text content, etc.) from the PDF field and annotation dictionaries.
193+ It respects inheritance for properties like default appearance (`/DA`).
194+
195+ Args:
196+ acro_form: The root AcroForm dictionary from the PDF catalog.
197+ field: The field dictionary object.
198+ annotation: The widget annotation dictionary object associated with the field.
199+ user_font_name: An optional user-provided font name to override the
200+ default. Defaults to an empty string.
201+ user_font_size: An optional user-provided font size to override the
202+ default. A value of -1 indicates no override.
203+
204+ Returns:
205+ A new `TextStreamAppearance` instance configured for the given field.
206+
207+ """
141208 # Calculate rectangle dimensions
142209 _rect = cast (RectangleObject , annotation [AnnotationDictionaryAttributes .Rect ])
143210 rect = RectangleObject ((0 , 0 , abs (_rect [2 ] - _rect [0 ]), abs (_rect [3 ] - _rect [1 ])))
0 commit comments