
    @wgk                    l   d dl Z d dlZd dlZd dlZd dlZd dl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mZ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mZ dd	l m!Z!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/m0Z0m1Z1m2Z2m3Z3m4Z4 ddl5m6Z7 ddl5m8Z9 ddl5m:Z:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@mAZA ddl5mBZC ddl5mDZE ddl5mFZG ddl5mHZI ddl5mJZK ddlLmMZM ddlNmOZOmPZPmQZQmRZRmSZSmTZTmUZUmVZVmWZWmXZXmYZYmZZZm[Z[m\Z\m]Z]m^Z^m_Z_m`Z`maZambZbmcZcmdZdmeZe ddlfmgZgmhZh ddlmiZimjZjmkZkmlZlmmZmmnZn ddlompZp  eAj                         ZrdZs G d dej                        Zud5ded evd!ewfd"Zx G d# d$e"      Zyd%eeewef   ewevee   f   d!e]fd&Zzd'edeYf   d(ewd)eee{e{e{f   ewdf   d*e|d+e|d!eafd,Z}d-ewd.eew   d/ewd0eewe~f   d1e^d2e{d3e{d!e~fd4Zy)6    N)BytesIOFileIOIOBase)compress)Path)TracebackType)IOAnyCallableDictIterableListOptionalPatternTupleTypeUnioncast   )_default_fonts_space_widthbuild_char_map_from_dict)DocumentInformationPdfDocCommon)EncryptAlgorithm
Encryption)
PageObject)nums_clear_rangenums_insert	nums_next)	PdfReader)StrByteType
StreamType_get_max_pdf_version_header	deprecatedeprecation_with_replacementlogger_warning)AnnotationDictionaryAttributes)CatalogAttributes)CatalogDictionary"FileSpecificationDictionaryEntriesGoToActionArguments	ImageTypeInteractiveFormDictEntriesPageLabelStyleTypFitArgumentsUserAccessPermissions)Core)FieldDictionaryAttributes)PageAttributes)PagesAttributes)TrailerKeys)
PyPdfError)PAGE_FITArrayObjectBooleanObjectByteStringObjectContentStreamDecodedStreamObjectDestinationDictionaryObjectFitFloatObjectIndirectObject
NameObject
NullObjectNumberObject	PdfObjectRectangleObjectStreamObjectTextStringObject
TreeObjectViewerPreferencescreate_string_object
hex_to_rgbis_null_or_none)	PageRangePageRangeSpec)AnnotationSubtypeBorderArrayType
LayoutTypeOutlineItemTypeOutlineTypePagemodeType)XmpInformation   c                   0   e Zd ZdZ ej
                         Z ej
                         Z ej
                         Z ej
                         Z	 ej
                         Z
 ej
                         Z ej
                         Z ej
                         Zeez  ez  Zy)ObjectDeletionFlagr   N)__name__
__module____qualname__NONEenumautoTEXTLINKSATTACHMENTS
OBJECTS_3DALL_ANNOTATIONSXOBJECT_IMAGESINLINE_IMAGESDRAWING_IMAGESIMAGES     D/var/www/horilla/myenv/lib/python3.12/site-packages/pypdf/_writer.pyrY   rY      s~    D499;DDIIKE$))+KJdiikOTYY[NDIIKMTYY[Nm+n<Frj   rY   stream	blocksizereturnc                      t        j                         }t         fdd      D ]  }|j                  |        |j	                         S )Nc                  &    j                         S N)read)rm   rl   s   rk   <lambda>z#_rolling_checksum.<locals>.<lambda>   s    fkk)4 rj   rj   )hashlibmd5iterupdate	hexdigest)rl   rm   hashblocks   ``  rk   _rolling_checksumr{      s>    ;;=D4c: E>>rj   c                   .    e Zd ZdZ	 	 	 	 ddedeeef   dedeeef   dededdf
d	Z	e
defd
       Ze
defd       Ze
dee   fd       Zej                   deeeef      ddfd       Ze
dee   fd       Zej                   dee   ddfd       ZddZdeee      dee   dee   ddfdZ	 	 ddedee   f   dedee   f   deeef   fdZe
defd       Zej                   deeef   ddfd       Zde defdZ!dee"ef   de fdZ#dee"ef   de de fd Z$	 dd!e%d"e"d#ee   de%fd$Z&dd&eddfd'Z'de(fd(Z)	 dd!e%d#ee   de%fd)Z*	 	 dd!e%d"e"d#ee   de%fd+Z+dede"e,ef   dee"   fd,Z-	 dd-ee.   d.ee.   de%fd/Z/	 	 	 dd-eee.e0jb                  f      d.eee.e0jb                  f      d"e"de%fd0Z2e
dede3e4e5f   f fd1       Z6e6j                   d2edee3e%f   ddfd3       Z6d4eddfd5Z7d6ed7eeef   ddfd8Z8	 dd9ed:ee9e%gdf      ddfd;Z:	 	 dd<ed=ed>ed?e.ddf
d@Z; e<jz                  d*      Z>e>d%fd!ee%e?e%   df   dAeeef   dBe<jz                  dCee   ddf
dDZ@	 dd!ee%   de?e   fdEZAd9eddfdFZB	 dd9ed:ee9e%gdf      ddfdGZCde5fdHZDddIZEdd%eFfddJdKedLee   dMedNeGdOee   ddfdPZHdQeIddfdRZJdQeeef   deKeeLe   f   fdSZMde?e   fdTZNdQeIddfdUZOdQeIdeKe?e"   e?e"   f   fdVZPdQeIdWe?e"   dXe?e"   de"fdYZQdQeIdZe"ddfd[ZRe
deeS   f fd\       ZTeTj                   deeeSeeeef   f      ddfd]       ZTd^eeef   ddfd_ZU	 	 dd`edaeddfdbZVdceeWeXeeYeeZe e[e4e,f
   ddfddZ\d7edefdeZ]de defdfZ^de_fdgZ`deWfdhZae
deWfdi       Zb	 	 	 ddjeee%e_f   dkede_ef   dlede_ef   dmedef
dnZc	 	 	 ddoeddkede_ef   dlede_ef   dmedef
dpZedddddefd%fdqedrede%ee"f   dkede_ef   dlede_ef   dseeeKe.e.e.f   ef      dteduedvegdmedefdwZhddxZidqe4dyeeeWf   ddfdzZjdje defd{Zkdqedre"defd|Zldd}Zmd~eeeneen   f      ddfdZod!eee%ef   d~eee      ddfdZpd!ee%ef   deeqeeq   f   ddfdZresj                  fdesddfdZuddZv	 ddre"dedewdeeW   ddf
dZxdZydeez   fdZ{deeZezf   ddfdZ|dezddfdZ}e
deez   fd       Z~e~j                   dezddfd       Z~dZdee   fdZe
dee   fd       Zej                   deddfd       Zdree"e%f   deeef   defdZd!ee%ef   de%fdZdeeeef   deKeee   f   fdZ	 	 	 	 ddeeeef   doeedeeKe"e"f   eKe"e"e"f   e?e"   f   dedeeKe"e"f   eKe"e"e"f   e?e"   e?e%   f   dedeee?e   eKedf   f      ddfdZ	 	 	 	 ddee"   deeeef   doee   deeee?e%   f      dedeee?e   eKedf   f      ddfdZdedee"e%f   d9edefdZdeee   ef   dee"e%f   d9eddfdZd!edee%e,f   dee"e%f   d9edee   fdZdeee?e   f   d!e%dee"e%f   d9ede?e3   f
dZdedee"e%f   d9ede?e3   fdZd2e3de_fdZ	 dde?e3   dkee_ef   dlede_ef   ddfdZddZ	 ddoeeef   dcee   dee?e"      fdZ	 ddoeeef   dcee   ddfdZ	 dd9edeef   ddfdZ	 	 	 dde"de"dee   dee   dee"   ddfdZ	 	 	 dde"de"dee   dee   dee"   ddfdZ xZS )	PdfWritera  
    Write a PDF file out, given pages produced by another class or through
    cloning a PDF file during initialization.

    Typically data is added from a :class:`PdfReader<pypdf.PdfReader>`.

    Args:
        clone_from: identical to fileobj (for compatibility)

        incremental: If true, loads the document and set the PdfWriter in incremental mode.


            When writing incrementally, the original document is written first and new/modified
            content is appended. To be used for signed document/forms to keep signature valid.

        full: If true, loads all the objects (always full if incremental = True).
            This parameters may allows to load very big PDFs.

    NFfileobj
clone_fromincrementalfullrn   c                    |xs || _         	 g | _        	 g | _        	 i | _        	 i | _        	 d | _        |  | j                   rt        |t        t        f      r/t        |d      5 }t        |j                  d            }d d d        t        |t              rt        |      }t        |t              st        d      || _        |j                  j!                         | _        d| _        n:d| _        | j'                  t)        t+        d      t-        d      i            | _        dt0        d t        t        t        t2        t4           t        f   d	t0        d t        t        t        t2        t4           t        f   d
t0        d t        t        t        t2        t4           t        f   fd} |||      }|| _        d| _        d| _        t)               }|j=                  t+        t>        j@                        t+        d      t+        t>        jB                        tE        d      t+        t>        jF                        tI               i       g | _%        d | _&        d | _'        |-t        |t              st        |      }| jQ                  |       n| j'                  |      | _)        t)               | _*        | jT                  j=                  t+        t>        j@                        t+        tV        jX                        t+        tV        jZ                        | jR                  i       | j'                  | jT                         |r	|sd| _         t        | j
                  t\              rt        | j
                  d   t^              r3ta        | j
                  d   jc                               | j
                  d<   t        | j
                  d   t^              r4ta        | j
                  d   jc                               | j
                  d<   y y y # 1 sw Y   xY w)Nrbz!Invalid type for incremental modeTs   %PDF-1.3z	/Producerpypdfr~   r   rn   c                    t        | t        t        t        t        f      r	| dk(  s||S d}t        | t        t        f      rSt        t        |             j                         r/t        t        |             j                         j                  dk(  rd}t        | t        t        f      rI| j                         }| j                  dd       | j                         dk(  rd}| j                  |d       |r| }|S )N Tr   Fr      )

isinstancestrr   r	   r   existsstatst_sizetellseek)r~   r   cloningts       rk   _get_clone_fromz+PdfWriter.__init__.<locals>._get_clone_from   s     'Cr7#;<2!7!!G'C;/W&--/G%**,449'B=1LLNR#<<>Q&#GQ"$
rj   r   F/Pagesr   r   )2r   _objects_original_hash_idnum_hash_id_translated_IDr   r   r   openr   rr   r    r6   _reader
pdf_headerencode_header	_readonly_add_objectr>   rB   rK   	_info_objr   r	   r
   temp_fileobjr~   with_as_usagerw   PATYPECOUNTrD   KIDSr8   flattened_pages_encryption_encrypt_entryclone_document_from_reader_pages_root_objectCOCATALOGPAGESlistrH   r:   get_original_bytes)selfr~   r   r   r   fr   pagess           rk   __init__zPdfWriter.__init__   sH    '.$	 46	 *,	 VX	
 :<	 .2'C;/'4( 2A%affRj1G2'7+#G,gy1 !DEE"DL"--446DL!DN&DL!-- ,.B7.KLDN	4Cr#wGH	dIsD"S'7JK	 4Cr#w?@	0 %Wj9
#" "277#Z%9288$l1o277#[]	
  "15:>!j)4&z2
++J7**51DK 0 2D$$rww'BJJ)?rxx($++ T../$Ddhh%$((1+'78.txx{/M/M/OP$((1+'78.txx{/M/M/OP 9 &Y2 2s   $O??P	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.
        Fri   r   s    rk   is_encryptedzPdfWriter.is_encrypted#  s     rj   c                     | j                   S )zw
        Provide direct access to PDF Structure.

        Note:
            Recommended only for read access.

        )r   r   s    rk   root_objectzPdfWriter.root_object-  s        rj   c                 n    | j                   dS t        t        | j                   j                               S )z
        Provide access to "/Info". Standardized with PdfReader.

        Returns:
            /Info Dictionary; None if the entry does not exist

        N)r   r   r>   
get_objectr   s    rk   _infozPdfWriter._info8  s7     ~~% 	
 &(A(A(CD	
rj   valuec                    |9	 d | j                   | j                  j                  j                  dz
  <   d | _        y | j                  | j                  t                     | _        t        t        | j                  j                               }|j                          |j                  t        t        |j                                      y # t        t
        f$ r
 Y d | _        y w xY w)Nr   )r   r   indirect_referenceidnumKeyErrorAttributeErrorr   r>   r   r   clearrw   )r   r   objs      rk   r   zPdfWriter._infoG  s    =MQdnn??EEIJ "DN~~%!%!1!12B2D!E')B)B)DECIIKJJt,e.>.>.@AB n- !DNs   0C C%$C%c                 J    t        t        | j                  j                        S )(XMP (Extensible Metadata Platform) data.)r   rV   r   xmp_metadatar   s    rk   r   zPdfWriter.xmp_metadataV  s     ND$4$4$A$ABBrj   c                     |1d| j                   v r| j                   d= | j                   j                  S || j                   t        d      <   | j                   j                  S )r   z	/Metadata)r   rB   r   r   r   s     rk   r   zPdfWriter.xmp_metadata[  sb     =d...$$[1 ,,, 9>DZ45,,,rj   c                 Z    | j                   }| j                          d| _        || _        | S )z+Store that writer is initialized by 'with'.T)r   r   r   r~   )r   r   s     rk   	__enter__zPdfWriter.__enter__f  s*    !rj   exc_typeexc	tracebackc                 T    | j                   r| j                  | j                          yy)zWrite data to the fileobj.N)r~   write)r   r   r   r   s       rk   __exit__zPdfWriter.__exit__n  s      <<JJt||$ rj   includeexcludec                    t               }| j                  |       d|i}|(|j                         D ci c]  \  }}||v s|| }}}|(|j                         D ci c]  \  }}||vs|| }}}|S c c}}w c c}}w )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
        zapplication/pdf)r   r   items)r   r   r   pdf_datadatakvs          rk   _repr_mimebundle_zPdfWriter._repr_mimebundle_x  s     9

