a
    còfÀ  ã                   @   sÔ  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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/m0Z0 ddl1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@mAZAmBZBmCZC ddlDmEZEmFZF ddlGmHZH eIeJeeJeedf f dœdd„ZKG dd„ de6ƒZLG dd„ dƒZMG dd„ deeef ƒZNdS ) é    N)Úabstractmethod)Údatetime)
ÚAnyÚDictÚIterableÚIteratorÚListÚMappingÚOptionalÚTupleÚUnionÚcasté   )Ú
Encryption)Ú
PageObjectÚ_VirtualList)Úindex2label)Úb_Údeprecate_with_replacementÚlogger_warningÚparse_iso8824_date)ÚCatalogAttributes)ÚCatalogDictionary)ÚCheckboxRadioButtonAttributesÚGoToActionArgumentsÚUserAccessPermissions)ÚCore)ÚDocumentInformationAttributes)ÚFieldDictionaryAttributes)ÚPageAttributes)ÚPagesAttributes)ÚPdfReadError)ÚArrayObjectÚBooleanObjectÚByteStringObjectÚDestinationÚDictionaryObjectÚEncodedStreamObjectÚFieldÚFitÚFloatObjectÚIndirectObjectÚ
NameObjectÚ
NullObjectÚNumberObjectÚ	PdfObjectÚTextStringObjectÚ
TreeObjectÚViewerPreferencesÚcreate_string_object)ÚOutlineTypeÚPagemodeType)ÚXmpInformation.)ÚdÚsizeÚreturnc                 C   s4   |dkrt dƒ‚d|  } | dd … } t d| ¡d S )Né   zinvalid size in convert_to_ints           iøÿÿÿz>qr   )r!   ÚstructÚunpack)r7   r8   © r=   úN/home/httpd/docs/test/DocsMgr/lib/python3.9/site-packages/pypdf/_doc_common.pyÚconvert_to_int`   s
    r?   c                   @   sf  e Zd ZdZddœdd„Zeee dœdd„Zeee dœd	d
„ƒZ	eee dœdd„ƒZ
eee dœdd„ƒZeee dœdd„ƒZeee dœdd„ƒZeee dœdd„ƒZeee dœdd„ƒZeee dœdd„ƒZeee dœdd„ƒZeee dœdd„ƒZeee dœdd„ƒZeee dœdd „ƒZeee dœd!d"„ƒZeee dœd#d$„ƒZdS )%Ú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.
    N©r9   c                 C   s   t  | ¡ d S ©N)r&   Ú__init__©Úselfr=   r=   r>   rC   w   s    zDocumentInformation.__init__©Úkeyr9   c                 C   s   |   |d ¡}t|tƒr|S d S rB   )ÚgetÚ
isinstancer0   )rE   rG   Úretvalr=   r=   r>   Ú	_get_textz   s    
zDocumentInformation._get_textc                 C   s,   |   tj¡r(|  tj¡p*|   t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)rH   ÚDIÚTITLErK   Ú
get_objectrD   r=   r=   r>   Útitle€   s    

ÿýzDocumentInformation.titlec                 C   s   |   tj¡S )z>The "raw" version of title; can return a ``ByteStringObject``.)rH   rL   rM   rD   r=   r=   r>   Ú	title_rawŽ   s    zDocumentInformation.title_rawc                 C   s   |   tj¡S )z¡
        Read-only property accessing the document's author.

        Returns a ``TextStringObject`` or ``None`` if the author is not
        specified.
        )rK   rL   ÚAUTHORrD   r=   r=   r>   Úauthor“   s    zDocumentInformation.authorc                 C   s   |   tj¡S )z?The "raw" version of author; can return a ``ByteStringObject``.)rH   rL   rQ   rD   r=   r=   r>   Ú
author_raw�   s    zDocumentInformation.author_rawc                 C   s   |   tj¡S )z£
        Read-only property accessing the document's subject.

        Returns a ``TextStringObject`` or ``None`` if the subject is not
        specified.
        )rK   rL   ÚSUBJECTrD   r=   r=   r>   Úsubject¢   s    zDocumentInformation.subjectc                 C   s   |   tj¡S )z@The "raw" version of subject; can return a ``ByteStringObject``.)rH   rL   rT   rD   r=   r=   r>   Úsubject_raw¬   s    zDocumentInformation.subject_rawc                 C   s   |   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.
        )rK   rL   ÚCREATORrD   r=   r=   r>   Úcreator±   s    
zDocumentInformation.creatorc                 C   s   |   tj¡S )z@The "raw" version of creator; can return a ``ByteStringObject``.)rH   rL   rW   rD   r=   r=   r>   Úcreator_raw½   s    zDocumentInformation.creator_rawc                 C   s   |   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.
        )rK   rL   ÚPRODUCERrD   r=   r=   r>   ÚproducerÂ   s    
zDocumentInformation.producerc                 C   s   |   tj¡S )zAThe "raw" version of producer; can return a ``ByteStringObject``.)rH   rL   rZ   rD   r=   r=   r>   Úproducer_rawÎ   s    z DocumentInformation.producer_rawc                 C   s   t |  tj¡ƒS )z:Read-only property accessing the document's creation date.)r   rK   rL   ÚCREATION_DATErD   r=   r=   r>   Úcreation_dateÓ   s    z!DocumentInformation.creation_datec                 C   s   |   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.
        )rH   rL   r]   rD   r=   r=   r>   Úcreation_date_rawØ   s    z%DocumentInformation.creation_date_rawc                 C   s   t |  tj¡ƒS )z”
        Read-only property accessing the document's modification date.

        The date and time the document was most recently modified.
        )r   rK   rL   ÚMOD_DATErD   r=   r=   r>   Úmodification_dateâ   s    z%DocumentInformation.modification_datec                 C   s   |   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.
        )rH   rL   r`   rD   r=   r=   r>   Úmodification_date_rawë   s    	z)DocumentInformation.modification_date_raw)Ú__name__Ú
