
    @wg=              
          d dl Z d dlZd dlmZ d dlmZ d dlmZmZmZm	Z	m
Z
mZmZmZmZmZ ddlmZ ddlmZmZ ddlmZ dd	lmZmZmZ dd
lmZ ddlmZ  ddlm!Z!m"Z"m#Z# ddlm$Z% ddlm&Z' ddlm(Z) ddlm*Z+ ddlm,Z- ddl.m/Z/m0Z0 ddl1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@mAZAmBZBmCZCmDZD ddlEmFZFmGZG ddlHmIZI deJdeKdeeKeedf   f   fdZL G d de6      ZM G d d      ZN G d d eeef         ZOy)!    N)abstractmethod)datetime)
AnyDictIterableIteratorListMappingOptionalTupleUnioncast   )
Encryption)
PageObject_VirtualList)index2label)deprecate_with_replacementlogger_warningparse_iso8824_date)CatalogAttributes)CatalogDictionary)CheckboxRadioButtonAttributesGoToActionArgumentsUserAccessPermissions)Core)DocumentInformationAttributes)FieldDictionaryAttributes)PageAttributes)PagesAttributes)PdfReadError
PyPdfError)ArrayObjectBooleanObjectByteStringObjectDestinationDictionaryObjectEncodedStreamObjectFieldFitFloatObjectIndirectObject
NameObject
NullObjectNumberObject	PdfObjectTextStringObject
TreeObjectViewerPreferencescreate_string_objectis_null_or_none)OutlineTypePagemodeType)XmpInformationdsizereturn.c                 h    |dkD  rt        d      d| z   } | dd  } t        j                  d|       d   S )N   zInvalid size in convert_to_ints           iz>qr   )r!   structunpack)r9   r:   s     H/var/www/horilla/myenv/lib/python3.12/site-packages/pypdf/_doc_common.pyconvert_to_intrA   ^   s@    ax;<<+a/A	"#A==q!!$$    c                      e Zd ZdZddZdedee   fdZedee   fd       Z	edee   fd       Z
edee   fd	       Zedee   fd
       Zedee   fd       Zedee   fd       Zedee   fd       Zedee   fd       Zedee   fd       Zedee   fd       Zedee   fd       Zedee   fd       Zedee   fd       Zedee   fd       Zy)DocumentInformationa  
    A class representing the basic document metadata provided in a PDF File.
    This class is accessible through
    :py:class:`PdfReader.metadata<pypdf.PdfReader.metadata>`.

    All text properties of the document metadata have
    *two* properties, e.g. author and author_raw. The non-raw property will
    always return a ``TextStringObject``, making it ideal for a case where the
    metadata is being displayed. The raw property can sometimes return a
    ``ByteStringObject``, if pypdf was unable to decode the string's text
    encoding; this requires additional safety in the caller and therefore is not
    as commonly accessed.
    r;   Nc                 .    t        j                  |        y N)r'   __init__selfs    r@   rG   zDocumentInformation.__init__u   s    !!$'rB   keyc                 L    | j                  |d       }t        |t              r|S y rF   )get
isinstancer1   )rI   rJ   retvals      r@   	_get_textzDocumentInformation._get_textx   s%    #t$f./MrB   c                     | j                  t        j                        rN| j                  t        j                        xs- | j                  t        j                        j	                         S dS )z
        Read-only property accessing the document's title.

        Returns a ``TextStringObject`` or ``None`` if the title is not
        specified.
        N)rL   DITITLErO   
get_objectrH   s    r@   titlezDocumentInformation.title~   sP     xx! NN288$G(:(E(E(G	
 	
rB   c                 @    | j                  t        j                        S )z>The "raw" version of title; can return a ``ByteStringObject``.)rL   rQ   rR   rH   s    r@   	title_rawzDocumentInformation.title_raw   s     xx!!rB   c                 @    | j                  t        j                        S )z
        Read-only property accessing the document's author.

        Returns a ``TextStringObject`` or ``None`` if the author is not
        specified.
        )rO   rQ   AUTHORrH   s    r@   authorzDocumentInformation.author   s     ~~bii((rB   c                 @    | j                  t        j                        S )z?The "raw" version of author; can return a ``ByteStringObject``.)rL   rQ   rX   rH   s    r@   
author_rawzDocumentInformation.author_raw   s     xx		""rB   c                 @    | j                  t        j                        S )z
        Read-only property accessing the document's subject.

        Returns a ``TextStringObject`` or ``None`` if the subject is not
        specified.
        )rO   rQ   SUBJECTrH   s    r@   subjectzDocumentInformation.subject   s     ~~bjj))rB   c                 @    | j                  t        j                        S )z@The "raw" version of subject; can return a ``ByteStringObject``.)rL   rQ   r]   rH   s    r@   subject_rawzDocumentInformation.subject_raw        xx