8x
 %)ZZ\BTQQ'\AqDBDB%)ZZ\FTQQg5EAqDFDF C Gs   A6A6A<,A<c                 6    | j                   j                         S )aL  
        Read/Write property of the PDF header that is written.

        This should be something like ``'%PDF-1.5'``. It is recommended to set
        the lowest version that supports all features which are used within the
        PDF file.

        Note: `pdf_header` returns a string but accepts bytes or str for writing
        )r   decoder   s    rk   r   zPdfWriter.pdf_header  s     ||""$$rj   
new_headerc                 R    t        |t              r|j                         }|| _        y rq   )r   r   r   r   )r   r   s     rk   r   zPdfWriter.pdf_header  s!    j#&#**,J!rj   r   c                    t        |dd       %|j                  j                  | k(  r|j                  S t        |t              rpt        |j                  t        j                  d       t        t        f      r<| j                  |t        j                           |t        t        j                        <   | j                  j                  |       t        t        | j                        d|       |_        |j                  S )Nr   r   )getattrr   pdfr   r>   getPGCONTENTSr8   r   rB   r   appendrA   len)r   r   s     rk   r   zPdfWriter._add_object  s    C-t4@&&**d2)))c+,GGBKK&6F(G2
 ,0+;+;C<L+MC
2;;'(S!!/DMM0BAt!L%%%rj   r   c                     t        |t              r| j                  |dz
     }n6|j                  | k7  rt	        d      | j                  |j
                  dz
     }|J |S )Nr   zpdf must be self)r   intr   r   
ValueErrorr   r   r   r   s      rk   r   zPdfWriter.get_object  sf     (#.-- 2Q 67C##t+/00-- 2 8 81 <=C
rj   c                    t        |t              r&|j                  | k7  rt        d      |j                  }| j
                  |dz
     j                  j                  }t        |dd       *|j                  j                  | k7  r|j                  |       }|| j
                  |dz
  <   t        |||       |_        t        |t              sJ |S )NzPDF must be selfr   r   )r   rA   r   r   r   r   r   
generationr   clonerE   )r   r   r   gens       rk   _replace_objectzPdfWriter._replace_object  s    
 (.9!%%- !344!3!9!9mm.23FFQQC-t4@&&**d2))D/C03(1,-!/0BC!N#y)))
rj   pageindexexcluded_keysc                    t        |t              r1|j                  t        j                  d       t
        j                  k7  rt        d      | j                  J d       |}t        |      }|t        j                  dgz  }	 | j                  t        |j                  j                           |j                  j                  = t#        d|j%                  | d|      j'                               }|j                  1|j                  j(                  }t+        | j(                  |      | _        | j-                  |      \  }}|j                  |t/        t        j                        <   |dk\  rXt#        t0        |t        j2                           j5                  ||j                         | j                  j5                  ||       nUt#        t0        |t        j2                           j7                  |j                         | j                  j7                  |       d}t9        |      st#        t:        |j'                               }t=        t#        t>        |t        j@                           dz         |t/        t        j@                        <   |j                  t        j                  d       }|dz  }|dk  rtC        d	      t9        |      s|S # t         $ r Y w xY w)
NzInvalid page objectfor mypyz/StructParentsr   Fr   i  r   zToo many recursive calls!)"r   r   r   r   r   r   PAGEr   r   r   PARENTr   idr   r   r   	Exceptionr   r   r   r   r#   _get_page_in_noderB   r8   r   insertr   rM   r>   rD   r   r   r6   )	r   r   r   r   page_orgothernodeidxcpts	            rk   	_add_pagezPdfWriter._add_page  sC    $
+txx/F"''/Q233##/;;/]+"))%566
	##Bx'B'B'F'F$GH++11
 (..umDOOQ
 <<#LL++E9$//5QDO**51	c&*&=&=Z		"#!8d277m,33C9P9PQ  ''t4d277m,33D4K4KL  ''-!$'($//*;<D)5d3RXX6ORS6S)TDBHH%&88BIIt,D1HCQw !<== "$' 3  		s   AK 	KKTstatec                    	 t         j                  | j                  vr=| j                  t	                     | j                  t        t         j                        <   t        t        j                        }t        |      t        t        | j                  t         j                           |<   y# t        $ r }t        d| d| t               Y d}~yd}~ww xY w)a  
        Sets the "NeedAppearances" flag in the PDF writer.

        The "NeedAppearances" flag indicates whether the appearance dictionary
        for form fields should be automatically generated by the PDF viewer or
        if the embedded appearance should be used.

        Args:
            state: The actual value of the NeedAppearances flag.

        Returns:
            None

        zset_need_appearances_writer(z
) catch : N)r)   	ACRO_FORMr   r   r>   rB   r-   NeedAppearancesr9   r   r   r&   rZ   )r   r  need_appearancesr   s       rk   set_need_appearances_writerz%PdfWriter.set_need_appearances_writer  s    "	 **$2C2CC $$%5%78 !!0::;  **D*T*TU e$ !4#4#45F5P5P#QR   	.ugZuEx 	s   B)B, ,	C5CCc                     t               }| j                  |      | j                  t        t        j
                        <   |S rq   )rJ   r   r   rB   r)   VIEWER_PREFERENCES)r   os     rk   create_viewer_preferencesz#PdfWriter.create_viewer_preferences)  s@     Q 	(;;<	
 rj   c                 t    | j                   J d       | j                  |t        | j                         |      S )a  
        Add a page to this PDF file.

        Recommended for advanced usage including the adequate excluded_keys.

        The page is usually acquired from a :class:`PdfReader<pypdf.PdfReader>`
        instance.

        Args:
            page: The page to add to the document. Should be
                an instance of :class:`PageObject<pypdf._page.PageObject>`
            excluded_keys:

        Returns:
            The added PageObject.

        mypy)r   r  r   )r   r   r   s      rk   add_pagezPdfWriter.add_page0  s9    , ##/77/~~dC(<(<$=}MMrj   r   c                     | j                   J d       |dk  rt        | j                         |z   }|dk  rt        d      |t        | j                         k\  r| j                  ||      S | j	                  |||      S )a]  
        Insert a page in this PDF file. The page is usually acquired from a
        :class:`PdfReader<pypdf.PdfReader>` instance.

        Args:
            page: The page to add to the document.
            index: Position at which the page will be inserted.
            excluded_keys:

        Returns:
            The added PageObject.

        r  r   zInvalid index value)r   r   r   r  r  )r   r   r   r   s       rk   insert_pagezPdfWriter.insert_pageI  s    & ##/77/19,,-5E19233C,,--==}55>>$}==rj   c                     t        |      ry|J d       t        |t              rt        |d|       }|j	                         }t        |t
              r|j                  S y)z
        Generate _page_id2num.

        Args:
            indirect_reference:

        Returns:
            The page number or None

        Nr  r   )rM   r   r   rA   r   r   page_numberr   s      rk   _get_page_number_by_indirectz&PdfWriter._get_page_number_by_indirectf  sa     -.!-5v5-(#.!/0BAt!L ++-c:&??"rj   widthheightc                 R    t        j                  | ||      }| j                  |      S )a(  
        Append a blank page to this PDF file and return it.

        If no page size is specified, use the size of the last page.

        Args:
            width: The width of the new page expressed in default user
                space units.
            height: The height of the new page expressed in default
                user space units.

        Returns:
            The newly appended page.

        Raises:
            PageSizeNotDefinedError: if width and height are not defined
                and previous page does not exist.

        )r   create_blank_pager  )r   r  r  r   s       rk   add_blank_pagezPdfWriter.add_blank_page~  s'    , ++D%@}}T""rj   c                     ||N|| j                         k  r;| j                  |   }|j                  j                  }|j                  j                  }t        j                  | ||      }| j                  ||       |S )aU  
        Insert a blank page to this PDF file and return it.

        If no page size is specified, use the size of the last page.

        Args:
            width: The width of the new page expressed in default user
                space units.
            height: The height of the new page expressed in default
                user space units.
            index: Position to add the page.

        Returns:
            The newly inserted page.

        Raises:
            PageSizeNotDefinedError: if width and height are not defined
                and previous page does not exist.

        )get_num_pagesr   mediaboxr  r  r   r  r  )r   r  r  r   oldpager   s         rk   insert_blank_pagezPdfWriter.insert_blank_page  sv    4 =FNut7I7I7K/Kjj'G$$**E%%,,F++D%@u%rj   c                     t         |   S rq   )superopen_destinationr   	__class__s    rk   r   zPdfWriter.open_destination  s     w''rj   destc                    |	 | j                   d= y t        |t              r"t	        |      | j                   t        d      <   y t        |t              r#|j                  | j                   t        d      <   y t        |t              rRt        d|j                  |j                  n	t               t              j                  | j                   t        d      <   y y # t        $ r Y y w xY w)Nz/OpenActionOpening)r   r   r   r   rH   rB   r=   
dest_arrayr   r   rC   r7   )r   r#  s     rk   r   zPdfWriter.open_destination  s    <%%m4 c";KD;QDj78k*;???Dj78j);F**6 ''\< j j78 *  s   C 	C&%C&
javascriptc           
         d| j                   vr.t               | j                   t        t        j                        <   t        t        | j                   t        j                           }d|vr*t        t        d      t               i      |t        d      <   t        t        t        t        |d         d         }t               }|j                  t        t        j                        t        d      t        d      t        d      t        d      t        |       i       |j                  t        t        t        j                                            |j                  | j!                  |             y)a%  
        Add JavaScript which will launch upon opening this PDF.

        Args:
            javascript: Your JavaScript.

        >>> output.add_js("this.print({bUI:true,bSilent:false,bShrinkToFit:true});")
        # Example: This will launch the print window when the PDF is opened.

        /Namesz/JavaScriptz/Action/Sz/JSN)r   r>   rB   CANAMESr   r8   rw   r   r   rH   r   rK   r   uuiduuid4r   )r   r'  namesjs_listjss        rk   add_jszPdfWriter.add_js  s    4,,,6F6HDj23%t'8'8'BC%/?H%{}50E*]+, .m0DEhO
 
		277#Z	%:4 *]";5!#3zl#D	
 	+C

,=>?t''+,rj   filenamer   c           	         t        |t              r|j                  d      }t               }|j	                  |       |j                  t        t        j                        t        d      i       t               }|j                  t        d      | j                  |      i       t               }|j                  t        t        j                        t        d      t        t        j                        t        |      t        t        j                        |i       t        j                   | j"                  vr=| j                  t                     | j"                  t        t        j                         <   dt%        t        | j"                  t        j                            vrst        t        t        j                         t'               i      }| j                  |      t%        t        | j"                  t        j                            t        d      <   n<t%        t        t%        t        | j"                  t        j                            d         }t%        t&        |t        j                            j)                  t        |      |g       y)a  
        Embed a file inside the PDF.

        Reference:
        https://opensource.adobe.com/dc-acrobat-sdk-docs/pdfstandards/PDF32000_2008.pdf
        Section 7.11.3

        Args:
            filename: The filename to display.
            data: The data in the file.

        zlatin-1z/EmbeddedFile/Fz	/Filespecz/EmbeddedFilesN)r   r   r   r<   set_datarw   rB   r   r   r>   r   r*   FrK   EFr+  r,  r   r   r8   extend)r   r3  r   
file_entryef_entryfilespecembedded_files_names_dictionarys          rk   add_attachmentzPdfWriter.add_attachment  s   : dC ;;y)D(*
D!:bgg.
?0KLM $%D)4+;+;J+GHI#%277#Z%<=??@BVC =@@A8	
, 884,,,6:6F6F "7Dj23 4(8$:K:KBHH:U#VV.>BHH%{}5/+
   !@A !4#4#4RXX#>?+, /3 %t'8'8'BCDTU/+ 	[9"((CDKK!(+X6	