__module__Ú__qualname__Ú__doc__rC   Ústrr
   rK   ÚpropertyrO   rP   rR   rS   rU   rV   rX   rY   r[   r\   r   r^   r_   ra   rb   r=   r=   r=   r>   r@   h   s>   			r@   c                	   @   s(  e Zd ZU dZdZeed< dZee	 ed< e
eedœdd„ƒƒZe
eedœd	d
„ƒƒZeeeef ee dœdd„ƒZeeeedœdd„ƒZe
eee dœdd„ƒƒZe
ee dœdd„ƒZe
ee dœdd„ƒZedzedee f edee f eeef dœdd„ƒZe
ee dœdd„ƒZ dZ!ee"e#  ed< edœdd„Z$ee#dœd d!„Z%e
eeef dœd"d#„ƒZ&e'dœd$d%„Z(d{ee)df ee eeef d&œd'd(„Z*d|ee) eeeef  ee ee"e  eeeef  d)œd*d+„Z+eed,œd-d.„Z,ee)ef eeef eee"e dd/œd0d1„Z-ee)ef eee"e dd)œd2d3„Z.eeedd4œd5d6„Z/d}eeeef d7œd8d9„Z0ee1eef e"e# d:œd;d<„Z2e
ede3e4e5f dœd=d>„ƒZ6e6j7edee3e#f dd?œd@d>„ƒZ6e
e8dœdAdB„ƒZ9d~ee ee e8dCœdDdE„Z:e
ee' dœdFdG„ƒZ;eedee<ef ee dœdHdI„ƒZ=e#ee dJœdKdL„Z>e3ee dMœdNdO„Z?eee"ee@ede<ef   e3dPœdQdR„ZAeee3 dSœdTdU„ZBe
e"e# dœdVdW„ƒZCe
e"e dœdXdY„ƒZDe
ee dœdZd[„ƒZEe
eeF dœd\d]„ƒZGdedee#f eeeef  ee dd^œd_d`„ZHd€eee#ef eddaœdbdc„ZIeeee ddœdedf„ZJeeeef dgœdhdi„ZKe
eeL dœdjdk„ƒZMe
eedœdldm„ƒƒZNe
eeeef  dœdndo„ƒZOe
ePee"eQ f dœdpdq„ƒZRe"e dœdrds„ZSee"eQ dtœdudv„ZTd�ee eeeeQe"eQ f f dwœdxdy„ZUdS )‚ÚPdfDocCommonzm
    Common functions from PdfWriter and PdfReader objects.

    This root class is strongly abstracted.
    FÚstrictNÚ_encryptionrA   c                 C   s   d S rB   r=   rD   r=   r=   r>   Úroot_object  s    zPdfDocCommon.root_objectc                 C   s   d S rB   r=   rD   r=   r=   r>   Ú