##rB   c                 @    | j                  t        j                        S )ac  
        Read-only property accessing the document's creator.

        If the document was converted to PDF from another format, this is the
        name of the application (e.g. OpenOffice) that created the original
        document from which it was converted. Returns a ``TextStringObject`` or
        ``None`` if the creator is not specified.
        )rO   rQ   CREATORrH   s    r@   creatorzDocumentInformation.creator   s     ~~bjj))rB   c                 @    | j                  t        j                        S )z@The "raw" version of creator; can return a ``ByteStringObject``.)rL   rQ   rc   rH   s    r@   creator_rawzDocumentInformation.creator_raw   ra   rB   c                 @    | j                  t        j                        S )aI  
        Read-only property accessing the document's producer.

        If the document was converted to PDF from another format, this is the
        name of the application (for example, macOS Quartz) that converted it to
        PDF. Returns a ``TextStringObject`` or ``None`` if the producer is not
        specified.
        )rO   rQ   PRODUCERrH   s    r@   producerzDocumentInformation.producer   s     ~~bkk**rB   c                 @    | j                  t        j                        S )zAThe "raw" version of producer; can return a ``ByteStringObject``.)rL   rQ   rh   rH   s    r@   producer_rawz DocumentInformation.producer_raw   s     xx$$rB   c                 R    t        | j                  t        j                              S )z:Read-only property accessing the document's creation date.)r   rO   rQ   CREATION_DATErH   s    r@   creation_datez!DocumentInformation.creation_date   s     "$..1A1A"BCCrB   c                 @    | j                  t        j                        S )z
        The "raw" version of creation date; can return a ``ByteStringObject``.

        Typically in the format ``D:YYYYMMDDhhmmss[+Z-]hh'mm`` where the suffix
        is the offset from UTC.
        )rL   rQ   rm   rH   s    r@   creation_date_rawz%DocumentInformation.creation_date_raw   s     xx(())rB   c                 R    t        | j                  t        j                              S )z
        Read-only property accessing the document's modification date.

        The date and time the document was most recently modified.
        )r   rO   rQ   MOD_DATErH   s    r@   modification_datez%DocumentInformation.modification_date   s     "$.."=>>rB   c                 @    | j                  t        j                        S )z
        The "raw" version of modification date; can return a
        ``ByteStringObject``.

        Typically in the format ``D:YYYYMMDDhhmmss[+Z-]hh'mm`` where the suffix
        is the offset from UTC.
        )rL   rQ   rr   rH   s    r@   modification_date_rawz)DocumentInformation.modification_date_raw   s     xx$$rB   )r;   N)__name__
__module____qualname____doc__rG   strr   rO   propertyrT   rV   rY   r[   r^   r`   rd   rf   ri   rk   r   rn   rp   rs   ru    rB   r@   rD   rD   f   s   (S Xc]  
x} 
 
 "8C= " " ) ) ) #HSM # # *# * * $Xc] $ $ 	*# 	* 	* $Xc] $ $ 	+(3- 	+ 	+ %hsm % % Dx1 D D *8C= * * ?8H#5 ? ? %x} % %rB   rD   c                      e Zd ZU dZdZeed<   dZee	   ed<   dZ
eed<   eedefd              Zeedefd	              Zed
eeef   dee   fd       Zedededefd       Zeedee   fd              Zedee   fd       Zedee   fd       Ze	 	 dVdedee   f   dedee   f   deeef   fd       Zedee    fd       Z!dZ"ee#e$      ed<   defdZ%dede$fdZ&dede'eef   fdZ(edeeef   fd       Z)de*fdZ+	 	 dVdee,df   dee   deeef   fdZ-	 	 	 	 dWdee,   deeeef      d ee   d!ee#e      deeeef      f
d"Z.d#edefd$Z/d%ee,ef   deeef   d ed&ed!e#e   ddfd'Z0dee,ef   ded ed!e#e   ddf
d(Z1d ed%ed&eddfd)Z2dXd*edeeef   fd+Z3d%ee4eef   de#e$   fd,Z5edede6e7e8f   fd-       Z9e9jt                  d.edee6e$f   ddfd/       Z9ede;fd0       Z<	 dVd1ee   d2ee   de;fd3Z=edee*   fd4       Z>ed
edee?ef   dee   fd5       Z@d6e$dee   fd7ZAd8e6dee   fd9ZBd:ed;ee#eeCede?ef         de6fd<ZDd1edee6   fd=ZEede#e$   fd>       ZFede#e   fd?       ZGedee   fd@       ZHedeeI   fdA       ZJ	 	 	 	 dYdBedCedee$f   dDeeeef      d
ee   ddf
dEZK	 dXd6eee$ef   dFeddfdGZLdHedIedee   fdJZMdKedeeef   fdLZNedeeO   fdM       ZPeedefdN              ZQedeeeef      fdO       ZRedeSee#eT   f   fdP       ZUde#e   fdQZVdRede#eT   fdSZW	 dZdTee   deeeeTe#eT   f   f   fdUZXy)[PdfDocCommonzm
    Common functions from PdfWriter and PdfReader objects.

    This root class is strongly abstracted.
    FstrictN_encryption	_readonlyr;   c                      y rF   r|   rH   s    r@   root_objectzPdfDocCommon.root_object       	rB   c                      y rF   r|   rH   s    r@   
pdf_headerzPdfDocCommon.pdf_header  r   rB   indirect_referencec                      y rF   r|   rI   r   s     r@   rS   zPdfDocCommon.get_object       	rB   indirectobjc                      y rF   r|   )rI   r   r   s      r@   _replace_objectzPdfDocCommon._replace_object      rB   c                      y rF   r|   rH   s    r@   _infozPdfDocCommon._info  r   rB   c                 j    t               }| j                  y|j                  | j                         |S )a  
        Retrieve the PDF file's document information dictionary, if it exists.

        Note that some PDF files use metadata streams instead of document
        information dictionaries, and these metadata streams will not be
        accessed by this function.
        N)rD   r   update)rI   rN   s     r@   metadatazPdfDocCommon.metadata  s.     %&::djj!rB   c                      y rF   r|   rH   s    r@   xmp_metadatazPdfDocCommon.xmp_metadata*  r   rB   includeexcludec                      y)z
        Integration into Jupyter Notebooks.

        This method returns a dictionary that maps a mime-type to its
        representation.

        See https://ipython.readthedocs.io/en/stable/config/integrating.html
        Nr|   )rI   r   r   s      r@   _repr_mimebundle_zPdfDocCommon._repr_mimebundle_.  s     	rB   c                 V   | j                   j                  t        j                  d      }|y|j	                         }t        |t              s[t        |      }t        |d      r| j                  |j                  |       |S || j                   t        t        j                        <   |S )zCReturns the existing ViewerPreferences as an overloaded dictionary.Nr   )r   rL   CDVIEWER_PREFERENCESrS   rM   r3   hasattrr   r   r-   )rI   os     r@   viewer_preferenceszPdfDocCommon.viewer_preferences>  s       !6!6=9LLN!./!!$Aq./$$Q%9%91=  GH  B,A,A!BCrB   flattened_pagesc                     | j                   r| j                  d   d   S | j                  | j                  | j                         | j                  J t        | j                        S )a  
        Calculate the number of pages in this PDF file.

        Returns:
            The number of pages of the parsed PDF file.

        Raises:
            PdfReadError: if file is encrypted and restrictions prevent
                this action.

        /Pages/Count)is_encryptedr   r   _flattenr   lenrH   s    r@   get_num_pageszPdfDocCommon.get_num_pagesO  sb     ##H-h77##+dnn-''333t++,,rB   page_numberc                     | j                   | j                  | j                         | j                   J d       | j                   |   S )a?  
        Retrieve a page by number from this PDF file.
        Most of the time ``.pages[page_number]`` is preferred.

        Args:
            page_number: The page number to retrieve
                (pages begin at zero)

        Returns:
            A :class:`PageObject<pypdf._page.PageObject>` instance.

        zhint for mypy)r   r   r   )rI   r   s     r@   get_pagezPdfDocCommon.get_pagef  sH     'MM$..)##/@@/##K00rB   c                     t        t        | j                  d         dt        dt        dt        t
        t           t        f   ffd d      \  }}t        |t              sJ d       ||fS )z
        Retrieve the node and position within the /Kids containing the page.
        If page_number is greater than the number of pages, it returns the top node, -1.
        r   nodemir;   c                    t        t        | j                  dd            }| d   dk(  r|k(  r| dfS d |dz   fS |z
  |k\  r| 	k(  r	dfS d ||z   fS t        t        t        | d               D ]D  \  }}t        t
        |j                               } ||      \  }}||dk  r| |fc S ||fc S |}F t        d      )	Nr   r   z/Type/Page/Kidsr   z"Unexpectedly cannot find the node.)r   intrL   	enumerater#   r'   rS   r"   )
r   r   maidxkidnir   recursive_calltops
          r@   r   z6PdfDocCommon._get_page_in_node.<locals>.recursive_call  s     c488Ha01BG}'"$8OR!V|#b R'3;7NR"W}$%d;W&FG S+S^^-=>%c2.1=1u#Sy(a4K ABBrB   r   mypy)r   r'   r   r   r   r   r0   rM   )rI   r   r   r   r   r   s    `  @@r@   _get_page_in_nodezPdfDocCommon._get_page_in_nodex  s     #T%5%5h%?@	C"	C(+	C8I&+,	C2 #3*	c$ 019691SyrB   c                 "    | j                         S )zu
        A read-only dictionary which maps names to
        :class:`Destinations<pypdf.generic.Destination>`
        )_get_named_destinationsrH   s    r@   named_destinationszPdfDocCommon.named_destinations  s     ++--rB   c                    t               }t        j                  | j                  v rt	        | j                  t        j                     t
              rUt        t
        | j                  t        j                           }|j                  }t        j                  |v rt	        |t        j                     t
              rt        t
        |t        j                           }|j                  }t        j                  |v r#t        t         |t        j                           }|S t               }||t        t        j                        <   |S t        | d      rSt               }| j                  |      }||t        t        j                        <   ||t        t        j                        <   |S t        | d      rt               }| j                  |      }|| j                  t        t        j                        <   t               }| j                  |      }||t        t        j                        <   ||t        t        j                        <   |S )N_add_object)r#   CANAMESr   rM   r'   r   r   DESTSr-   r   r   )rI   