rj   readerafter_page_appendc                     t        |j                        }t        |      D ]6  }|j                  |   }| j                  |      }t	        |      s/ ||       8 y)a  
        Copy pages from reader to writer. Includes an optional callback
        parameter which is invoked after pages are appended to the writer.

        ``append`` should be preferred.

        Args:
            reader: a PdfReader object from which to copy page
                annotations to this writer object. The writer's annots
                will then be updated.
            after_page_append:
                Callback function that is invoked after each page is appended to
                the writer. Signature includes a reference to the appended page
                (delegates to append_pages_from_reader). The single parameter of
                the callback is a reference to the page just appended to the
                document.

        N)r   r   ranger  callable)r   r?  r@  reader_num_pagesreader_page_numberreader_pagewriter_pages          rk   append_pages_from_readerz"PdfWriter.append_pages_from_readerR  sW    0 v||,"'(8"9 	/ ,,'9:K--4K)*!+.	/rj   fieldanno	font_name	font_sizec                 t   t        t        |t        j                           }t        ddt	        |d   |d   z
        t	        |d   |d   z
        f      }|j                  t        j                  t        t        | j                  t        j                           j                  t        j                  d             }|t        d      }n|j                         }|j                  dd      j                  dd      j                  d      }|D 	cg c]
  }	|	d	k7  s	|	 }}	|r|||j!                  d
      dz
  <   n||j!                  d
      dz
     }|dk\  r|nt#        ||j!                  d
      dz
           }
|
dk(  rQ|j                  t$        j&                  d      t$        j(                  j*                  z  rt,        }
n|j.                  dz
  }
t1        |
      ||j!                  d
      dz
  <   dj3                  |      }|j.                  dz
  |
z
  }t        t        t        t        |j                  dt        t        | j                  t        j                           j                  dt                                 j                               }|j                  dt                     j                         }||vr|t4        vrt        t6        t8        t8        f   t        t        | j                  t        j                           j                  di             }|j                         j                  dt                     j                         }|j                  |d       }t;        |      st        t        |j                               }t=        d|      \  }}}}	 |d= tA        |t0              r3|jC                         D ci c]  \  }}||jE                  |       }}}n|jC                         D ci c]  \  }}|tG        |f       }}}|jC                         D ci c]  \  }}|tG        |f       }}}|jC                         D ]  \  }}|j                  ||      ||<    ntI        d| dtJ               i }|j                  t$        j&                  d      }|j                  t$        jL                  d      dk(  ru|t$        j(                  jN                  z  dk(  rUdj3                  |j                  t$        jP                  g             }|j                  dg       }tA        |tR              s|g}n|j                  dd	      }g }|j                  dd      j                  dd      j                  dd      }tU        ||||||
|      }tW        jX                  t[        d      t[        d      t[        d      t[        d      t[        d      |dt]        |      d di      }t        j^                  |v rQt        t        |t        j^                           j                  d!i       jC                         D ]  \  }}|d"vs|||<    |At        t[        d      t        t[        |      ta        |d#|      i      i      |t[        d$      <   t        j^                  |vr@t        t[        d!      | jc                  |      i      |t[        t        j^                        <   y d!t        t        |t        j^                           vrF| jc                  |      t        t        |t[        t        j^                                 t[        d!      <   y |t        j^                     d!   jd                  jf                  }|| jh                  |dz
  <   tk        |d|       |_2        y c c}	w # t>        $ r Y w xY wc c}}w c c}}w c c}}w )%Nr   r      r   z/Helv 0 Tf 0 g
 r   Tfz/DRz/Font   r   zFont dictionary for z not found./Tx/Ch/V\z\\(z\()z\)/Type/XObject/Subtype/Form/BBox__streamdata__/Length/N>   r^  rZ  /Filterr`  r\  r   
/Resources)6r   rF   AARectabsget_inheritedDAr>   r   r)   r  r   rH   r   replacesplitr   floatFAFfFfBits	Multiline DEFAULT_FONT_HEIGHT_IN_MULTILINEr  r   joinr   r   r
   rM   r   r   r   r   r   bytesr&   rZ   FTComboOptr   generate_appearance_streamr<   initialize_from_dictionaryrB   r:   APr   r   r   r   r   rA   )r   rI  rJ  rK  rL  _rctrctdafont_propertiesxfont_heighty_offsetdrfont_resfont_subtype_font_encodingfont_mapr   r   font_full_revfont_encoding_revkkfield_flagstxtsel	ap_streamdctns                                rk   _update_field_annotationz"PdfWriter._update_field_annotations  s    OT"'']3q!Sa47):%;Sa4PQ7AR=STU EE!4#3#34E4O4O#PQUUt
 :!"23BB**T3/77cBHHM&5Ab1AA?HOO11$7!;<'(=(=d(Ca(GHI A~ '<'<T'BQ'FGH 	
 !yy"RYY%8%88>!jj1n;>{;K--d3a78XXo&::>K/  ""($*:*:;L;V;V*Wc%!1!34	 jl
 VVG-/0;;=B94N#NS#X$d&6&67H7R7R&S#eR.	B $$W.>.@ALLNB66)T*x(,h.A.A.CDH7OX84L!]HRL --;C>>;K!371aAqxx..! ! =J<O<O<Q RDAqE1$K R R@M@S@S@U$V1Qqd^$V!$V%^^- EEB'8'<'<R'DM!$E 1)KH(SM iiq)99RUUE"e+biioo0MQR0R))D..rvvr:;C))D"%Cc4(e))D"%CCkk$'//U;CCCO.b-k8
	
 "<<7#Z
%;:&
7(;7#S "29"=1
 55D=-tBEE{;??bIOOQ 1PPCF
 ,<w')9&y17 (*>4*
-C
<() 55&6D!4#3#3C#89'DBEE"# .RUU<<   % !4
255(9#:;4  RUUD!44::A#&DMM!a% %3Aq$%?C"w Bf  ! !S$Vs0   
^^^ ^(4^.!^4	^%$^%fieldsflagsauto_regeneratec           	         t         j                  | j                  vrt        d      t	        t
        | j                  t         j                           }t        j                  |vrt        d      t        |t              r| j                  |       |t        | j                        }t        |t              r/|D ])  }t        j                  |v s| j                  |||d       + yt        j                  |vrt!        dt"               y|t        j                     D ]  }t	        t
        |j%                               }|j'                  dd      dk7  r8d|v rd	|v r|}n6|j'                  t        j(                  t                     j%                         }|j+                         D ]G  \  }	}
| j-                  |      |	k(  s|j'                  d	d      |	k(  s1|j'                  dd      d
k(  rd|v r|d= |r%t/        |      |t1        t2        j4                        <   t        |
t              r/t7        d |
D              }||t1        t2        j8                        <   n^t        |
t:              r)t=        |
d         |t1        t2        j8                        <   n%t=        |
      |t1        t2        j8                        <   |j'                  t2        j>                        dv r^t1        |
      }||t1        t@        jB                           t1        d         vrt1        d      }||t1        t@        jD                        <   |j'                  t2        j>                        dk(  s"|j'                  t2        j>                        d
k(  r@t        |
t:              r| jG                  |||
d   |
d           | jG                  ||       |j'                  t2        j>                        dk(  s8t!        dt"               J  y)a  
        Update the form field values for a given page from a fields dictionary.

        Copy field texts and values from fields to page.
        If the field links to a parent object, add the information to the parent.

        Args:
            page: `PageObject` - references **PDF writer's page** where the
                annotations and field data will be updated.
                `List[Pageobject]` - provides list of pages to be processed.
                `None` - all pages.
            fields: a Python dictionary of:

                * field names (/T) as keys and text values (/V) as value
                * field names (/T) as keys and list of text values (/V) for multiple choice list
                * field names (/T) as keys and tuple of:
                    * text values (/V)
                    * font id (e.g. /F1, the font id must exist)
                    * font size (0 for autosize)

            flags: A set of flags from :class:`~pypdf.constants.FieldDictionaryAttributes.FfBits`.

            auto_regenerate: Set/unset the need_appearances flag;
                the flag is unchanged if auto_regenerate is None.

        z+No /AcroForm dictionary in PdfWriter Objectz0No /Fields dictionary in Pdf in PdfWriter ObjectNz No fields to update on this pager\  r   /Widget/FT/TrU  /Ic              3   2   K   | ]  }t        |        y wrq   )rH   ).0r   s     rk   	<genexpr>z:PdfWriter.update_page_form_field_values.<locals>.<genexpr>U  s     %Ia&6q&9%I   r   z/Btnra  z/OffrT  r   r   z/Sigz#Signature forms not implemented yet)$r)   r  r   r6   r   r>   r-   Fieldsr   boolr  r   r   r   ANNOTSupdate_page_form_field_valuesr&   rZ   r   r   r   r   _get_qualified_field_namerD   rB   rl  rm  r8   VtuplerH   rs  rd  rx  ASr  )r   r   r  r  r  afpwriter_annotwriter_parent_annotrI  r   lstr   s                rk   r  z'PdfWriter.update_page_form_field_values	  sQ   B &&d.?.??JKK"D$5$56G6Q6Q$RS%,,B6OPPot,,,_=<

#DdD! O99>66q&%NO 99D =xH O 7	TL 0,2I2I2KLL
B/9<$)=&2#&2&6&6II/1'*, $ !' ,Tu223FG5P*..tT:eC'++E48EA 33+D16B56ILBEE!23eT*%%I5%IIC<?'
244(89u-5Ea6LBDD!12 =MU<S'
244(89&**2551f="5)AZ-> ?
4@P QQ&v.67LBEE!23'++BEE2e;*..ruu5> "%/55/uQxq 556I<X $$RUU+v5 ##H(SY,T7	Trj   c                    g }|'| j                   D ]  }|| j                  |      z  } |S 	 t        t        | j                  t
        j                           }	 t        t        |t        j                           }d|vr|S t        t        |d         }t        t        |            D ]  }||   }t        |t              }	t        t        |j!                               }|j#                  dd      dk(  sLd|v sQd|j$                  v r|j&                  |v rn|	s| j)                  |      ||<   |j+                  |j&                         |j+                  |        |S # t        $ r4 t               }|| j                  t        t
        j                        <   Y ?w xY w# t        $ r* t               }||t        t        j                        <   Y Sw xY w)a1  
        Parse annotations within the page looking for orphan fields and
        reattach then into the Fields Structure.

        Args:
            page: page to analyze.
                  If none is provided, all pages will be analyzed.

        Returns:
            list of reattached fields.

        /Annotsr\  r   r  r  r   )r   reattach_fieldsr   r>   r   r)   r  r   rB   r8   r-   r  rB  r   r   rA   r   r   __dict__r   r   r   )
r   r   r  r  r  r  annotsr   anoindirects
             rk   r  zPdfWriter.reattach_fieldsu  s    <ZZ /t++A../J	L&(9(9:K:U:U(VWB	G+r*D*K*K'LMF
 D Jk4	?3V% 	 C+C!#~6H')9:Cwwz2&)3(CLL8..&8"&"2"23"7F3Kc445

3	  
5  	L!#BIKDj):)D)DEF	L
  	G ]F@FBz4;;<=	Gs#   +E !F 9FF/GGc                    d| _         | j                  rdgt        t        |j                  d         z  | _        t        t        | j
                        dz
        D ]7  }|j                  |dz         }||j                  |       | j
                  |<   9 n| j
                  j                          |j                  j                  |       | _        | j                  j                  d      | _        t        | j
                        t        t        |j                  d         k  sJ | j                  r1| j
                  D cg c]  }||j!                         nd c}| _        | j%                          | j&                  J | j&                  D ][  }| j)                  t        t*        |j,                        j.                  |       | j                  rD| j                  |t1        d      <   ] | j                  s`t3        | j&                  D cg c]  }|j,                   c}      t        t4        | j                  j                               t1        d      <   yyc c}w c c}w )a  
        Copy the reader document root to the writer and all sub-elements,
        including pages, threads, outlines,... For partial insertion, ``append``
        should be considered.

        Args:
            reader: PdfReader from which the document root should be copied.

        N/Sizer   r   r   /Parent/Kids)r   r   r   r   trailerr   rB  r   r   	replicater   r   r   r   raw_getr   hash_binr   _flattenr   r   rA   r   r   rB   r8   r>   )r   r?  ir
  r   r  s         rk   clone_reader_document_rootz$PdfWriter.clone_reader_document_root  s    !FT#v~~g/F%GGDM3t}}-12 9%%a!e,='({{4'8DMM!$9
 MM!"..44T:''//94==!T#v~~g/F%GGGGFJmm#?B3?9#D 	##///%% 	7A  na6J6J!K!Q!QSTU##+/;;*Y'(	7  4;O;OPaQ11PQ !4;;#9#9#;<7#  # Qs   ?I	Ic           	      @   | j                  |       |j                  }| j                  r|t        t        |j                  |       j                        | _        t        | j                  t              sJ d       | j                  j                         | j                  | j                  j                  j                  dz
  <   n=|;| j                  t        t        t        |j                                           | _        	 t        t        |j                         j                  |       | _        t%        |      rUt        t        t        t        | j&                  j                               d         D ]  } ||j                                 yy# t"        $ r Y lw xY w)a{  
        Create a copy (clone) of a document from a PDF file reader cloning
        section '/Root' and '/Info' and '/ID' of the pdf.

        Args:
            reader: PDF file reader instance from which the clone
                should be created.
            after_page_append:
                Callback function that is invoked after each page is appended to
                the writer. Signature includes a reference to the appended page
                (delegates to append_pages_from_reader). The single parameter of
                the callback is a reference to the page just appended to the
                document.

        Nr   r   r  )r  r   r   r   rA   r   r   r   r   r>   r  r   r   r   r   r8   r   r   rC  r   )r   r?  r@  infr   s        rk   r   z$PdfWriter.clone_document_from_reader  sX   ( 	''/ll!%"CIIdO$F$F" "$**.>?KK? JJ'') ##NN55;;a? _!-- &68H!IJDN
	K4::4@DH %&T"2DKK4J4J4LMgV 5 "$//"345 '  		s   .F 	FFc                     t               }| j                  |       |j                  d       t        t	        |      j                  d            S )Nr   utf8)r   _write_pdf_structurer   r:   r{   r   )r   rl   s     rk   _compute_document_identifierz&PdfWriter._compute_document_identifier  s>    !!&)A 1& 9 @ @ HIIrj   c                     | j                   r | j                   d   }| j                         }n| j                         }|}t        ||f      | _         y)a   
        Generate an identifier for the PDF that will be written.

        The only point of this is ensuring uniqueness. Reproducibility is not
        required.
        When a file is first written, both identifiers shall be set to the same value.
        If both identifiers match when a file reference is resolved, it is very
        likely that the correct and unchanged file has been found. If only the first
        identifier matches, a different version of the correct file has been found.
        see 14.4 "File Identifiers".
        r   N)r   r  r8   )r   id1id2s      rk   generate_file_identifiersz#PdfWriter.generate_file_identifiers  sI     88((1+C335C335CCSz*rj   )	algorithmuser_passwordowner_password