pdf_header  s    zPdfDocCommon.pdf_header)Úindirect_referencer9   c                 C   s   d S rB   r=   ©rE   rn   r=   r=   r>   rN     s    zPdfDocCommon.get_object)ÚindirectÚobjr9   c                 C   s   d S rB   r=   )rE   rp   rq   r=   r=   r>   Ú_replace_object  s    zPdfDocCommon._replace_objectc                 C   s   d S rB   r=   rD   r=   r=   r>   Ú_info  s    zPdfDocCommon._infoc                 C   s$   t ƒ }| jdu rdS | | 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)r@   rs   Úupdate)rE   rJ   r=   r=   r>   Úmetadata  s
    	
zPdfDocCommon.metadatac                 C   s   d S rB   r=   rD   r=   r=   r>   Úxmp_metadata*  s    zPdfDocCommon.xmp_metadata)ÚincludeÚexcluder9   c                 C   s   dS )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=   )rE   rw   rx   r=   r=   r>   Ú_repr_mimebundle_.  s    zPdfDocCommon._repr_mimebundle_c                 C   sd   | j  tjd¡}|du rdS | ¡ }t|tƒs`t|ƒ}t|dƒrP|  |j	|¡ n|| j t
tjƒ< |S )zCReturns the existing ViewerPreferences as an overloaded dictionary.Nrn   )rl   rH   ÚCDZVIEWER_PREFERENCESrN   rI   r2   Úhasattrrr   rn   r,   )rE   Úor=   r=   r>   Úviewer_preferences>  s    

zPdfDocCommon.viewer_preferencesÚflattened_pagesc                 C   sB   | j r| jd d S | jdu r&|  ¡  | jdus4J ‚t| jƒS d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ú/CountN)Úis_encryptedrl   r~   Ú_flattenÚlenrD   r=   r=   r>   Úget_num_pagesO  s    
zPdfDocCommon.get_num_pages)Úpage_numberr9   c                 C   s.   | j du r|  ¡  | j dus$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.
        Nzhint for mypy)r~   r‚   )rE   r…   r=   r=   r>   Úget_pagee  s    
zPdfDocCommon.get_pagec                 C   s   |   ¡ S )zu
        A read-only dictionary which maps names to
        :class:`Destinations<pypdf.generic.Destination>`
        )Ú_get_named_destinationsrD   r=   r=   r>   Únamed_destinationsv  s    zPdfDocCommon.named_destinationsc                 C   s2  t ƒ }tj| jv rÖt| jtj tƒrÖtt| jtj ƒ}|j}tj|v ržt|tj tƒržtt|tj ƒ}|j}tj|v rˆtt |tj ƒ}qÔt ƒ }||t	tjƒ< n6t
| dƒrÔtƒ }|  |¡}||t	tjƒ< ||t	tjƒ< nXt
| dƒ�r.tƒ }|  |¡}|| jt	tjƒ< tƒ }|  |¡}||t	tjƒ< ||t	tjƒ< |S )NÚ_add_object)r"   ÚCAÚNAMESrl   rI   r&   r   rn   ÚDESTSr,   r{   r‰   )rE   Z
named_destÚnamesZ	names_refZdestsZ	dests_refr=   r=   r>   Úget_named_dest_root~  s6    ÿ




z PdfDocCommon.get_named_dest_root)ÚtreerJ   r9   c                 C   sÐ  |du rbi }| j }tj|v r.tt|tj ƒ}n4tj|v rbtt|tj ƒ}tj|v rbtt|tj ƒ}|du rn|S tj|v r¢tt	|tj ƒD ]}|  
| ¡ |¡ qˆ�n*tj|v �rjtt|tj ƒ}d}|t|ƒk �rÌtt||  ¡ ƒ}|d7 }t|tƒsöqÂz||  ¡ }W n t�y    Y �qÌY n0 |d7 }t|tƒ�rJd|v rÂ|d }nqÂ|  ||¡}	|	durÂ|	||< qÂnb| ¡ D ]X\}
}| ¡ }t|tƒ�rªd|v �rr|d  ¡ }n�qr|  |
|¡}	|	du�rr|	||
< �qr|S )zü
        Retrieve the named destinations present in the document.

        Args:
            tree:
            retval:

        Returns:
            A dictionary which maps names to
            :class:`Destinations<pypdf.generic.Destination>`.
        Nr   r   ú/D)rl   rŠ   rŒ   r   r1   r‹   r&   ÚPAÚKIDSr"   r‡   rN   rƒ   rg   rI   Ú
IndexErrorÚ_build_destinationÚitems)rE   r�   rJ   Úcatalogr�   ÚkidÚirG   ÚvalueÚdestZk__Zv__Úvalr=   r=   r>   r‡   ¡  sX    







z$PdfDocCommon._get_named_destinations)r�   rJ   ÚfileobjÚstackr9   c           
         sÐ   t  ¡ }| t ¡ ¡ |du rPi }| j}g }tj|v rLttt	 |tj ƒ‰ ndS ˆ du r\|S |dushJ ‚dˆ v r¤tt
ˆ d ƒ}|D ]}| ¡ }	|  |	||||¡ q‚n(t‡ fdd„|D ƒƒrÌ|  ˆ ||||¡ |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   s   | ]}|ˆ v V  qd S rB   r=   ©Ú.0Úattr©r�   r=   r>   Ú	<genexpr>  ó    z*PdfDocCommon.get_fields.<locals>.<genexpr>)ÚFAZattributes_dictrt   r   rl   rz   Z	ACRO_FORMr   r
   r1   r"   rN   Ú_build_fieldÚany)
rE   r�   rJ   rœ   r�   Úfield_attributesr–   ÚfieldsÚfÚfieldr=   r¡   r>   Ú
get_fieldsê  s(    
zPdfDocCommon.get_fields)Úparentr9   c                 C   s^   d|v rt t|d ƒS d|v rH|  t t|d ƒ¡d t t| dd¡ƒ S t t| dd¡ƒS d S )Nú/TMú/ParentÚ.ú/TÚ )r   rg   Ú_get_qualified_field_namer&   rH   )rE   r¬   r=   r=   r>   r²     s    ÿýüÿz&PdfDocCommon._get_qualified_field_name)rª   rJ   rœ   r§   r�   r9   c                    sø  t ‡ fdd„dD ƒƒrd S |  ˆ ¡}|r@|  |ˆ |¡ | d¡ tˆ ƒ||< || j ¡ }| tj	d¡dkr†|t
tjƒ || t
dƒ< | tj	d¡dkrðd	|v rðtt|d	 d
  ¡ ƒƒ|| t
dƒ< d|| d vrî|| t
dƒ  t
dƒ¡ nô| tj	d¡dk�rä| tjd¡tjj@ dk�räg }t|ƒ|| t
dƒ< | tji ¡D ]R}	|	 ¡ }	t|	d	 d
  ¡ ƒD ]}
|
|v�rf| |
¡ �qft|ƒ|| t
dƒ< �qF| tjd¡tjj@ dk�räd|| d v �rä|| d || d  d¡= |  ˆ |||¡ d S )Nc                 3   s   | ]}|ˆ vV  qd S rB   r=   rž   ©rª   r=   r>   r¢   1  r£   z,PdfDocCommon._build_field.<locals>.<genexpr>)r°   r­   Ú
r±   ú/Chz	/_States_ú/Btnz/APz/Nz/Offr   )Úallr²   Ú_write_fieldÚwriter(   rn   rN   rH   r¤   ÚFTr,   ÚOptr"   ÚlistÚkeysÚappendZFfZFfBitsZRadioÚKidsZNoToggleToOffÚindexÚ_check_kids)rE   rª   rJ   rœ   r§   r�   rG   rq   ZstatesÚkÚsr=   r³   r>   r¥   )  s>    

ÿ0
ÿþzPdfDocCommon._build_fieldc                 C   sb   ||v r"t |  |¡› d�tƒ d S | |¡ tj|v r^|tj D ]}| ¡ }|  ||||¡ q@d S )Nz already parsed)r   r²   rc   r¾   r‘   r’   rN   r«   )rE   r�   rJ   rœ   r�   r—   r=   r=   r>   rÁ   S  s    ÿ

zPdfDocCommon._check_kids)rœ   rª   r§   r9   c           	   	   C   s
  t  ¡ }|t ¡  }|D ]ì}|t jt jfv r.q|| }z¸|t jkr|dddddœ}|| |v rì| |› d|||  › d�¡ np|t jkrÒz|| t j }W n  t	y¸   || t j
 }Y n0 | |› d|› d�¡ n| |› d|| › d�¡ W q t	�y   Y q0 qd S )NZButtonÚTextZChoiceÚ	Signature)r¶   ú/Txrµ   z/Sigú: r´   )r¤   Ú
attributesr   r¿   ZAArº   r¹   ZParentZTMÚKeyErrorÚT)	rE   rœ   rª   r§   Zfield_attributes_tupler    Ú	attr_nameÚtypesÚnamer=   r=   r>   r¸   f  s8    
ÿþ
ü 
zPdfDocCommon._write_field)Úfull_qualified_namer9   c                 C   sˆ   t tttf t dœdd„}|  ¡ }|du r.i S i }| ¡ D ]H\}}| d¡dkr:|rd| d¡||< q:| d¡||tt |d ƒ|ƒ< q:|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¨   r9   c                    s6   ˆ |vrˆ S ˆ d t t‡ fdd„|D ƒƒd ƒ S d S )Nr¯   c                    s   g | ]}|  ˆ d  ¡rd‘qS )r¯   r   )Ú
startswith)rŸ   Úkk©rÂ   r=   r>   Ú
<listcomp>¡  r£   zJPdfDocCommon.get_form_text_fields.<locals>.indexed_key.<locals>.<listcomp>é   )rg   Úsum)rÂ   r¨   r=   rÑ   r>   Úindexed_keyš  s    ÿþÿz6PdfDocCommon.get_form_text_fields.<locals>.indexed_keyNú/FTrÆ   z/Vr°   )rg   r   r   r«   r•   rH   r   )rE   rÎ   rÕ   Z
formfieldsÚffrª   r™   r=   r=   r>   Úget_form_text_fields‹  s     z!PdfDocCommon.get_form_text_fields)rª   r9   c              
      s0  t ttdœ‡ fdd„‰ ztt ˆj ¡ ƒ‰W n. tyX } ztdƒ|‚W Y d}~n
d}~0 0 ˆ ˆdƒdu rptdƒ‚g }ˆ dd	¡d
kr²dˆv rœˆd  ¡ g}n‡fdd„ˆj	D ƒ}nlˆ dd¡}|D ]Z‰ˆ ¡ ‰ˆ dd	¡d
krÂdˆvrÂdˆv �r|ˆd  ¡ g7 }qÂ|‡fdd„ˆj	D ƒ7 }qÂ‡fdd„|D ƒS )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).
        )rq   rG   r9   c                    s8   || v r| | S d| v r0ˆ t t| d  ¡ ƒ|ƒS d S d S )Nr®   )r   r&   rN   )rq   rG   )Ú_get_inheritedr=   r>   rÙ   Ç  s    ÿz<PdfDocCommon.get_pages_showing_field.<locals>._get_inheritedzfield type is invalidNrÖ   zfield is not validz/Subtyper±   z/Widgetz/Pc                    s"   g | ]}ˆ j | d d¡v r|‘qS ©z/Annotsr±   ©rn   rH   ©rŸ   Úpr³   r=   r>   rÒ   Ý  s   þz8PdfDocCommon.get_pages_showing_field.<locals>.<listcomp>z/Kidsr=   r°   c                    s"   g | ]}ˆ j | d d¡v r|‘qS rÚ   rÛ   rÜ   rÑ   r=   r>   rÒ   ë  s   þc                    s,   g | ]$}t |tƒr|nˆ jˆ  |j¡ ‘qS r=   )rI   r   ÚpagesÚ_get_page_number_by_indirectrn   )rŸ   ÚxrD   r=   r>   rÒ   ð  s   þÿ)
r&   rg   r   r   rn   rN   Ú	ExceptionÚ
ValueErrorrH   rÞ   )rE   rª   ÚexcÚretZkidsr=   )rÙ   rª   rÂ   rE   r>   Úget_pages_showing_field±  s4    
 
þ
þ

üz$PdfDocCommon.get_pages_showing_fieldc              
   C   sÂ   d| j vrdS | j d }t|tƒr*| ¡ }t|tƒr<t|ƒS t|tƒrºz8|dd… \}}|dd… }t|t|ƒƒ}t	d||ƒW S  t
y¶ } z t
d|› d|› �ƒ‚W Y d}~q¾d}~0 0 ndS dS )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Ó   Z
OpenActionzInvalid Destination rÇ   )rl   rI   ÚbytesÚdecoderg   r3   r"   r)   Útupler%   rá   )rE   ZoaÚpageÚtypÚarrayÚfitrã   r=   r=   r>   Úopen_destination÷  s     




,zPdfDocCommon.open_destination)rš   r9   c                 C   s   t dƒ‚d S )Nzno setter for open_destination)ÚNotImplementedError)rE   rš   r=   r=   r>   rí     s    c                 C   s   |   ¡ 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_outlinerD   r=   r=   r>   Úoutline  s    zPdfDocCommon.outline)Únoderð   r9   c                 C   sÔ   |d u rbg }| j }tj|v rXtt|tj ƒ}t|tƒr:|S |d urXd|v rXtt|d ƒ}|  ¡ | _|d u rn|S |  	|¡}|r†| 
|¡ d|v r¶g }|  tt|d ƒ|¡ |r¶| 
|¡ d|vrÀqÐtt|d ƒ}qn|S )Nz/Firstz/Next)rl   ÚCOZOUTLINESr   r&   rI   r-   r‡   Ú_namedDestsÚ_build_outline_itemr¾   rï   )rE   rñ   rð   r–   ÚlinesZoutline_objZsub_outliner=   r=   r>   rï   "  s0    





zPdfDocCommon._get_outlinec                 C   s(   | j }tj|v r td|tj ƒS dS dS )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)rl   rò   ZTHREADSr   )rE   r–   r=   r=   r>   ÚthreadsK  s    
zPdfDocCommon.threadsc                 C   s   d S rB   r=   ro   r=   r=   r>   rß   a  s    z)PdfDocCommon._get_page_number_by_indirect)ré   r9   c                 C   s   |   |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ß   rn   )rE   ré   r=   r=   r>   Úget_page_numberg  s    zPdfDocCommon.get_page_number)Údestinationr9   c                 C   s   |   |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é   )rE   rø   r=   r=   r>   Úget_destination_page_numbert  s    
z(PdfDocCommon.get_destination_page_number)rO   rë   r9   c                 C   sà   d\}}t |ttfƒs4t |tƒr,t|ƒdks4|d u rJtƒ }t||t ¡ ƒS |dd… \}}|dd … }zt||t||d�ƒW S  tyÚ   t	d|› d|› �t
ƒ | jr¨‚ | jd j}|d u rÂtƒ n|}t||t ¡ ƒ Y S 0 d S )N)NNr   rÓ   )Zfit_typeZfit_argszUnknown destination: ú )rI   r-   rg   r"   rƒ   r%   r)   rì   r!   r   rc   rj   rÞ   rn   )rE   rO   rë   ré   rê   Útmprn   r=   r=   r>   r”   €  s,    	ÿþ
þýzPdfDocCommon._build_destination)rñ   r9   c                 C   s  d\}}}zt d|d ƒ}W n* tyF   | jr>td|›�ƒ‚d}Y n0 d|v r‚t t|d ƒ}t t|tj ƒ}|dkr¬|tj }n*d|v r¬|d }t	|tƒr¬d	|v r¬|d	 }t	|t
ƒrÄ|  ||¡}n–t	|tƒ�rz|  || j| j¡}W n  t�y   |  |d ¡}Y n0 nN|d u �r$|  ||¡}n6| j�r<td
|›�ƒ‚ntd|›d�tƒ |  |d ¡}|�rØd|v �rˆt
dd„ |d D ƒƒ|tdƒ< d|v �r¢|d |tdƒ< d|v �r¼|d |tdƒ< t| dd¡dkƒ|tdƒ< ||_z|j|_W n t�yþ   Y n0 |S )N)NNNrg   z/Titlez(Outline Entry Missing /Title attribute: r±   z/Az/GoToz/Destr�   zUnexpected destination zRemoved unexpected destination z from destinationz/Cc                 s   s   | ]}t |ƒV  qd S rB   )r*   )rŸ   Úcr=   r=   r>   r¢   ×  r£   z3PdfDocCommon._build_outline_item.<locals>.<genexpr>ú/Fr€   r   z
/%is_open%)r   rÉ   rj   r!   r&   r,   r   ÚSÚDrI   r"   r”   rg   ró   Z
dest_arrayr   rc   r#   rH   rñ   rn   ÚAttributeError)rE   rñ   rš   rO   Zoutline_itemÚactionZaction_typer=   r=   r>   rô      sd    


ÿ

þ


ÿz PdfDocCommon._build_outline_itemc                 C   s   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†   rD   r=   r=   r>   rÞ   ë  s    zPdfDocCommon.pagesc                    s   ‡ fdd„t tˆ jƒƒD ƒS )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.
        c                    s   g | ]}t ˆ |ƒ‘qS r=   )Úpage_index2page_label)rŸ   r˜   rD   r=   r>   rÒ     r£   z,PdfDocCommon.page_labels.<locals>.<listcomp>)Úrangerƒ   rÞ   rD   r=   rD   r>   Úpage_labelsü  s    zPdfDocCommon.page_labelsc                 C   s.   zt t| jtj ƒW S  ty(   Y dS 0 dS )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,   rl   rz   ZPAGE_LAYOUTrÉ   rD   r=   r=   r>   Úpage_layout  s    zPdfDocCommon.page_layoutc                 C   s&   z| j d W S  ty    Y dS 0 dS )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)rl   rÉ   rD   r=   r=   r>   Ú	page_mode"  s    zPdfDocCommon.page_mode)rÞ   Úinheritrn   r9   c                 C   s\  t tjƒt tjƒt tjƒt tjƒf}|d u r0i }|d u r^| j}|d  ¡ }t|t	ƒsXJ ‚g | _
tj|v rztt|tj ƒ}ntj|vrŠd}nd}|dk�r|D ]}||v rœ|| ||< qœtt|tj ƒD ]:}i }	t|tƒrà||	d< | ¡ }
|
rÆ| j|
|fi |	¤Ž qÆnT|dk�rXt| ¡ ƒD ]\}}||v�r|||< �qt| |ƒ}| |¡ | j
 |¡ d S )Nr   z/Pagern   )r,   ÚPGZ	RESOURCESZMEDIABOXZCROPBOXZROTATErl   rN   rI   r&   r~   r‘   ZTYPEr   rg   r’   r"   r+   r‚   r¼   r•   r   rt   r¾   )rE   rÞ   r  rn   Zinheritable_page_attributesr–   Útr    ré   Zaddtrq   Zattr_inr™   Zpage_objr=   r=   r>   r‚   <  sF    ü







zPdfDocCommon._flatten)ré   Úcleanr9   c                 C   sä   | j du r|  ¡  | j dus J ‚t|tƒrN| ¡ }t|tƒsJtdtƒ dS |}t|tƒsˆz| j  	|¡}W n t
y†   tdtƒ Y dS 0 d|  kr¢t| j ƒk s²n tdtƒ dS | j| j}| j|= |rà|durà|  |tƒ ¡ dS )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‚   rI   r+   rN   r   r   rc   ÚintrÀ   râ   rƒ   rÞ   rn   rr   r-   )rE   ré   r
  rÝ   Úindr=   r=   r>   Úremove_pager  s,    






zPdfDocCommon.remove_page)ÚnumÚgenr9   c                 C   s   t ||| ƒ ¡ S )a/  
        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+   rN   )rE   r  r  r=   r=   r>   Ú_get_indirect_object�  s    z!PdfDocCommon._get_indirect_object)Úpermissions_coder9   c              	      sJ   t dddd� tjtjtjtjtjtjtjtj	dœ}‡ fdd„| 
¡ D ƒS )z>Take the permissions as an integer, return the allowed access.Údecode_permissionsÚuser_access_permissionsz5.0.0)Zold_nameÚnew_nameZ
removed_in)ÚprintÚmodifyÚcopyÚannotationsZformsZaccessabilityÚassembleZprint_high_qualityc                    s   i | ]\}}|ˆ |@ d k“qS )r   r=   )rŸ   rG   Úflag©r  r=   r>   Ú
<dictcomp>Â  s   ÿz3PdfDocCommon.decode_permissions.<locals>.<dictcomp>)r   r   ZPRINTZMODIFYZEXTRACTZADD_OR_MODIFYZFILL_FORM_FIELDSZEXTRACT_TEXT_AND_GRAPHICSZASSEMBLE_DOCZPRINT_TO_REPRESENTATIONr•   )rE   r  Zpermissions_mappingr=   r  r>   r  ¬  s"    ý÷
þzPdfDocCommon.decode_permissionsc                 C   s   | j du rdS t| j jƒS )zWGet the user access permissions for encrypted documents. Returns None if not encrypted.N)rk   r   ÚPrD   r=   r=   r>   r  Ç  s    
z$PdfDocCommon.user_access_permissionsc                 C   s   dS )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=   rD   r=   r=   r>   r�   Î  s    	zPdfDocCommon.is_encryptedc           
      C   s    d }i }| j }d|vs|d s"d S tt|d ƒ}d|v rœtt|d ƒ}t|ƒ}|D ]H}|}t|ƒ}t|tƒrRttt	 | 
¡ ƒ}|rRt t|jƒ¡}	|	||< qR|S )Nz	/AcroFormz/XFA)rl   r   r1   r"   ÚiterÚnextrI   r+   r
   r'   rN   ÚzlibÚ
decompressr   Ú_data)
rE   r�   rJ   r–   r¨   r˜   r©   Útagrª   Úesr=   r=   r>   ÚxfaÙ  s$    

zPdfDocCommon.xfac                    s   t ‡ fdd„ˆ  ¡ D ƒƒS )Nc                    s   i | ]}|ˆ j |f“qS r=   )Ú_get_attachment_list)rŸ   rÍ   rD   r=   r>   r  ô  s   ÿz,PdfDocCommon.attachments.<locals>.<dictcomp>)ÚLazyDictÚ_list_attachmentsrD   r=   rD   r>   Úattachmentsñ  s
    
þÿzPdfDocCommon.attachmentsc                 C   sV   | j }z&tttttt|d ƒd ƒd ƒ}W n tyB   g  Y S 0 dd„ |D ƒ}|S )zv
        Retrieves the list of filenames of file attachments.

        Returns:
            list of filenames
        ú/Namesú/EmbeddedFilesc                 S   s   g | ]}t |tƒr|‘qS r=   )rI   rg   )rŸ   r©   r=   r=   r>   rÒ     r£   z2PdfDocCommon._list_attachments.<locals>.<listcomp>)rl   r   r"   r&   rÉ   )rE   r–   Ú	filenamesZattachments_namesr=   r=   r>   r(  ú  s    þýþ
zPdfDocCommon._list_attachments)rÍ   r9   c                 C   s"   |   |¡| }t|tƒr|S |gS rB   )Ú_get_attachmentsrI   r¼   )rE   rÍ   Úoutr=   r=   r>   r&    s    
z!PdfDocCommon._get_attachment_list)Úfilenamer9   c           
      C   sâ   | j }z&tttttt|d ƒd ƒd ƒ}W n tyB   i  Y S 0 i }tt|ƒƒD ]ˆ}|| }t|tƒrT|dur|||kr|qT|}||d   	¡ }|d d  
¡ }	||v rÔt|| tƒsÄ|| g||< ||  |	¡ qT|	||< qT|S )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+  Nr   z/EFrý   )rl   r   r"   r&   rÉ   r  rƒ   rI   rg   rN   Úget_datar¼   r¾   )
rE   r/  r–   r,  r)  r˜   r©   rÍ   Zf_dictZf_datar=   r=   r>   r-    s8    þýþ


zPdfDocCommon._get_attachments)NN)NN)NNNN)F)NN)NNN)F)N)Vrc   rd   re   rf   rj   ÚboolÚ__annotations__rk   r
   r   rh   r   r&   rl   rg   rm   r   r  r+   r/   rN   rr   rs   r@   ru   r6   rv   r   r   r   ry   r2   r}   r~   r   r   r„   r†   rˆ   r"   rŽ   r1   r‡   r«   r²   r¥   rÁ   r¸   rØ   r(   rå   r%   r0   r$   rí   Úsetterr4   rð   rï   rö   r-   rß   r÷   rù   r.   r”   rô   rÞ   r  r  r5   r  r‚   r  r  r  r   r  r�   r%  r	   ræ   r)  r(  r&  r-  r=   r=   r=   r>   ri   ÷   s&  

þ  ý
ü%  ý

üK    û
ú1

ù,
ú%'þFþ	 ÿþ)þÿÿø K	   üû9 ýü+
þ	 ÿþri   c                   @   s\   e Zd Zeeddœdd„Zeedœdd„Zee dœd	d
„Ze	dœdd„Z
edœdd„ZdS )r'  N)ÚargsÚkwr9   c                 O   s   t |i |¤Ž| _d S rB   )ÚdictÚ	_raw_dict)rE   r4  r5  r=   r=   r>   rC   G  s    zLazyDict.__init__rF   c                 C   s   | j  |¡\}}||ƒS rB   )r7  Ú__getitem__)rE   rG   ÚfuncÚargr=   r=   r>   r8  J  s    zLazyDict.__getitem__rA   c                 C   s
   t | jƒS rB   )r  r7  rD   r=   r=   r>   Ú__iter__N  s    zLazyDict.__iter__c                 C   s
   t | jƒS rB   )rƒ   r7  rD   r=   r=   r>   Ú__len__Q  s    zLazyDict.__len__c                 C   s   dt |  ¡ ƒ› d�S )NzLazyDict(keys=ú))r¼   r½   rD   r=   r=   r>   Ú__str__T  s    zLazyDict.__str__)rc   rd   re   r   rC   rg   r8  r   r;  r  r<  r>  r=   r=   r=   r>   r'  F  s
   r'  )Or;   r   Úabcr   r   Útypingr   r   r   r   r   r	   r
   r   r   r   rk   r   Z_pager   r   Z_page_labelsr   r  Ú_utilsr   r   r   r   Ú	constantsr   rŠ   r   rz   r   r   r   r   rò   r   rL   r   r¤   r   r  r    r‘   Úerrorsr!   Zgenericr"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   rÌ   r4   r5   Úxmpr6   ræ   r  r?   r@   ri   r'  r=   r=   r=   r>   Ú<module>   sB   0P"         W