named_destnames	names_refdests	dests_refs         r@   get_named_dest_rootz PdfDocCommon.get_named_dest_root  s    ]
88t'''JRXX&(8-
 )4+;+;BHH+EFE00Ixx5 ZbhhAQ%R-uRXX?!44	88u$!%k5?!CJ& # "-J2<E*RXX./   }-(* ,,U3	.7j*+.8j*+  T=)$&E((/I5>DZ12$&E((/I*3E*RXX&'*4E*RXX&'rB   treerN   c                    |i }| j                   }t        j                  |v r"t        t        |t        j                           }nft        j
                  |v rTt        t        |t        j
                           }t        j                  |v r!t        t        |t        j                           }||S t        j                  |v rHt        t        |t        j                           D ]"  }| j                  |j                         |       $ |S t        j
                  |v rt        t        |t        j
                           }d}|t        |      k  rt        t        ||   j                               }|dz  }t        |t              sE	 ||   j                         }|dz  }t        |t              rd|v r|d   }ny| j!                  ||      }	|	|	||<   |t        |      k  r|S |j#                         D ]X  \  }
}|j                         }t        |t              rd|v r|d   j                         }n?| j!                  |
|      }	|	T|	||
<   Z |S # t        $ r Y |S w xY w)z
        Retrieve the named destinations present in the document.

        Args:
            tree:
            retval:

        Returns:
            A dictionary which maps names to
            :class:`Destinations<pypdf.generic.Destination>`.

        r   r   /D)r   r   r   r   r2   r   r'   PAKIDSr#   r   rS   r   rz   rM   
IndexError_build_destinationitems)rI   r   rN   catalogr   r   r   rJ   valuedestk__v__vals                r@   r   z$PdfDocCommon._get_named_destinations  s;   " >F&&G xx7"J(9:W$-wrxx/@A88u$
E"((O<D<M77d?Kbgg7 G,,S^^-=vFGF A XX)4>:EAc%j.3a 3 3 56Q!#s+!!H//1E Qe%56u} %d ..sE:#"&F3K# c%j.:  !JJL 	'Snn&c#34s{!$i224 ..sC8#"&F3K	' - " * -s   I 	IIfileobjstackc                    t        j                         }|j                  t        j                                |Li }| j                  }g }t
        j                  |v r)t        t        t           |t
        j                           ny|S |J dv rAt        t        d         }|D ]'  }|j                         }	| j                  |	||||       ) |S t        fd|D              r| j                  ||||       |S )a  
        Extract field data if this PDF contains interactive form fields.

        The *tree*, *retval*, *stack* parameters are for recursive use.

        Args:
            tree: Current object to parse.
            retval: In-progress list of fields.
            fileobj: A file object (usually a text file) to write
                a report to on all interactive form fields found.
            stack: List of already parsed objects.

        Returns:
            A dictionary where each key is a field name, and each
            value is a :class:`Field<pypdf.generic.Field>` object. By
            default, the mapping name is used for keys.
            ``None`` if form data could not be located.

        Nz/Fieldsc              3   &   K   | ]  }|v  
 y wrF   r|   ).0attrr   s     r@   	<genexpr>z*PdfDocCommon.get_fields.<locals>.<genexpr>A  s     ;$;   )FAattributes_dictr   r   r   r   	ACRO_FORMr   r   r2   r#   rS   _build_fieldany)
rI   r   rN   r   r   field_attributesr   fieldsffields
    `        r@   
get_fieldszPdfDocCommon.get_fields  s   4 --/ = M M OP>F&&GE||w&HZ0'",,2GH<M   +tI7F S!!%:JERS  ;*:;;dFG5EuMrB   parentc                    d|v rt        t        |d         S d|v rF| j                  t        t        |d               dz   t        t        |j	                  dd            z   S t        t        |j	                  dd            S )N/TM/Parent./T )r   rz   _get_qualified_field_namer'   rL   )rI   r   s     r@   r   z&PdfDocCommon._get_qualified_field_nameF  s    F?VE]++& ..)6)+<=  sFJJtR01	2 VZZb122rB   r   r   c                    t        fddD              ry | j                        }|r$| j                  ||       |j                  d       t	              ||<   ||   j
                  j                         }|j                  t        j                  d      dk(  r+|t        t        j                           ||   t        d      <   |j                  t        j                  d      dk(  rqd|v rmt        t        |d   d	   j                                     ||   t        d      <   d
||   d   vr||   t        d         j                  t        d
             nn|j                  t        j                  d      dk(  rJ|j                  t        j                   d      t        j"                  j$                  z  dk7  rg }t        |      ||   t        d      <   |j                  t        j&                  i       D ]f  }	|	j                         }	t        |	d   d	   j                               D ]  }
|
|vs|j                  |
        t        |      ||   t        d      <   h |j                  t        j                   d      t        j"                  j(                  z  dk7  r(d
||   d   v r||   d   ||   d   j+                  d
      = | j-                  |||       y )Nc              3   &   K   | ]  }|v 
 y wrF   r|   )r   r   r   s     r@   r   z,PdfDocCommon._build_field.<locals>.<genexpr>\  s     ;Tt5 ;r   )r   r   
r   /Chz	/_States_/Btnz/APz/Nz/Offr   )allr   _write_fieldwriter)   r   rS   rL   r   FTr-   Optr#   listkeysappendFfFfBitsRadioKidsNoToggleToOffindex_check_kids)rI   r   rN   r   r   r   rJ   r   stateskss    `         r@   r   zPdfDocCommon._build_fieldT  sp    ;];;,,U3gu.>?MM$ElsSk,,77977255"&36z"&&7I3JF3K
;/077255"'ESL3>SZ%**,-4F3K
;/0 VC[55sJ{34;;Jv<NOWWRUUB6)cggbeeQ.?"))//.QUV.V "F3>v3FF3K
;/0WWRWWb) KLLNahtn1134 )Aa() 8C67JsJ{34K q!BII$;$;;q@fSk+663K,VC[-E-K-KF-ST7rB   c                    ||v r#t        | j                  |       dt               y |j                  |       t        j
                  |v r=|t        j
                     D ]&  }|j                         }| j                  ||||       ( y y )Nz already parsed)r   r   rv   r   r   r   rS   r   )rI   r   rN   r   r   r   s         r@   r  zPdfDocCommon._check_kids~  s     5=11$78H( T77d?BGG} =nn&VWe<= rB   c                 X   t        j                         }|t        j                         z   }|D ]  }|t         j                  t         j                  fv r&||   }	 |t         j
                  k(  r,ddddd}||   |v rz|j                  | d|||       d       n\|t         j                  k(  r/	 ||   t         j                     }|j                  | d| d       n|j                  | d||    d        y # t        $ r ||   t         j                     }Y Vw xY w# t        $ r Y w xY w)NButtonTextChoice	Signature)r   /Txr   z/Sig: r   )r   
attributesr   r  AAr   r   ParentTMKeyErrorT)	rI   r   r   r   field_attributes_tupler   	attr_nametypesnames	            r@   r   zPdfDocCommon._write_field  sH   !#"%B%M%M%OO 	 + 	D  (.I255= !)%' +	E T{e+2eE$K6H5I&LMRYY&1$T{2551 MMYKr$r":;MMYKr%+b"AB5	, $ 1$T{24401
  s7   AD-C82D8DDDD	D)(D)full_qualified_namec           
      X   dt         dt        t        t        f   dt         fd}| j                         }|i S i }|j	                         D ]]  \  }}|j                  d      dk(  s|r|j                  d      ||<   2|j                  d      | |t        t         |d         |      <   _ |S )	a  
        Retrieve form fields from the document with textual data.

        Args:
            full_qualified_name: to get full name

        Returns:
            A dictionary. The key is the name of the form field,
            the value is the content of the field.

            If the document contains multiple form fields with the same name, the
            second and following will get the suffix .2, .3, ...

        r	  r   r;   c                     | |vr| S | dz   t        t        |D cg c]  }|j                  | dz         sd c}      dz         z   S c c}w )Nr   r      )rz   sum
startswith)r	  r   kks      r@   indexed_keyz6PdfDocCommon.get_form_text_fields.<locals>.indexed_key  sY     #6LRR]]1s75KqLMPQQRS Ms
   AA/FTr  z/Vr   )rz   r   r   r   r   rL   r   )rI   r  r$  
formfieldsffr   r   s          r@   get_form_text_fieldsz!PdfDocCommon.get_form_text_fields  s     	3 	S#X 	3 	 __&
I&,,. 	RLE5yy5(& %		$BuIBG))D/B{4U4[#92>?	R 	rB   c           
         dt         dt        dt        ffd	 t        t         |j                  j                               }t         |d            rt        d      g }|j                  d	d
      dk(  rQd|v r|d   j                         g}n| j                  D cg c]"  }|j                  |j                  dd
      v r|$ }}n|j                  dd      }|D ]  }|j                         }|j                  d	d
      dk(  s)d|vs.d|v r||d   j                         gz  }J|| j                  D cg c]"  }|j                  |j                  dd
      v r|$ c}z  } |D cg c]<  }t        |t              r|n'| j                  | j                  |j                           > c}S # t        $ r}t        d      |d}~ww xY wc c}w c c}w c c}w )a  
        Provides list of pages where the field is called.

        Args:
            field: Field Object, PdfObject or IndirectObject referencing a Field

        Returns:
            List of pages:
                - Empty list:
                    The field has no widgets attached
                    (either hidden field or ancestor field).
                - Single page list:
                    Page where the widget is present
                    (most common).
                - Multi-page list:
                    Field with multiple kids widgets
                    (example: radio buttons, field repeated on multiple pages).

        r   rJ   r;   c                 p    || v r| |   S d| v r( t        t        | d   j                               |      S y )Nr   )r   r'   rS   )r   rJ   _get_inheriteds     r@   r+  z<PdfDocCommon.get_pages_showing_field.<locals>._get_inherited  sG    cz3xc!%)3y>+D+D+FG  rB   zField type is invalidNr%  zField is not validz/Subtyper   z/Widgetz/Pz/Annotsr   r|   r   )r'   rz   r   r   r   rS   	Exception
ValueErrorr5   rL   pagesrM   r   _get_page_number_by_indirect)	rI   r   excretpkidsr	  xr+  s	           @r@   get_pages_showing_fieldz$PdfDocCommon.get_pages_showing_field  s   .	 0 	s 	s 		?)5+C+C+N+N+PQE >%7812299Z$	1u}T{--/0 "ZZ//155B3GG   99Wb)D LLNEE*b)Y6T]qy$ 2 2 455%)ZZ  ! 33quuY7KK     	
  !Z( **T>>q?S?STUW
 	
9  	?453>	? 

s*   (F+ 'G5'G'AG+	G4G  Gc                 z   d| j                   vry| j                   d   }t        |t              r|j                         }t        |t              rt        |      S t        |t              r0	 |dd \  }}|dd }t        |t        |            }t        d||      S y# t        $ r}t        d| d|       d}~ww xY w)z
        Property to access the opening destination (``/OpenAction`` entry in
        the PDF catalog). It returns ``None`` if the entry does not exist
        or is not set.

        Raises:
            Exception: If a destination is invalid.

        z/OpenActionNr   r   
OpenActionzInvalid Destination r  )r   rM   bytesdecoderz   r4   r#   r*   tupler&   r,  )rI   oapagetyparrayfitr0  s          r@   open_destinationzPdfDocCommon.open_destination$  s      0 00""=1b% Bb#'++K(DqG	c12#uU|,"<s;;   D"6rd"SE BCCDs   +.B 	B:$B55B:r   c                     t        d      )NzNo setter for open_destination)NotImplementedError)rI   r   s     r@   r@  zPdfDocCommon.open_destinationC  s    !"BCCrB   c                 "    | j                         S )z
        Read-only property for the outline present in the document
        (i.e., a collection of 'outline items' which are also known as
        'bookmarks').
        )_get_outlinerH   s    r@   outlinezPdfDocCommon.outlineG  s       ""rB   r   rE  c                    |g }| j                   }t        j                  |v rUt        t        |t        j                           }t        |t              r|S t        |      sd|v rt        t        |d         }| j                         | _	        ||S 	 | j                  |      }|r|j                  |       d|v r8g }| j                  t        t        |d         |       |r|j                  |       d|vr	 |S t        t        |d         }{)Nz/Firstz/Next)r   COOUTLINESr   r'   rM   r.   r5   r   _named_destinations_build_outline_itemr   rD  )rI   r   rE  r   linesoutline_objsub_outlines          r@   rD  zPdfDocCommon._get_outlineP  s    ?G&&G {{g%-wr{{/CDeZ0"N 'u-(e2C 0%/BD'+'C'C'ED$<N 2248K{+ 4)+!!$'7h"H+VNN;/d"  ($w-8D rB   c                 z    | j                   }t        j                  |v rt        d|t        j                           S y)uN  
        Read-only property for the list of threads.

        See §12.4.3 from the PDF 1.7 or 2.0 specification.

        It is an array of dictionaries with "/F" (the first bead in the thread)
        and "/I" (a thread information dictionary containing information about
        the thread, such as its title, author, and creation date) properties or
        None if there are no articles.

        Since PDF 2.0 it can also contain an indirect reference to a metadata
        stream containing information about the thread, such as its title,
        author, and creation date.
        r#   N)r   rG  THREADSr   )rI   r   s     r@   threadszPdfDocCommon.threadsy  s4      "":: wrzz':;;rB   c                      y rF   r|   r   s     r@   r/  z)PdfDocCommon._get_page_number_by_indirect  r   rB   r<  c                 8    | j                  |j                        S )a  
        Retrieve page number of a given PageObject.

        Args:
            page: The page to get page number. Should be
                an instance of :class:`PageObject<pypdf._page.PageObject>`

        Returns:
            The page number or None if page is not found

        )r/  r   )rI   r<  s     r@   get_page_numberzPdfDocCommon.get_page_number  s     001H1HIIrB   destinationc                 8    | j                  |j                        S )z
        Retrieve page number of a given Destination object.

        Args:
            destination: The destination to get page number.

        Returns:
            The page number or None if page is not found

        )r/  r<  )rI   rT  s     r@   get_destination_page_numberz(PdfDocCommon.get_destination_page_number  s     001A1ABBrB   rT   r>  c                    d\  }}t        |t        t        f      s t        |t              rt	        |      dk(  s|)t               }t        ||t        j                               S |dd \  }}|dd  }	 t        ||t        ||            S # t        $ rl t        d| d| t               | j                  r | j                  d   j                  }|
t               n|}t        ||t        j                               cY S w xY w)NNNr   r   )fit_typefit_argszUnknown destination:  )rM   r.   rz   r#   r   r&   r*   r?  r!   r   rv   r   r.  r   )rI   rT   r>  r<  r=  tmpr   s          r@   r   zPdfDocCommon._build_destination  s     	c uz3/05+.3u:?}<DudCGGI66a
ID#!"IE	I"5$S50QRR I!6ugQugFQ;;jjm6658[Z\c""5*<cggiHHIs   3B A2D ?D c                 `   d\  }}}	 t        d|d         }d|v rMt        t        |d         }t        t
        |t        j                           }|dk(  r6|t        j                     }n"d|v r|d   }t        |t              r	d	|v r|d	   }t        |t              r| j                  ||      }nt        |t              r+	 | j                  || j                  |   j                        }nU|| j                  ||      }n@| j                  rt        d
|      t        d|dt                | j                  |d       }|ryd|v r!t        d |d   D              |t        d      <   d|v r|d   |t        d      <   d|v r|d   |t        d      <   t#        |j%                  dd      dk\        |t        d      <   ||_        	 |j(                  |_        |S # t        $ r  | j                  rt        d|      d}Y w xY w# t        $ r | j                  |d       }Y w xY w# t*        $ r Y |S w xY w)N)NNNrz   z/Titlez(Outline Entry Missing /Title attribute: r   z/Az/GoToz/Destr   zUnexpected destination zRemoved unexpected destination z from destinationz/Cc              3   2   K   | ]  }t        |        y wrF   )r+   )r   cs     r@   r   z3PdfDocCommon._build_outline_item.<locals>.<genexpr>  s     <`PQ[^<`s   /Fr   r   z
/%is_open%)r   r  r   r!   r'   r-   r   SDrM   r#   r   rz   rI  
dest_arrayr   rv   r$   rL   r   r   AttributeError)rI   r   r   rT   outline_itemactionaction_types          r@   rJ  z PdfDocCommon._build_outline_item  sl   $4!e\	X/E 4<*DJ7Fz62E2G2G+HIKg%1334_=D$ 01ddlDzdK(225$?Lc"D#66433D9DD  \  225$?L{{"%<TH#EFF5dX=NO  225$?L t|1<<`UYZ^U_<`1`Z-.t| 26dZ-.4 6:(^Z125B1%*6LL12 !	.2.E.EL+ E  	{{"%MdX#VWWE	6  D#66udCDJ  		s5   G >)G?  H  %G<;G<?HH 	H-,H-c                 B    t        | j                  | j                        S )aW  
        Property that emulates a list of :class:`PageObject<pypdf._page.PageObject>`.
        This property allows to get a page or a range of pages.

        Note:
            For PdfWriter only: Provides the capability to remove a page/range of
            page from the list (using the del operator). Remember: Only the page
            entry is removed, as the objects beneath can be used elsewhere. A
            solution to completely remove them - if they are not used anywhere - is
            to write to a buffer/temporary file and then load it into a new
            PdfWriter.

        )r   r   r   rH   s    r@   r.  zPdfDocCommon.pages  s     D..>>rB   c                 x    t        t        | j                              D cg c]  }t        | |       c}S c c}w )z
        A list of labels for the pages in this document.

        This property is read-only. The labels are in the order that the pages
        appear in the document.
        )ranger   r.  page_index2page_label)rI   r   s     r@   page_labelszPdfDocCommon.page_labels,  s-     9>c$**o8NO1%dA.OOOs   7c                 x    	 t        t        | j                  t        j                           S # t
        $ r Y yw xY w)a  
        Get the page layout currently being used.

        .. list-table:: Valid ``layout`` values
           :widths: 50 200

           * - /NoLayout
             - Layout explicitly not specified
           * - /SinglePage
             - Show one page at a time
           * - /OneColumn
             - Show one column at a time
           * - /TwoColumnLeft
             - Show pages in two columns, odd-numbered pages on the left
           * - /TwoColumnRight
             - Show pages in two columns, odd-numbered pages on the right
           * - /TwoPageLeft
             - Show two pages at a time, odd-numbered pages on the left
           * - /TwoPageRight
             - Show two pages at a time, odd-numbered pages on the right
        N)r   r-   r   r   PAGE_LAYOUTr  rH   s    r@   page_layoutzPdfDocCommon.page_layout6  s5    .	
D$4$4R^^$DEE 		s   *- 	99c                 @    	 | j                   d   S # t        $ r Y yw xY w)a2  
        Get the page mode currently being used.

        .. list-table:: Valid ``mode`` values
           :widths: 50 200

           * - /UseNone
             - Do not show outline or thumbnails panels
           * - /UseOutlines
             - Show outline (aka bookmarks) panel
           * - /UseThumbs
             - Show page thumbnails panel
           * - /FullScreen
             - Fullscreen view
           * - /UseOC
             - Show Optional Content Group (OCG) panel
           * - /UseAttachments
             - Show attachments panel
        z	/PageModeN)r   r  rH   s    r@   	page_modezPdfDocCommon.page_modeR  s*    *	##K00 		s    		list_onlyr.  inheritc                 
   t        t        j                        t        t        j                        t        t        j                        t        t        j
                        f}|i }|M| j                  }|j                  d      j                         }t        |t              st        d      g | _        t        j                  |v r"t        t         |t        j                           }nt        j"                  |vrd}nd}|dk(  r{|D ]  }||v s||   ||<    t        t$        |t        j"                           D ]B  }	i }
t        |	t&              r|	|
d<   |	j                         }|s-	  | j(                  |||fi |
 D y|dk(  rft-        |j/                               D ]  \  }}||vs|||<    t1        | |      }|s|j3                  |       | j                  j5                  |       yy# t*        $ r t        d      w xY w)a  
        Prepare the document pages to ease searching

        Args:
            list_only: Will only list the pages within _flatten_pages.
            pages:
            inherit:
            indirect_reference: Used recursively to flatten the /Pages object.

        Nr   zInvalid object in /Pagesr   r   z7Maximum recursion depth reached during page flattening.)r-   PG	RESOURCESMEDIABOXCROPBOXROTATEr   rL   rS   rM   r'   r!   r   r   TYPEr   rz   r   r#   r,   r   RecursionErrorr   r   r   r   r   )rI   rr  r.  rs  r   inheritable_page_attributesr   tr   r<  addtr   attr_inr   page_objs                  r@   r   zPdfDocCommon._flattenl  s   $ r||$r{{#rzz"ryy!	'
# ?G= &&GKK)446Ee%56"#=>>#%D 77eS%.)AWWE!AA=3 05=$)$KGDM0 [%.9 dN315D-.oo'%igFF '\"&w}}"7 + %'%*E'N	+
 "$(:;H&   ''1 	 * *U s   )G--Hcleanc                 x   | j                   | j                  | j                         | j                   J t        |t              r3|j                         }t        |t              st        dt               y|}t        |t              s	 | j                   j                  |      }d|cxk  rt        | j                         k  sn t        dt               y| j                  |   j                  }| j                  |= |r|| j                  |t!                      yyy# t        $ r t        dt               Y yw xY w)a  
        Remove page from pages list.

        Args:
            page:
                * :class:`int`: Page number to be removed.
                * :class:`~pypdf._page.PageObject`: page to be removed. If the page appears many times
                  only the first one will be removed.
                * :class:`~pypdf.generic.IndirectObject`: Reference to page to be removed.

            clean: replace PageObject with NullObject to prevent annotations
                or destinations to reference a detached page.

        Nz(IndirectObject is not referencing a pagezCannot find page in pagesr   zPage number is out of range)r   r   r   rM   r,   rS   r   r   rv   r   r  r-  r   r.  r   r   r.   )rI   r<  r  r2  inds        r@   remove_pagezPdfDocCommon.remove_page  s   & 'MM$..)##///dN+!Aa,I8TD$$++11$7 T5C 4 4558(Cjj11JJtS_  jl3 %5  :HEs   
D D98D9numgenc                 8    t        |||       j                         S )a0  
        Used to ease development.

        This is equivalent to generic.IndirectObject(num,gen,self).get_object()

        Args:
            num: The object number of the indirect object.
            gen: The generation number of the indirect object.

        Returns:
            A PdfObject

        )r,   rS   )rI   r  r  s      r@   _get_indirect_objectz!PdfDocCommon._get_indirect_object  s     c3-88::rB   permissions_codec           	      t   t        ddd       t        j                  t        j                  t        j                  t        j
                  t        j                  t        j                  t        j                  t        j                  d}|j                         D ci c]  \  }}|||z  dk7   c}}S c c}}w )z>Take the permissions as an integer, return the allowed access.decode_permissionsuser_access_permissionsz5.0.0)old_namenew_name
removed_in)printmodifycopyannotationsformsaccessabilityassembleprint_high_qualityr   )r   r   PRINTMODIFYEXTRACTADD_OR_MODIFYFILL_FORM_FIELDSEXTRACT_TEXT_AND_GRAPHICSASSEMBLE_DOCPRINT_TO_REPRESENTATIONr   )rI   r  permissions_mappingrJ   flags        r@   r  zPdfDocCommon.decode_permissions  s     	#).	
 +00+22)110>>*;;2LL-::"7"O"O

 1668
T !D(A--
 	
 
s   B4c                 Z    | j                   yt        | j                   j                        S )zWGet the user access permissions for encrypted documents. Returns None if not encrypted.N)r   r   PrH   s    r@   r  z$PdfDocCommon.user_access_permissions  s*     #$T%5%5%7%788rB   c                      y)z
        Read-only boolean property showing whether this PDF file is encrypted.

        Note that this property, if true, will remain true even after the
        :meth:`decrypt()<pypdf.PdfReader.decrypt>` method is called.
        Nr|   rH   s    r@   r   zPdfDocCommon.is_encrypted  s     	rB   c                    d }i }| j                   }d|vs|d   sy t        t        |d         }d|v rt        t        |d         }t	        |      }|D ]l  }|}t        |      }t        |t              s!t        t        t           |j                               }|sIt        j                  |j                        }	|	||<   n |S )Nz	/AcroFormz/XFA)r   r   r2   r#   iternextrM   r,   r   r(   rS   zlib
decompress_data)
rI   r   rN   r   r   r   r   tagr   ess
             r@   xfazPdfDocCommon.xfa  s    %)!#""g%W[-AJ 45T>+tF|4FVA )Ga0 *=!>OE!__U[[9&(s) rB   c                 t    t        | j                         D ci c]  }|| j                  |f c}      S c c}w rF   )LazyDict_list_attachments_get_attachment_list)rI   r  s     r@   attachmentszPdfDocCommon.attachments5  sE     !224 t00$77
 	
s   5c                     | j                   }	 t        t        t        t        t        t        |d         d         d         }|D cg c]  }t        |t              s| }}|S # t        $ r g cY S w xY wc c}w )zw
        Retrieves the list of filenames of file attachments.

        Returns:
            list of filenames

        /Names/EmbeddedFiles)r   r   r#   r'   r  rM   rz   )rI   r   	filenamesr   attachments_namess        r@   r  zPdfDocCommon._list_attachments>  s     ""		$)78+<=>NO I )2H1Z35GQHH    	I	Hs   5A% A6A6%A32A3r  c                 T    | j                  |      |   }t        |t              r|S |gS rF   )_get_attachmentsrM   r   )rI   r  outs      r@   r  z!PdfDocCommon._get_attachment_listU  s-    ##D)$/c4 JurB   filenamec                    | j                   }	 t        t        t        t        t        t        |d         d         d         }i }t        t        |            D ]  }||   }t        |t              s|||k7  r!|}||dz      j                         }|d   d   j                         }	||v r1t        ||   t              s	||   g||<   ||   j                  |	       |	||<    |S # t        $ r i cY S w xY w)a  
        Retrieves all or selected file attachments of the PDF as a dictionary of file names
        and the file data as a bytestring.

        Args:
            filename: If filename is None, then a dictionary of all attachments
                will be returned, where the key is the filename and the value
                is the content. Otherwise, a dictionary with just a single key
                - the filename - and its content will be returned.

        Returns:
            dictionary of filename -> Union[bytestring or List[ByteString]]
            If the filename exists multiple times a list of the different versions will be provided.

        r  r  r   z/EFr`  )r   r   r#   r'   r  rj  r   rM   rz   rS   get_datar   r   )
rI   r  r   r  r  r   r   r  f_dictf_datas
             r@   r  zPdfDocCommon._get_attachments[  s$   $ ""		$)78+<=>NO I =?s9~& 	/A!A!S!'AM"1q5)446t,557;&%k$&7>-8->,?D)%,,V4(.K%	/ %  	I	s   5C& &C43C4rX  )NNNN)F)FNNNrF   )Yrv   rw   rx   ry   r   bool__annotations__r   r   r   r   r{   r   r'   r   rz   r   r   r   r,   r0   rS   r   r   rD   r   r8   r   r   r   r   r   r3   r   r   r	   r   r   r   r   r   r   r#   r   r2   r   r   r   r   r  r   r(  r)   r5  r&   r1   r%   r@  setterr6   rE  rD  rP  r.   r/  rS  rV  r/   r   rJ  r.  rl  ro  r7   rq  r   r  r  r  r   r  r   r  r
   r8  r  r  r  r  r|   rB   r@   r~   r~      s    FD(,K*%,It-    C    "'^(;"<	)	 
  Y 9   x 01    (#67   h~6    /3.2tXc]*+ tXc]*+ 
c3h	  H->$?   37OXd:./6-s -.1C 1J 1$%% 
$	%%N .DcN . . [  J )- $EJ$%E E 
c3h	EX &*+/!%+/0z"0 c3h(0 #	0
 Y(0 
$sCx.	!0d30@ 3S 3(8Z!112(8 S#X(8 	(8
 (8 I(8 
(8T=J 001= = 	=
 I= 
=&#C # #s #t #J% %cSVh %NE
5)^;<E
	j	E
N 	t["24DD	E < DU4k:+M%N DSW D D # # # QU'-.'@H'	'R +.  * "'c:~(M"N	# 
JJ J8C= JC{ CxPS} CII lND*FVVW
I 
I@I(8 IXk=R IV ?tJ' ? ?  PT#Y P P Xc]  6 8L1  6  ;?,07;F2F2 T+Z78F2 $sCx.)	F2
 %^4F2 
F2V *4C^34*4 *4 
	*4X; ;# ;(9:M ; 
 #
	c4i
6 92G)H 9 9 d    Xd38n-  . 
WS$u+%56 
 
!49 !. e  )-. .	c5U+,,	-.rB   r~   c                   Z    e Zd ZdededdfdZdedefdZdee   fdZde	fd	Z
defd
Zy)r  argskwr;   Nc                 $    t        |i || _        y rF   )dict	_raw_dict)rI   r  r  s      r@   rG   zLazyDict.__init__  s    t*r*rB   rJ   c                 N    | j                   j                  |      \  }} ||      S rF   )r  __getitem__)rI   rJ   funcargs       r@   r  zLazyDict.__getitem__  s$    NN..s3	cCyrB   c                 ,    t        | j                        S rF   )r  r  rH   s    r@   __iter__zLazyDict.__iter__  s    DNN##rB   c                 ,    t        | j                        S rF   )r   r  rH   s    r@   __len__zLazyDict.__len__  s    4>>""rB   c                 <    dt        | j                                dS )NzLazyDict(keys=))r   r   rH   s    r@   __str__zLazyDict.__str__  s    TYY[ 12!44rB   )rv   rw   rx   r   rG   rz   r  r   r  r   r  r  r|   rB   r@   r  r    sW    +c + + +s s $(3- $# #5 5rB   r  )Pr>   r  abcr   r   typingr   r   r   r   r	   r
   r   r   r   r   r   r   _pager   r   _page_labelsr   rk  _utilsr   r   r   	constantsr   r   r   r   r   r   r   r   rG  r   rQ   r   r   r   ru  r    r   errorsr!   r"   genericr#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r  r6   r7   xmpr8   r8  r   rA   rD   r~   r  r|   rB   r@   <module>r     s   >       $ + > 
 / . 
 " : 6 + , ,     * - %e %3 %5eCHo1E+F %L%* L%^T Tn$5wsCx  5rB   