use_128bitpermissions_flagr  c                   ||}|"	 t        t        |j                  dd            }n"t        j
                  }|st        j                  }| j                          | j                  sJ t        j                  ||| j                  d         | _        | j                  j                  ||      }| j                  ra| j                  j                  J | j                  j                  |_        || j                  |j                  j                   dz
  <   || _        y| j#                  |       || _        y# t        $ r t	        d| d      w xY w)a  
        Encrypt this PDF file with the PDF Standard encryption handler.

        Args:
            user_password: The password which allows for opening
                and reading the PDF file with the restrictions provided.
            owner_password: The password which allows for
                opening the PDF files without any restrictions. By default,
                the owner password is the same as the user password.
            use_128bit: flag as to whether to use 128bit
                encryption. When false, 40bit encryption will be used.
                By default, this flag is on.
            permissions_flag: permissions as described in
                Table 3.20 of the PDF 1.7 specification. A bit value of 1 means
                the permission is granted.
                Hence an integer value of -1 will set all flags.
                Bit position 3 is for printing, 4 is for modifying content,
                5 and 6 control annotations, 9 for form fields,
                10 for extraction of text and graphics.
            algorithm: encrypt algorithm. Values may be one of "RC4-40", "RC4-128",
                "AES-128", "AES-256-R5", "AES-256". If it is valid,
                `use_128bit` will be ignored.

        N-r  zAlgorithm 'z' NOT supportedr   r   )r   r   ri  r   r   RC4_128RC4_40r  r   r   maker   write_entryr   r   r   r   r   )r   r  r  r  r  r  algentrys           rk   encryptzPdfWriter.encrypt  s;   B !*N K.	0A0A#s0KL #**C&--&&(xxx%??30@$((1+N  ,,]NK&&99EEE'+':':'M'ME$@EDMM%22881<= $ U##% " K ;yk!IJJKs    D4 4Erl   c                    t        |d      r,d|j                  vrt        d|j                   dt               | j
                  r| j                  j                  j                  d       |j                  | j                  j                  j                  d             t        | j                               dkD  r| j                  |       y y | j                  |      \  }}| j                  |||      }| j!                  ||       y )NmodebzFile <zH> to write to is not in binary mode. It may not be written to correctly.r   r   )hasattrr  r&   namerZ   r   r   rl   r   r   rr   r   list_objects_in_increment_write_incrementr  _write_xref_table_write_trailer)r   rl   object_positionsfree_objectsxref_locations        rk   write_streamzPdfWriter.write_streamT  s    66"s&++'= &6 6 LL$$Q'LL,,11"5641134q8%%f- 9 .2-F-Fv-N*l 22(,M 6rj   c                     d}|dk(  rt        d|d      t        |t        t        f      rt	        |d      }d| _        d}| j                  |       | j
                  r|j                          ||fS )a  
        Write the collection of pages added to this object out as a PDF file.

        Args:
            stream: An object to write the file to. The object can support
                the write method and the tell method, similar to a file object, or
                be a file path, just like the fileobj, just named it stream to keep
                existing workflow.

        Returns:
            A tuple (bool, IO).

        Fr   zOutput(stream=z) is empty.wbT)r   r   r   r   r   r   r  close)r   rl   my_files      rk   r   zPdfWriter.writel  su     R<vi{;<<fsDk*FD)F!%DG&!LLNrj   c                 r   t        t        | j                              D cg c]  }| j                  |   z|t        | j                        k\  s;t	        t
        | j                  |         j                         | j                  |   k7  r't	        t        | j                  |         j                   c}S c c}w )a  
        For debugging/analysis.
        Provides the list of new/modified objects that will be written
        in the increment.
        Deleted objects will not be freed but will become orphans.

        Returns:
            List of (new / modified) IndirectObjects

        )	rB  r   r   r   r   rE   r  rA   r   )r   r  s     rk   r  z#PdfWriter.list_objects_in_increment  s     3t}}-.
a ,T0011It}}Q'78AAC**1-. q!12EE
 	
 
s   BB4c                 l   i }g }d}d}t        | j                        D ]   \  }}| j                  |   |t        | j                        k\  s<t	        t
        | j                  |         j                         | j                  |   k7  sk|dz   }t        |t
              sJ |j                         ||<   |j                  | dj                                	 |j                  |       |j                  d       ||k7  r|dkD  r|j                  |||z
  g       |}|dz   } |dkD  sJ d       |j                  |||z
  g       |j                         }	t        | j                        dz   }
|j                  |
 dj                                t        d	      t        d
      t        d      t        |
dz         t        d      | j                  j                   t        d      t        d      t        d      t#        |D cg c]  }|D ]  }t        |        c}}      t        d      t#        t        d      t        d      t        d      g      ddi}| j$                  | j$                  j                   j&                  dz
  t        | j                        k\  sYt	        t(        | j$                        j                         | j                  | j$                  j                   j&                  dz
     k7  r0| j$                  j                   |t        t*        j,                        <   t        | j.                  j0                        |t        t*        j2                        <   | j4                  r&| j4                  |t        t*        j6                        <   t9        j:                  |      }|j=                  dj?                  |jA                         D cg c]  }tC        jD                  dd|d       c}             |j                  |       |j                  d|	 dj                                y c c}}w c c}w )Nr   r    0 obj
   
endobj
r   zfor pytest onlyz 0 objrZ  z/XRefr  z/Rootrb  z/FlateDecodez/Indexz/W   r_  rj   s   >BIB
startxref

%%EOF
)#	enumerater   r   r   r   rE   r  r   r   r   r   write_to_streamr   rB   rD   r   r   r8   r   r   rA   TKINFOr   
_startxrefPREVr   IDrG   rw  r6  rq  valuesstructpack)r   rl   r  object_blockscurrent_startcurrent_stopr  r   r   r  xr_id_su_it	init_dataxr_poss                   rk   r  zPdfWriter._write_increment  s   . 	)FAs}}Q+S,,--	4==#34==?&&q)* A!#y111*0++- 'wh/6689 ##F+]+ L($q(%,,*L=,HI %*M$qy3	)4 q 3"33 m\M-IJKDMM"Q&wf%,,./wG!4weai!8w!1!1!D!Dy!:n#=x +.;KssKc"K"K# tka,q/<?C c
	 ::!JJ))//!34&&'(NDJJ/88:""

--33a7
 .2ZZ-J-JIj)*)5dll6M6M)N	*RWW%&88+/88Ij'(44Y?
HH>N>U>U>WXdWaq1X	

 	6"}]O9=DDFG5 L, Ys    P+P1c                 x   g }g }|j                  | j                  j                         dz          |j                  d       t        | j                        D ]  \  }}||dz   }|j                  |j                                |j                  | dj                                | j                  r,|| j                  k7  r| j                  j                  ||d      }|j                  |       |j                  d       |j                  d       |j                  |dz           |j                  d       ||fS )N   
s   %
r   r  r   r  r   )r   r   r   r  r   r   r   r   r   encrypt_objectr  )r   rl   r  r  r  r   r   s          rk   r  zPdfWriter._write_pdf_structure  s   T__++-56+,. 	+FAsA ''6wh/6689##t/B/B(B**99#uaHC##F+]+ ''+##AE*	+ 	A--rj   r  r  c                    |j                         }|j                  d       |j                  dt        | j                        dz    dj	                                |j                  |d   dddd	d
j	                                d}|D ]^  }|dkD  r(|j                  |dddd	dj	                                0|j                  ||   dddd	d
j	                                |dz  }` |S )Ns   xref
0 r   rO  r   z0>10rP  i  z0>5z f 
z n 
)r   r   r   r   r   )r   rl   r  r  r  free_idxoffsets          rk   r  zPdfWriter._write_xref_table  s     Yr#dmm,q014;;=>Q-QuSk?FFHI& 	Fzt}AaWE:AACDX 6t<AaWEJQQSTA	 rj   r  c                    |j                  d       t        t        t        j                        t        t        | j                        dz         t        t        j                        | j                  j                  i      }| j                  0| j                  j                  |t        t        j                        <   | j                  &| j                  |t        t        j                        <   | j                  r0| j                  j                  |t        t        j                         <   |j#                  |       |j                  d| dj%                                y)z
        Write the PDF trailer to the stream.

        To quote the PDF specification:
            [The] trailer [gives] the location of the cross-reference table and
            of certain special objects within the body of the file.
        s   trailer
r   Nr  r  )r   r>   rB   r  SIZErD   r   r   ROOTr   r   r   r  r   r  r   ENCRYPTr  r   )r   rl   r  r  s       rk   r  zPdfWriter._write_trailer  s     	\""277#\#dmm2Dq2H%I277#T%5%5%H%H
 ::!+/::+H+HGJrww'(88)-GJruu%&.2.A.A.T.TGJrzz*+'}]O9=DDFGrj   c                     t         |   S )a  
        Retrieve/set the PDF file's document information dictionary, if it exists.

        Args:
            value: dict with the entries to be set. if None : remove the /Info entry from the pdf.

        Note that some PDF files use (xmp)metadata streams instead of document
        information dictionaries, and these metadata streams will not be
        accessed by this function.

        )r  metadatar!  s    rk   r  zPdfWriter.metadata'  s     wrj   c                     |d | _         y | j                   | j                   j                          nt               | _         | j                  |       y rq   )r   r   r>   add_metadatar   s     rk   r  zPdfWriter.metadata6  sB    
 =DJzz%

  "-/
e$rj   infosc                    i }t        |t              rt        t        |j	                               }t        |j                               D ]E  \  }}t        |t              r|j	                         }t        t        |            |t        |      <   G t        | j                  t              sJ | j                  j                  |       y)z
        Add custom metadata to the output.

        Args:
            infos: a Python dictionary where each key is a field
                and each value is your new metadata.

        N)r   rE   r   r>   r   r   r   rK   r   rB   r   rw   )r   r  argskeyr   s        rk   r  zPdfWriter.add_metadataD  s     eY')5+;+;+=>Eu{{}- 	EJC%+((*$8U$DDC!	E $**&6777

$rj   remove_identicalsremove_orphansc           
         dt         dt        t        t        f   ddffdi | _        dgt	        | j
                        z  t        | j
                        D ]  \  }}|	t        |j                  t              sJ |j                         }|rI|| j                  v r;| j                  |   d   j                  |j                         d| j
                  |<   |j                  g f| j                  |<    | j                  j                         D ci c]  }t	        |d         dkD  s|d   |d    }}i }|j                         D ]-  \  }	}|j                  t        ||	ft	        |      z               / | j
                  D ]"  }t        |t        t         f      s ||       $ d	| j"                  j                  j$                  dz
  <   d	| j&                  j                  j$                  dz
  <   	 d	| j(                  j                  j$                  dz
  <   t-        t/        t	        | j
                                    D ]  }
d| j
                  |
<    yc c}w # t*        $ r Y Mw xY w)
aE  
        Parse the PDF file and merge objects that have the same hash.
        This will make objects common to multiple pages.
        Recommended to be used just before writing output.

        Args:
            remove_identicals: Remove identical objects.
            remove_orphans: Remove unreferenced objects.

        r   crossrefrn   Nc                 :   t        | t              r| j                         }nt        | t              rt	        |       }ny t        | t        t        f      sJ |D ]?  \  }}t        |t
              r d|j                  dz
  <   ||v s-||   | |<   6	  ||       A y )NFr   )r   r>   r   r8   r  rA   r   )r   r  key_valr   r   orphansreplace_in_objs        rk   r  z<PdfWriter.compress_identical_objects.<locals>.replace_in_objg  s     #/0))+C-#C.c$4k#BCCC 01a0+0GAGGaK(H}!)!A?"1h/0rj   Tr   r   F)rE   r   rA   r   r   r   r  r   r   
hash_valuer   r  r   rw   zipr>   r8   r   r   r   r   r   r   rB  )r   r  r  r   r   hr   cnvcnv_revr   r  r  r  s              @@rk   compress_identical_objectsz$PdfWriter.compress_identical_objectsW  sL    	0	0&*>>+I&J	0	0* &3t}}--!$--0 		CHC{c44nEEE A Q$*:*:%:  #A&--c.D.DE%)c"'*'='=r&B  #		C $(#3#3#:#:#<NaAaD	AqtQqTzNN8:IIK 	2DAqNN3q1$Q-01	2 == 	-C# 0+>?sG,	-
 BG  3399A=>;@

--33a78	=BGDHH//559: %DMM 23W= 	$A#DMM!	$' O"  		s   I*I7&I 	I+*I+rootc                     t        d       y)a  
        Resolving any circular references to Page objects.

        Circular references to Page objects can arise when objects such as
        annotations refer to their associated page. If these references are not
        properly handled, the PDF file will contain multiple copies of the same
        Page object. To address this problem, Page objects store their original
        object reference number. This method adds the reference number of any
        circularly referenced Page objects to an external reference map. This
        ensures that self-referencing trees reference the correct new object
        location, rather than copying in a new copy of the Page object.

        Args:
            root: The root of the PDF object tree to sweep.

        zb_sweep_indirect_references has been removed, please report to dev team if this warning is observedN)r$   )r   r   s     rk   _sweep_indirect_referencesz$PdfWriter._sweep_indirect_references  s    > 	p	
rj   c                 2    t        d       t        dd|       S )a  
        Resolves an indirect object to an indirect object in this PDF file.

        If the input indirect object already belongs to this PDF file, it is
        returned directly. Otherwise, the object is retrieved from the input
        object's PDF file using the object's ID number and generation number. If
        the object cannot be found, a warning is logged and a `NullObject` is
        returned.

        If the object is not already in this PDF file, it is added to the file's
        list of objects and assigned a new ID number and generation number of 0.
        The hash value of the object is then added to the `_idnum_hash`
        dictionary, with the corresponding `IndirectObject` reference as the
        value.

        Args:
            data: The `IndirectObject` to resolve.

        Returns:
            The resolved `IndirectObject` in this PDF file.

        Raises:
            ValueError: If the input stream is closed.

        z`_resolve_indirect_object has been removed, please report to dev team if this warning is observedr   )r$   rA   )r   r   s     rk   _resolve_indirect_objectz"PdfWriter._resolve_indirect_object  s     8 	n	
 aD))rj   c                     | j                   j                  |      dz   }t        |d|       }|j                         |k(  sJ |S Nr   r   )r   r   rA   r   )r   r   r   refs       rk   get_referencezPdfWriter.get_reference  sC    ##C(1,UAt,~~3&&&
rj   c                 B   t         j                  | j                  v rt        t        | j                  t         j                           }t        |t              s3t	        |      }| j                  |j                  j                  |       |}| j                  j                  |      dz   }t        |d|       }|j                         |k(  sJ |S t	               }|j                  i        | j                  |      }|| j                  t        t         j                        <   |S r&  )r   OUTLINESr   r   rI   r   r   r   r   r   r   rA   r   rw   r   rB   )r   outliner   r   outline_refs        rk   get_outline_rootzPdfWriter.get_outline_root  s    ;;$+++:t'8'8'EFGgz2w'$$W%?%?%E%EqIMM''014E(48K))+w666  !lGNN2**73K9DDj56rj   c                     t         j                  | j                  v r-t        t        | j                  t         j                           }|S t	               }|| j                  t        t         j                        <   |S )u   
        The list of threads.

        See §12.4.3 of the PDF 1.7 or PDF 2.0 specification.

        Returns:
            An array (possibly empty) of Dictionaries with ``/F`` and
            ``/I`` properties.

        )r   THREADSr   r   r8   rB   )r   threadss     rk   get_threads_rootzPdfWriter.get_threads_root  s_     ::***;(9(9"**(EFG  "mG8?Dj45rj   c                 "    | j                         S )u   
        Read-only property for the list of threads.

        See §8.3.2 from PDF 1.7 spec.

        Each element is a dictionaries with ``/F`` and ``/I`` keys.
        )r1  r   s    rk   r0  zPdfWriter.threads  s     $$&&rj   page_destinationparentbeforeis_openc           	      P   t        t        |j                               }t        |t              rS| j	                  t        d|j                   t        t        |j                        t        j                                     S || j                         }t        |      |t        d      <   t        t        |j                               }| j                  |      }||j                  }|j!                  ||| |r|j"                  nd        d|vrt%        d      |t        d      <   |S )Nzpage #z
/%is_open%c                      y)Nr   ri   )r}  ys     rk   rs   z8PdfWriter.add_outline_item_destination.<locals>.<lambda>;  s    rj   z/Countr   )r   r   r   r   add_outline_item_destinationr=   r  rA   r   r?   fitr-  r9   rB   rI   r   insert_childinc_parent_counter_outlinerD   )r   r3  r4  r5  r6  page_destination_refs         rk   r:  z&PdfWriter.add_outline_item_destination  s     
,<,G,G,IJ&
344-99:;)9)L)LMGGI  >**,F5B75KL12j&"3"3"56#//0@A..F  77 	
 ++5A!_Z12##rj   outline_itemc                 b    t               }|j                  |       	 | j                  ||||      S rq   )rI   rw   r:  )r   r?  r4  r5  r6  outline_item_objects         rk   add_outline_item_dictzPdfWriter.add_outline_item_dictB  s=     )l""<0	 00
 	
rj   titler  colorbolditalicr;  c
                    t        |t              r |||}| j                  |||d|||||		      S |d}
nt        |t              r|}nGt        |t              r|j
                  }n*t        |t              r	 | j                  |   j
                  }t        d| t               t               }t        t        d|z   dz         ||      }| j                  t!        t        t"        j$                        |j&                  t        t"        j(                        t        d      i            }
| j                  t+        |
||||            }|| j-                         }| j/                  ||||	      S # t        $ r t        |      }Y w xY w)a  
        Add an outline item (commonly referred to as a "Bookmark") to the PDF file.

        Args:
            title: Title to use for this outline item.
            page_number: Page number this outline item will point to.
            parent: A reference to a parent outline item to create nested
                outline items.
            before:
            color: Color of the outline item's font as a red, green, blue tuple
                from 0.0 to 1.0 or as a Hex String (#RRGGBB)
            bold: Outline item font is bold
            italic: Outline item font is italic
            fit: The fit of the destination page.

        Returns:
            The added outline item as an indirect object.

        N)r6  zcan not find reference of page /z outline item/GoTo)r   r?   add_outline_itemrA   r   r   r   r   
IndexErrorrD   r&   rZ   rC   r=   rB   r   r>   r+   Dr&  S_create_outline_itemr-  r:  )r   rC  r  r4  r5  rD  rE  rF  r;  r6  
action_refpage_refr#  r?  s                 rk   rJ  zPdfWriter.add_outline_itemY  s   @ fc";#6!(({FD&%vW^ )   J+~6&K4&99K-9#zz+6IIH 5k]C &<3;89D )) "#6#8#894??"#6#8#89:g;NJ '' UE64H
 >**,F00vvwWW9 " 9+K8H9s   7E. .FFc                     t        d      )NzIThis method is not yet implemented. Use :meth:`add_outline_item` instead.)NotImplementedErrorr   s    rk   add_outlinezPdfWriter.add_outline  s    !W
 	
rj   destinationc                    | j                         }d}|t        |      k  rJ|||   k  r.|j                  ||       |j                  |t        |             y |dz  }|t        |      k  rJ|j	                  t        |      |g       y )Nr   r   )get_named_dest_rootr   r   rH   r9  )r   rC  rT  
named_destr  s        rk   add_named_destination_arrayz%PdfWriter.add_named_destination_array  s     --/
#j/!z!}$!!![1!!!%5e%<=Q #j/! 	+E2K@Arj   c                 z    | j                  |j                        }| j                  t        d|d         |       |S )NrH   /Title)r   r&  rX  r   )r   r3  r>  s      rk   add_named_destination_objectz&PdfWriter.add_named_destination_object  sF      $//0@0K0KL((#%5h%?@BV	
 $#rj   c           
         | j                  | j                        t        j                     |   }t	               }|j                  t        t        j                        t        |t        t        j                        t        d      g      t        t        j                        t        d      i       | j                  |      }t        |t               st!        t#        |            }| j%                  ||       |S )Ni:  rI  )r   r   r   r   r>   rw   rB   r+   rL  r8   r/   FIT_HrD   rM  r   r   rH   r   rX  )r   rC  r  rP  r#  dest_refs         rk   add_named_destinationzPdfWriter.add_named_destination  s    
 ??4;;/8E!.001;z/*?*?@,sBST4 .001:g3F		
 ##D)%!12$SZ0E((9rj   c                 f    | j                   D ]"  }| j                  |t        j                         $ y)z.Remove links and annotations from this output.N)r   remove_objects_from_pagerY   rd   r   r   s     rk   remove_linkszPdfWriter.remove_links  s.    JJ 	TD))$0B0R0RS	Trj   subtypesc                 J    | j                   D ]  }| j                  ||        y)aC  
        Remove annotations by annotation subtype.

        Args:
            subtypes: subtype or list of subtypes to be removed.
                Examples are: "/Link", "/FileAttachment", "/Sound",
                "/Movie", "/Screen", ...
                If you want to remove all annotations, use subtypes=None.

        N)r   _remove_annots_from_page)r   rd  r   s      rk   remove_annotationszPdfWriter.remove_annotations  s'     JJ 	:D))$9	:rj   c                 r   t        t        |j                               }t        j                  |v rd}|t        t        t        |t        j                                 k  rt        t        |t        j                           |   }t        t        |j                               }|t        t        |d         |v rIt        |t              r$t               | j                  |j                  dz
  <   |t        j                     |= n|dz  }|t        t        t        |t        j                                 k  ry y y )Nr   r\  r   )r   r>   r   r   r  r   r8   r   r   rA   rC   r   r   )r   r   rd  r  anr   s         rk   rf  z"PdfWriter._remove_annots_from_page  s    
 $doo&7899Ac${DO<==+tBII7:+R]]_=#tCZ'AX'M!"n56@lbhhl3RYY*FA c${DO<== rj   	to_deletec           	         	 t        t        t        f      rD ]  } j                  ||        yt        t              sJ t        j
                  z  r j                  |d      S t        j                  z  r j                  |d      S t        j                  z  r j                  |d      S t        j                  z  r j                  |d      S g 	t        j                  z  rg dddgz   g dz   g d	z   d
gz   	t        j                  z  rg d	dt        dt        t           dt        t           ddf	fddt        dt        t           dt         t        t           t        t           f   f fdt        |t"              st#         |j$                        }d|v rFt'        t        |j)                               } |g       \  }} |||       |j+                  |       yy)a	  
        Remove objects specified by ``to_delete`` from the given page.

        Args:
            page: Page object to clean up.
            to_delete: Objects to be deleted; can be a ``ObjectDeletionFlag``
                or a list of ObjectDeletionFlag

        N)/Link)z/FileAttachmentz/Soundz/Moviez/Screen)z/3D)   w   J   j   M   d   i   Ws   W*)
   bs   b*   Bs   B*   S   s   fs   f*   F   n)   m   l   c   v   y   hs   res   sh)s   Tjs   TJ   '   "contentimagesformsrn   c                 L   d}|t        | j                        k  ry| j                  |   \  }}|dk(  rt        j                  z  s#|v s|dk(  r(t        j                  z  r|d   |v r| j                  |= n|dz  }|t        | j                        k  ry| j                          y )Nr   s   INLINE IMAGEs   Dor   )r   
operationsrY   rf   re   get_data)r  r  r  r  operandsoperatorjump_operatorsrj  s         rk   cleanz1PdfWriter.remove_objects_from_page.<locals>.clean/  s    Ac',,--%,%7%7%:"( !O3&);)I)II N2 E)&);)J)JJ%a[F2  **1-FA! c',,--" rj   eltstackc           	           |v s t         d      rt         fd|D              rg g fS 	 t        t        t        t        f   t        t
         d         d         }g }g }|j                         D ]-  \  }}|j                         }	 d }t        j                  z  r#|d   dk(  rt               }|j                  |       |d   dk(  r|j                  |       t        |t              r|}nUt        |      }|j                  |j                         D 	
ci c]  \  }	}
|	dvr|	|
 c}
}	       	 |j                   |_        |j                           ||       |At        |t$              r|j&                  |j(                  d	z
  <   nj+                  |      ||<   0 |D ]  }||=  t         t.              rGt         t              s-t               }|j                   j                                |   ||       ||fS # t        $ r i }Y w xY wc c}
}	w # t"        $ r Y w xY w# t,        t        f$ r Y w xY w)
Nr   c              3   R   K   | ]  }j                   t        |d d      k(     yw)r   r   N)r   r   )r  r}  r  s     rk   r  zJPdfWriter.remove_objects_from_page.<locals>.clean_forms.<locals>.<genexpr>L  s.       **ga9Mr.RRs   $'rc  r[  r\  z/Imager]  )r`  rb  z/DecodeParmsr   )r  anyr   r   r
   r>   r   r   r   rY   re   rC   r   r   r;   rw   r   r   rA   r   r   r   	TypeErrorrG   )r  r  dr  r  r   r   r
  r  k1v1imer  clean_formsr   rj  s   `            rk   r  z7PdfWriter.remove_objects_from_page.<locals>.clean_formsE  sQ   
 u12 "  2vcN)3|+<=jI FE	 %1LLN##'G!$6$E$EEjMX5",,a(}/Q%a7&'G&3At&<G#NN 34'')!"(.B')1W'W %'F!"%=>=Q=Q : S)#GU3*%a89@DMM!''A+6 $(#3#3G#<AaDG%L  bE#|,!#}5%c40AHHSYY[)Cc65)5= e  *!" $2 % $% "8, s[   1H 	BH.H(H.1HAH.HHH.	H+(H.*H++H..I Iz	/Contents)r   r   r  ra  rY   ra   rf  rb   rc   rd   rg   r`   r;   r   r   r>   r   r   r   r   get_contentsreplace_contents)
r   r   rj  to_dr  r  r  r  r  r  s
   ` `    @@@rk   ra  z"PdfWriter.remove_objects_from_page  s    i$/! :--dD9:)%7888)///00zBB)55500H  )44400x@@)99900t<<)8884- QR >> '	  )...7N	= 	$s) 	DI 	RV 	,E	!!E	!*./?*@E	!49d3i'(E	! E	!N $
+dD$;$;<D$=$*;*;*=>G'b1MFE'65)!!'* rj   c                    t        |t              rt        j                  }|t        j                  z  rt
        j                  nt
        j                  |t        j                  z  rt
        j                  nt
        j                  z  |t        j                  z  rt
        j                  nt
        j                  z  }| j                  D ]  }| j                  ||        y)z
        Remove images from this output.

        Args:
            to_delete : The type of images to be deleted
                (default = all images types)

        N)r   r  r,   ALLre   rY   r]   rf   rg   r   ra  )r   rj  r  r   s       rk   remove_imageszPdfWriter.remove_images  s     i&!I y777 #11',, y666 #00',,	 y777 #11',, 	
" JJ 	3D))$2	3rj   c                 f    | j                   D ]"  }| j                  |t        j                         $ y)zRemove text from this output.N)r   ra  rY   r`   rb  s     rk   remove_textzPdfWriter.remove_text  s.    JJ 	ID))$0B0G0GH	Irj   urirectborderc                 D   | j                  | j                        t        j                     |   }t	        t
        t        t        f   | j                  |            }|`|dd D cg c]  }t        |       }}t        |      dk(  rWt        |d   D cg c]  }t        |       c}      }	|j                  |	       n t        d      t        d      t        d      g}t        |t              rt        |      }nt        |t              rnt        |      }t               }
|
j                  t!        d      t!        d      t!        d      t#        |      i       t               }|j                  t!        t$        j&                        t!        d      t!        t$        j(                        t!        d      t!        t$        j*                        |t!        t$        j,                        |t!        d	      t!        d
      t!        t$        j.                        t        |      t!        d      |
i       | j1                  |      }t2        j4                  |v r#|t2        j4                     j                  |       yt        |g      |t!        t2        j4                        <   yc c}w c c}w )a  
        Add an URI from a rectangular area to the specified page.

        Args:
            page_number: index of the page on which to place the URI action.
            uri: URI of resource to link to.
            rect: :class:`RectangleObject<pypdf.generic.RectangleObject>` or
                array of four integers specifying the clickable rectangular area
                ``[xLL, yLL, xUR, yUR]``, or string in the form
                ``"[ xLL yLL xUR yUR ]"``.
            border: if provided, an array describing border-drawing
                properties. See the PDF spec for details. No border will be
                drawn if this argument is omitted.

        NrN  r  r   r*  z/URIz/Annotrl  z/Hr  /A)r   r   r   r   r   r   r   r
   rD   r   r8   r   r   rF   r>   rw   rB   rH   rd  r   SubtypePre  Borderr   r   r  )r   r  r  r  r  	page_linkrP  r  
border_arrdash_patternlnk2lnklnk_refs                rk   add_urizPdfWriter.add_uri  s   , OODKK09+F	S#X	(BC 39"1:>a,q/>J>6{a*VAY+OLO+OP!!,/&q/<?LOLJdC %Do."4(D!4 *V"46"$4S$9	
  

277#Z%92::&
7(;244 )277#T4 *T"2299%{:'>4 $
	
 ""3'99 RYY&&w/.97).DHZ		*+M ?+Os   %JJ)z	/NoLayoutz/SinglePagez
/OneColumnz/TwoColumnLeftz/TwoColumnRightz/TwoPageLeftz/TwoPageRightc                 \    	 t        t        | j                  d         S # t        $ r Y y w xY w)N/PageLayout)r   rR   r   r   r   s    rk   _get_page_layoutzPdfWriter._get_page_layout
	  s1    	
D$5$5m$DEE 		    	++layoutc                     t        |t              sG|| j                  vr.t        dddj	                  | j                        f t
               t        |      }| j                  j                  t        d      |i       y)a  
        Set the page layout.

        Args:
            layout: The page layout to be used.

        .. list-table:: Valid ``layout`` arguments
           :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

        zLayout should be one of: r   r  N)r   rB   _valid_layoutsr&   rq  rZ   r   rw   r   r  s     rk   _set_page_layoutzPdfWriter._set_page_layout	  sp    4 &*-T000/BGGD<O<O4P0P/QR  'F  *]";V!DErj   c                 &    | j                  |       y)a  
        Set the page layout.

        Args:
            layout: The page layout to be used

        .. list-table:: Valid ``layout`` arguments
           :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  s     rk   set_page_layoutzPdfWriter.set_page_layout3	  s    4 	f%rj   c                 "    | j                         S )a  
        Page layout property.

        .. 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
        )r  r   s    rk   page_layoutzPdfWriter.page_layoutO	  s    . $$&&rj   c                 &    | j                  |       y rq   r  r  s     rk   r  zPdfWriter.page_layouth	  s    f%rj   )z/UseNonez/UseOutlinesz
/UseThumbsz/FullScreenz/UseOCz/UseAttachmentsc                 \    	 t        t        | j                  d         S # t        $ r Y y w xY w)N	/PageMode)r   rU   r   r   r   s    rk   _get_page_modezPdfWriter._get_page_modeu	  s1    	d&7&7&DEE 		r  c                 "    | j                         S )a  
        Page mode property.

        .. 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
        )r  r   s    rk   	page_modezPdfWriter.page_mode{	  s    * ""$$rj   r  c                     t        |t              r|}nE|| j                  vr,t        ddj	                  | j                         t
               t        |      }| j                  j                  t        d      |i       y )NzMode should be one of: z, r  )r   rB   _valid_modesr&   rq  rZ   r   rw   )r   r  	mode_names      rk   r  zPdfWriter.page_mode	  sm    dJ'$(I4,,,-dii8I8I.J-KLh #4(I  *["99!EFrj   
annotationc           
      ^   |}t        |t              r| j                  |   }nt        |t              st	        d      t        t        t        |            }|j                  |t        d      <   |j                  t               |t        d      <   |j                  J |j                  d      dk(  rxd|v rtt        t        t        t        f   |t        d               }t        t        d      |d   t!        |d	   t#        |      d
               }|j$                  |t        d      <   |j                  j'                  | j)                  |             |j                  d      dk(  rDt        d      |v r7|j                  t        t        |d   j+                               t        d      <   |S )a  
        Add a single annotation to the page.
        The added annotation must be a new annotation.
        It cannot be recycled.

        Args:
            page_number: PageObject or page index.
            annotation: Annotation to be added (created with annotation).

        Returns:
            The inserted object.
            This can be used for popup creation, for example.

        zpage: invalid type/Pr  r\  rl  /Destz	/LinkNametarget_page_indexr;  fit_args)fit_typer  z/Popupr  )r   r   r   r   r  r   r>   _pdf_objectifyr   rB   annotationsr8   r   r   r
   r=   r?   dictr&  r   r   r   )r   r  r  r   to_addtmpr#  s          rk   add_annotationzPdfWriter.add_annotation	  s   & dC ::d#DD*-011&z(BC#'#:#:z$ #*5-DI&'+++ ::j!W,F1BtCH~vj.A'BCC;''( Z$s)J2GD +///F:g&' 0 0 89::j!X-*Y2G62Q )) !6)#4#?#?#AB8$ rj   c                    t        d|j                               }|j                  dg       D ]  }|j                         }|j                  dd      }|j                  dd      }t        |t              rt        |      |t	        d      <   _|b|j                         }|j                  dd      }t        |t              st        |      |t	        d      <    |S )z
        Perform some clean up in the page.
        Currently: convert NameObject named destination to TextStringObject
        (required for names/dests list)

        Args:
            page:

        Returns:
            The cleaned PageObject

        r   r  r  Nr  /D)r   r   r   r   rB   rH   )r   r   aa_objr  acts         rk   
clean_pagezPdfWriter.clean_page	  s     L$//"34)R( 
	@ALLNE		'4(A))D$'C!Z(-=a-@j)*nn&GGD$'a,,<Q,?C
4()
	@ rj   c                    d }t        |t        t        f      r2t        |d      5 }t	        |j                               }d d d        ||fS t        |t              r|j                  r|j                  }|j                  j                         }|j                  j                  d       t	        |j                  j                               }|j                  j                  |       ||fS t        |d      r<t        |d      r0|j                  d       |j                         }t	        |      }||fS t        d      # 1 sw Y   |fS xY w)Nr   r   r   rr   zMerging requires an object that PdfReader can parse. Typically, that is a Path or a string representing a Path, a file object, or an object implementing .seek and .read. Passing a PdfReader directly works as well.)r   r   r   r   r   rr   r    r   rl   r   r   r  rR  )r   r~   encryption_objr   rl   	orig_tellfilecontents          rk   _create_streamzPdfWriter._create_stream	  s/    gT{+& +! *+, ~%%) +""!(!4!4++-INN"W^^0023F NN	* ~%% Wf%''6*BLLO!,,.K[)F ~%% &> !+, ~%%s   D==E	r   import_outlineexcluded_fields.c                     |d}t        |t        t        t        f      r=t        |t              rt        |t              s|}|}|}| j                  d|d|||       y| j                  d|||||       y)aQ  
        Identical to the :meth:`merge()<merge>` method, but assumes you want to
        concatenate all pages onto the end of the file instead of specifying a
        position.

        Args:
            fileobj: A File Object or an object that supports the standard
                read and seek methods similar to a File Object. Could also be a
                string representing a path to a PDF file.
            outline_item: Optionally, you may specify a string to build an
                outline (aka 'bookmark') to identify the beginning of the
                included file.
            pages: Can be a :class:`PageRange<pypdf.pagerange.PageRange>`
                or a ``(start, stop[, step])`` tuple
                or a list of pages to be processed
                to merge only the specified range of pages from the source
                document into the output document.
            import_outline: You may prevent the source document's
                outline (collection of outline items, previously referred to as
                'bookmarks') from being imported by specifying this as ``False``.
            excluded_fields: Provide the list of fields/keys to be ignored
                if ``/Annots`` is part of the list, the annotation will be ignored
                if ``/B`` is part of the list, the articles will be ignored

        Nri   )r   r  r   rN   r  merge)r   r~   r?  r   r  r  s         rk   r   zPdfWriter.append
  s    T " OlUD)$<=%&!.$7&4O!& EJJ JJrj   positionc                 	    t        |t              r|n! j                  |      \  }}t        |d      |d}|(t	        t        t        j                                    }nt        |t              r4t	        t        |j                  t        j                                     }n\t        |t              rnKt        |t              r t        |      dk  rt	        t        |       }nt        |t              st        d      i |D ]  }	t        |	t              r|	}
nj                  |	   }
|
j                  J |8 j                  |
t	        |      g dz         |
j                  j                  <   n= j!                  |
|t	        |      g dz         |
j                  j                  <   |dz  }|
|
j                  j                     _         j$                  _        d	t(        d
df fd}j&                  j+                         D ]
  } ||        |dt-        d j/                  t1        |      t3        t5        j+                                     j                  t6              j9                               }n j;                         }j<                  }|rVt>        j@                  |v rD jC                  |jE                  t>        j@                  d            } jG                  ||d       d|vrpj+                         D ]]  } jI                  |j"                  jE                  dd      |      }t        |      dkD  r||tK        d      <    jM                  |       _ d|v r*|d   $d jN                  vr^ jQ                  t-        tR        j<                  d         jU                   dd             jN                  tK        d      <   tW               }n.t-        tV        t-        tR         jN                  d         d         } jX                  t[                 }	 j<                  d   d   D ]2  }	 t]        ||j                     d       }||vr|j_                  |       4 	 |t-        tR         jN                  d         tK        d      <   d|vr jc                  d       yy# t`        $ r Y w xY w# t`        $ r  jQ                  tW                     }Y uw xY w)a  
        Merge the pages from the given file into the output file at the
        specified page number.

        Args:
            position: The *page number* to insert this file. File will
                be inserted after the given number.
            fileobj: A File Object or an object that supports the standard
                read and seek methods similar to a File Object. Could also be a
                string representing a path to a PDF file.
            outline_item: Optionally, you may specify a string to build an outline
                (aka 'bookmark') to identify the
                beginning of the included file.
            pages: can be a :class:`PageRange<pypdf.pagerange.PageRange>`
                or a ``(start, stop[, step])`` tuple
                or a list of pages to be processed
                to merge only the specified range of pages from the source
                document into the output document.
            import_outline: You may prevent the source document's
                outline (collection of outline items, previously referred to as
                'bookmarks') from being imported by specifying this as ``False``.
            excluded_fields: provide the list of fields/keys to be ignored
                if ``/Annots`` is part of the list, the annotation will be ignored
                if ``/B`` is part of the list, the articles will be ignored

        Raises:
            TypeError: The pages attribute is not configured properly

        F)strictNri   rN  z:"pages" must be a tuple of (start, stop[, step]) or a list)r   /Br   r  r   r#  rn   c                    | j                   }dj                  v rL| d   t        t        t           t        t
        t        t
        j                  d         d         d         v ry t        | d   t              ry t        | d   t              rp	 j                  | d      }|j                  J 	 t        |j                  j                     j                        |t        d      <   j                  | d   |       y | d   j                  j                  v rH| d   j                  j                     j                  |t        d      <   j                  | d   |       y y # t        $ r Y y w xY w# t         $ r Y y w xY w)Nr)  rZ  z/Dests/Pager   )r&  r   r   r   r
   r>   r   rC   r   r   rK  r   rD   r   r  rX  r   )r#  arrr  r?  r   srcpagess      rk   _process_named_destsz-PdfWriter.merge.<locals>._process_named_dests
  s   //C4,,,h4S	$)4+<+<X+FGQ D 2 DM:6DM3/T']3A ++777+7 !5!5!;!;<HH,CQ( 44T(^SI g11778C'/M44::($$ LO$ 00hE	 D "    s%   E# 1AE2 #	E/.E/2	E>=E>rI   )r;  r  r   z	/AcroForm)/Fieldsr  r  r   )2r   r   r  r    r   rB  r   r   rN   indicesr  r  r   r   r  r   r  original_pagenamed_destinations_named_destinationsr
   r  r   rJ  rH   nextrv   r7   r   r-  r   r   r*  _get_filtered_outliner   _insert_filtered_outline_insert_filtered_annotationsrB   r  r   r   r>   r   r8   r   r   rA   r   r   add_filtered_articles)r   r  r~   r?  r   r  r  rl   r  r   pgr  r#  outline_item_typ_ror+  pagr  r  trslatr   indr?  r  s   `                     @@rk   r  zPdfWriter.mergeW
  s   L g|,F%)%8%8%A"FN ve4F" O=s6<<012Ey)c&,,.? @ABEt$u%#e*/'EE5)L   	ED$
+\\$'((444 9=_-0GG9..445 9=8H8H$"7:Q"Q9..445 ABDHR**001?#	E( %% 	" 	Fs  	Ft  	FD ..557 	'D &	' ##%%$\2hoo/01DD  &  *,   $446  bkkS000T*HfG )))4 O+( %77%%)))R8#x s8a<14C
9-.$% ##k"2">$"3"33=A=M=M(**;7 eD%6	>!!*["9: "m)4+<+<[+IJ9U ((F4F6++K8C A,VAGG_aFc>JJsO	  !4#4#4[#AB9% &&&r8V< ' $    6&&{}56s6   (S >/R2-S 2	R>;S =R>>S "S&%S&threadc           
      2   |j                  | dd      }| j                  j                  |j                         t	        d|d         }|}d}|| j                  t	        d|d         ||      }|B|Lt	        d| j                  t                     j                               }|}	|j                  |t        d      <   nat	        d| j                  t        t        d	      |j                  i            j                               }
|
j                  |t        d
      <   |
}||t        d      <   |j                  |t        d      <   |d   |t        d      <   t	        d|j                               }d|vrt               |t        d      <   t	        d|d         j                  |j                         t	        d|d
         }||k(  r2	j                  |t        d
      <   |j                  |	t        d	      <   d}||j                  J |j                  S )z
        Clone the thread with only the applicable articles.

        Args:
            thread:
            pages:
            reader:

        Returns:
            The added thread as an indirect reference

        T)r5  )force_duplicateignore_fieldsr>   r5  Nr   r  rV  ra  r  z/Rr  r8   )r   r0  r   r   r   _get_cloned_pager   r>   r   rB   r8   )r   r  r   r?  nthreadfirst_articlecurrent_articlenew_articler  	new_firstnew_article2pag_objs               rk   _add_articles_threadzPdfWriter._add_articles_thread  s$   $ ,,$g  
 	G667/>6C26)''\?4#895&C &"&*(()9);<GGI#K !,I0;0N0NGJt,-#'*((,!+D!1;3Q3Q R %*,$L 5A4S4SK
4 01".K03Jt,-070J0JJt,-0?0EJt,-|S^^-=>w&0;GJt,-]GDM29922 ##5t7LMO-/090L0LJt,-.9.L.L	*T*+"&I )J ))555)))rj   fltrc                 V   t        |t              rt        j                  |      }n%t        |t              st        j                  d      }|j                         D ]  }|j                  }|j                  dd      D ]  }|j                         j                  d      }|%|j                         }|j                  j                  | j                  t        |         vsd|j                  |j                  di       j                  dd            s| j                  |||         y)z
        Add articles matching the defined criteria.

        Args:
            fltr:
            pages:
            reader:

        r   r  ri   r  Nr  rZ  )r   r   recompiler   r  r  r   r   r   r   r   r   searchr  )r   r	  r   r?  r  ppr  thrs           rk   r  zPdfWriter.add_filtered_articlesV  s    " dC ::d#DD'*::b>D 	BABVVD"% 	Blln((.;..*C))//t7J7JvJ8 kk3774#4"9"9(B"GH--c5&A	B	Brj   c                     t        |t              ry t        |t              r"|j                  dd      dk(  r|j                  }nt        |t
              r|}	 |j                     j                  S # t        $ r Y y w xY w)NrZ  r   r  )r   rC   r>   r   r   rA   r   r   )r   r   r   r?  _is        rk   r   zPdfWriter._get_cloned_pagex  ss     dJ'./DHHWb4IW4T((Bn-B	?555 		s   A0 0	A<;A<r  c                    t               }t        |t              rt        d|j	                               }|D ]:  }t        d|j	                               }|d   dk7  sd|vst        d|d         d   dk7  sd|v rd|vr0|j                  | j                  |j                  |                    x|d   }t        |t              rGt        |      | j                         v s|j                  |j                  |       j                         t        d	|      }| j                  |d
   ||      }	|	|j                  | d      }
t        |	g|dd  z         |
t        d      <   |j                  | j                  |
             Lt        d|d         d   }t        |t              rIt        |      | j                         v s|j                  |j                  |       j                         t        d	|      }| j                  |d
   ||      }	|	|j                  | d      }
t        |	g|dd  z         t        d|
d         t        d      <   |j                  | j                  |
             = |S )Nz	List[Any]r>   r\  rl  r  r*  rI  r  r8   r   )r  )r  r   r  )r  )r8   r   rA   r   r   r   r   r   r   rV  r   r   rB   )r   r  r   r   r?  outlistri  r  r  r  ancs              rk   r  z&PdfWriter._insert_filtered_annotations  s:    -fn-+v'8'8':;F %	>B)2==?;CJ7*s?*CI6t<Gc>#%NN4#3#3CIIdO#DEGA!!S)q6T%=%=%??#NN399T?+M+MN 2 11!A$vF="%))D
)"KC7BA312;7OC
7 34#NN4+;+;C+@A+SY7=a%1v!9!9!;;syy'I'IJ]A.A--adE6BA}!iiGiD (ae4 /T;&t,  t'7'7'<=K%	>L rj   r   c                    g }|
t               }|j                         }t        |      r
t               }|j	                  dd      dk(  sd|vr<|j	                  dd      }|&|j                         }|| j                  |||      z  }|S ||j                         }t        d|j                  |            }| j                  t        d|d	         ||      }|
t               }||t        d	      <   d|v r| j                  |d   ||      |_
        ng |_
        t        |d	   t               rt        |j                        d
kD  r|j                  |       |j	                  dd      }||S )z
        Extract outline item entries that are part of the specified page set.

        Args:
            node:
            pages:
            reader:

        Returns:
            A list of destination objects.

        NrZ  r   	/OutlinesrZ  /Firstr=   r   r  r   /Next)rC   r   rM   r>   r   r  r   _build_outline_itemr   rB   _filtered_childrenr   r   r   )r   r   r   r?  new_outliner
  r   s          rk   r  zPdfWriter._get_filtered_outline  ss   $ <<D 4 #%D88GR K/843G88Hd+D(t99$vNN, ' "((B(B4(HI))$|QwZ*H%QWX9"A)**W%&t#+/+E+EXv,A( ,.A("1W:z:1//014&&q)xx.% "& rj   c           
      h   t               }| j                  |       t        |d         |t        d      <   t	        |d   t
              s]|j                  9d|j                  v r+|j                  d   j                  |       |t        d      <   n|j                  |t        d      <   |j                  t        |j                  j                  dd            |t        d      <   t        |j                  j                  dt        d      t        d      t        d      g            |t        d      <   |S )	NrZ  r  r  r  r5  r   /Cg        )rI   r   rH   rB   r   rC   r   r   r&  rD   r   r8   r@   )r   r#  n_ols      rk   _clone_outlinezPdfWriter._clone_outline  s    |%5d8n%EZ!"$w-4yy$):)-4)>)>t)DZ%&,0OOZ()99 %1$))--a2H%IDD!"%0		;s+[-={3?OP&DD!"
 rj   outlinesc                    |D ]{  }|j                  dd      dk(  sd|vr|}n@| j                  |      }t        t        |j	                               j                  |||        | j                  |j                  |d        } y )NrZ  r   r  rZ  )r   r  r   rI   r   r<  r  r  )r   r   r4  r5  r#  nps         rk   r  z"PdfWriter._insert_filtered_outline  s      	MD xx$3xt7K((.Z!2!2!45BB2vtT))$*A*A2tL	Mrj   c                      y)z"Implemented for API harmonization.Nri   r   s    rk   r  zPdfWriter.close  s    rj   c                 L   || j                         }nt        d|      }d}||j                  |k(  s|j                  dd       |k(  r|gS d|v r4| j	                  |t        t
        |d               }|rd|v r|g|z   S g |z   S d|v r|dz  }t        t        |d         }ny |y y )NrI   r   rZ  r  r  r   )r-  r   r   r   find_outline_itemrT   rI   )r   r?  r   r
  r  ress         rk   r%  zPdfWriter.find_outline_item  s    
 <%%'A\4(Am$$4554(L8s
Q,, ${AhK"@ #+q=QCC??bC??!|QQwZ0! mrj   c                     t        ddd       y)zZ
        .. deprecated:: 2.9.0
            Use :meth:`find_outline_item` instead.
        find_bookmarkr%  z5.0.0N)r%   )r   r?  r   s      rk   r(  zPdfWriter.find_bookmark3  s     	%_6I7Srj   c                    |i | _         yt        |t              r	 | j                   t        |      = yt        |t
              r"	 | j                   t        |j                        = yt	        d      # t        $ r Y yw xY w# t        $ r Y yw xY w)a3  
        Reset the translation table between reader and the writer object.

        Late cloning will create new independent objects.

        Args:
            reader: PdfReader or IndirectObject referencing a PdfReader object.
                if set to None or omitted, all tables will be reset.

        Nzinvalid parameter {reader})r   r   r    r   r   rA   r   )r   r?  s     rk   reset_translationzPdfWriter.reset_translation>  s     >"$D	*''6
3 /''6::7 899  
  s#   A0  A? 0	A<;A<?	B
Bpage_index_frompage_index_tostyleprefixstartc                     ||t        d      |dk  rt        d      ||k  rt        d      |t        | j                        k\  rt        d      ||dk7  r|dk  rt        d      | j                  |||||       y)	aK  
        Set a page label to a range of pages.

        Page indexes must be given starting from 0.
        Labels must have a style, a prefix or both.
        If a range is not assigned any page label, a decimal label starting from 1 is applied.

        Args:
            page_index_from: page index of the beginning of the range starting from 0
            page_index_to: page index of the beginning of the range starting from 0
            style: The numbering style to be used for the numeric portion of each page label:

                       * ``/D`` Decimal Arabic numerals
                       * ``/R`` Uppercase Roman numerals
                       * ``/r`` Lowercase Roman numerals
                       * ``/A`` Uppercase letters (A to Z for the first 26 pages,
                         AA to ZZ for the next 26, and so on)
                       * ``/a`` Lowercase letters (a to z for the first 26 pages,
                         aa to zz for the next 26, and so on)

            prefix: The label prefix for page labels in this range.
            start:  The value of the numeric portion for the first page label
                    in the range.
                    Subsequent pages are numbered sequentially from this value,
                    which must be greater than or equal to 1.
                    Default value: 1.

        Nz.At least one of style and prefix must be givenr   z/page_index_from must be greater or equal than 0z;page_index_to must be greater or equal than page_index_fromz%page_index_to exceeds number of pagesr   z1If given, start must be greater or equal than one)r   r   r   _set_page_label)r   r+  r,  r-  r.  r/  s         rk   set_page_labelzPdfWriter.set_page_labelZ  s    H =V^MNNQNOO?*M  C

O+DEE!	PQQ_mUFERrj   c                    t               }t        d      |t        d      <   t               }|t        |      |t        d      <   |t        |      |t        d      <   |dk7  rt        |      |t        d      <   t        t        j
                        | j                  vr^t               }t        t        d      ||       t               }	||	t        d      <   |	| j                  t        t        j
                        <   t        t        | j                  t        t        j
                                 }	t        t        |	t        d               }t        t        |      ||       t        t        |      ||       t        t        |      |      ^}
}|
|dz   k7  r4|dz   t        | j                        k  rt        t        |dz         ||       ||	t        d      <   |	| j                  t        t        j
                        <   y)	a   
        Set a page label to a range of pages.

        Page indexes must be given starting from 0.
        Labels must have a style, a prefix or both.
        If a range is not assigned any page label a decimal label starting from 1 is applied.

        Args:
            page_index_from: page index of the beginning of the range starting from 0
            page_index_to: page index of the beginning of the range starting from 0
            style:  The numbering style to be used for the numeric portion of each page label:
                        /D Decimal Arabic numerals
                        /R Uppercase Roman numerals
                        /r Lowercase Roman numerals
                        /A Uppercase letters (A to Z for the first 26 pages,
                           AA to ZZ for the next 26, and so on)
                        /a Lowercase letters (a to z for the first 26 pages,
                           aa to zz for the next 26, and so on)
            prefix: The label prefix for page labels in this range.
            start:  The value of the numeric portion for the first page label
                    in the range.
                    Subsequent pages are numbered sequentially from this value,
                    which must be greater than or equal to 1. Default value: 1.

        r  r*  Nr  r   z/Stz/Numsr   )r>   rB   rH   rD   r)   PAGE_LABELSr   r8   r   rI   r   r   r   r   r   )r   r+  r,  r-  r.  r/  default_page_labelnew_page_labelnumspage_labelsnext_label_posr  s               rk   r1  zPdfWriter._set_page_label  s   B .//9$/?:d+,)+/9%/@N:d+,/?/GN:d+,A:0<U0CN:e,-'334D<M<MM=DQ);TB$,K/3K
7+,KVDj):)F)FGH))*5F5R5R*ST
 KZ-@!ABL1>4Ho6tL&|O'DdK]Q..=13Ds4::3V]Q%679KTR+/Jw'(GR*%6%B%BCDrj   )r   NFF)rn   r}   )NN)ri   )T)r   ri   )NNr   rq   )r   r   )rn   N)TT)NNT)NNTN)NNTri   )rZ   r[   r\   __doc__r   r    r!   r   r  r   propertyr   r>   r   r   r   setterrA   rV   r   r   r   BaseExceptionr   r   r   r   r   r
   r   r   rr  rE   r   r   r   r   r   r  r  rJ   r  r  r  rC   r  rk  r  decimalDecimalr  r=   rH   r:   r   r2  r>  r   rH  r  rl  rn  
FFBITS_NULr   r  r  r  r   r  r  ALL_DOCUMENT_PERMISSIONSr0   r  r"   r  r   r	   r   r  r  r  r  r  r   r  r  r  r8   r9   r@   rB   rD   r"  r$  r(  rI   r-  r1  r0  r:  rS   rB  r7   r?   rJ  rS  rX  r[  r_  rc  rP   rg  rf  rY   ra  r,   r  r  r  rF   r  r  rR   r  r  r  r  r  rU   r  r  r  r  r   r   r  rN   r   rO   r  r  r   r  r   r  r  r  r  r  rT   r%  r(  r*  r.   r2  r1  __classcell__)r"  s   @rk   r}   r}      s   , >@@D!xQtYT9:xQ $	;<=xQ 	xQ
 xQ 
xQv d   !- ! ! 
x 01 
 
 \\C8E.:J*J$KL CQU C C Ch~6 C C -(>": -t - -%4./% m$% M*	%
 
% /3.2tXc]*+ tXc]*+ 
c3h	: 
%C 
% 
% "U3:%6 "4 " "
&y &^ &!#~"56 
!#~"56  
	2 (*	-- -  }	-
 
-^  B+<  (*NN  }N 
	N8 ')	>> >  }	>
 
>:"'c:~(M"N	#2 HL#e_#5=e_#	#6 :>:>	 eW__456  ugoo567  	 
 
 D (	t["24DD	E( (
 U4k:+M%N SW  &"- "- "-HY
s Y
%U
2C Y
 Y
| EI// $Hj\4-?$@A/ 
	/J R@R@ R@ 	R@
 R@ 
R@h 1J &*.jTJZ 0$67jT S#XjT yy	jT
 "$jT 
jTZ ,01Z(1		1f%R %Rt %RT EI.5.5 $Hj\4-?$@A.5 
	.5`J.> J+. )-2J9$ $(9$9$ !9$ 	9$
 09$ C=9$ 
9$v7: 7$ 70E$"34 tRW}9M >
4+? 
0EHz EHd EHN.: .%S	4PS9@T:U .* 48IMQRUY	 HZ H H H0  (#67     __%13CT#s(^STU% 
% % $sCx.  T  * #'#H$H$ H$ 
	H$T!
	
!
 
!
F*"*	*B ~ * &+ & ' ' ' ;?:>$$
J FG$$ dJ67$$ dJ67	$$
 $$ 
$$R ;?:>
%
 dJ67
 dJ67	

 
 

6 ;?:>BFLXLX 4^S@ALX dJ67	LX
 dJ67LX eE5%$78#=>?LX LX LX LX LX 
LX\

%49.+:U4V		$#	$ 
	$  
	.T
: '8(CT:U'U!VW:	: NJ0@@A 8C=) 
	$P+J 001P+ +X6H-IIJP+ 
	P+h  )}} 3 3 
 3DI )-AEAE AE 	AE
 %AE 
AEFN(:"6 !FuZ-C'D !F !FF&j &T &8 'Xj1 ' '0 &* & & &L 6  %8L1 % %, 	Gl 	Gt 	G 	G43
?+4 cN4 
	4luZ%?@ Z 6"&T;	9:"&	vx
++	,"&R  #GKB{It34B y%S/5c33GcR
B #s(O#sC- I
B B "%S	5c?(B"CDB  
!BP '+BF#GI|=3-|= t[)34|= sm	|=
 mT*-==>?|= |= "%S	5c?(B"CD|= 
|=|?* ?* CO$?* 	?*
 
?*B BCL#
 B
 CO$ B  B 
 BDD.*j@A CO$ 	
 
.	!"0nd+;&<<=0 0 CO$	0
 0 
k	0d22 CO$2 	2
 
k	2h; : . ;?	M{#M j.01M dJ67	M
 
M  '+38n {# 
$s)		@ '+	T38n	T {#	T 
		T @D:D)^;<:	:@ +/ $ 1S1S 1S '	1S
 1S }1S 
1Sn +/ $ ?S?S ?S '	?S
 ?S }?S 
?Srj   r}   r   c                    t        | t              r| S t        | t              r?t               }| j	                         D ]   \  }}t        |      }t        |      }|||<   " |S t        | t              r'| j                  d      rt        |       S t        |       S t        | t        t        f      rt        |       S t        | t              rt        d | D              S t        dt!        |       d      )NrH  c              3   2   K   | ]  }t        |        y wrq   )r  )r  r  s     rk   r  z!_pdf_objectify.<locals>.<genexpr>  s     :>!,:r  z
type(obj)=z! could not be cast to a PdfObject)r   rE   r  r>   r   rB   r  r   
startswithrH   r   rk  r@   r   r8   rR  type)r   r  r  r   name_key
cast_values         rk   r  r    s    #y!
#t!#))+ 	*JC!#H'.J)F8	* 	C	>>#c?"#C((	C#u	&3	C	:c:::!tCyl;<
 	
rj   rO  rC  rD  rF  rE  c                    t               }| | |t        d      <   |j                  t        d      t        |      i       |rWt	        |t
              rt        |      }|j                  t        d      t        |D cg c]  }t        |       c}      i       |s|r5d}|r|dz  }|r|dz  }|j                  t        d      t        |      i       |S c c}w )Nr  rZ  r  r   r   r   r5  )
rI   rB   rw   rK   r   r   rL   r8   r@   rD   )rO  rC  rD  rF  rE  r?  cformat_flags           rk   rN  rN    s     <L)3Z%&x "6u"=	

 eS!u%E{E+JqKN+JKL	
 1K1KZ-|K/HIJ ,Ks   ;Cr  r  r{  r  rz  r~  r  c                    d|j                   dz
   d|j                  dz
   d| dj                         }t        | j	                  dd      j                  d            D ]  \  }}	|	|v r<|d|||z  dz  z
  dz
   d|j                   d	z
   d|d	z    d
| d	j                         z  }|dk(  r|d| dj                         z  }n|d| dz   dj                         z  }|	D 
cg c]#  }
|j                  |
|
j                  d            % }}
t        d |D              r7|ddj                  |      j                         j                         z   dz   z  }|ddj                  |      z   dz   z  } |dz  }|S c c}
w )Nzq
/Tx BMC 
q
1 1 r   rP  z	 re
W
BT
rO  rQ  z1 gffffff?r   z re
0.5 0.5 0.5 rg s
r   z2 z Td
r  z	utf-16-bec              3   8   K   | ]  }t        |      d k\    yw)r   N)r   )r  rJ  s     rk   r  z-generate_appearance_stream.<locals>.<genexpr>   s     -qs1v{-s      <rj   s   > Tj
   (s   ) Tj
s   ET
Q
EMC
Q
)
r  r  r   r  ri  rj  r   r  rq  hex)r  r  r{  r  rz  r~  r  r  line_numberlinerJ  enc_lines               rk   rv  rv    s    'syy1}oQszzA~6FlSURVVXY``bI&s{{4'>'D'DT'JK ?T3;X{!:S!@AAEFa		TUVWXcfgXgWh i%%'D,fhI !2hZu-4466I 2mc12%8??AAIAE!
<=Ma+!67!
 !
 -H--(!3 8 8 : A A CCiOOI 22Y>>I%?& ##I!
s   "(E9)i   )r>  r^   rt   r  r  r-  ior   r   r   	itertoolsr   pathlibr   typesr   typingr	   r
   r   r   r   r   r   r   r   r   r   r   _cmapr   r   _doc_commonr   r   r   r   r   _pager   _page_labelsr   r   r   r   r    _utilsr!   r"   r#   r$   r%   r&   	constantsr'   rd  r(   r+  r)   r*   r+   r,   r-   r.   r/   r0   r1   r   r2   rl  r3   r   r4   r   r5   r  errorsr6   genericr7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   	pagerangerN   rO   rP   rQ   rR   rS   rT   rU   xmprV   allrA  rp  IntFlagrY   r   r   r{   r}   r  rk  r  rN  rr  rv  ri   rj   rk   <module>re     s  <    	   & &       H : 5  B B   < .	 	 	 " 6 + , (       2 0   40446 #%  
= 
=g # # y0S y0Sxa
d38nc3S	AB 
y 
2dN*+ ueU*+S$67 	
  >		c 	 U
#	
 
   rj   