
    jz_                     Z   d dl mZmZmZ d dlmZ d dlmZ d dlm	Z	m
Z
mZmZmZmZ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 dd
lmZ ddlmZm Z 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/m0Z0m1Z1 ddl2m3Z3m4Z4 ddlm5Z5m6Z6m7Z7 dZ8 G d d          Z9 G d d          Z:dS )    )BytesIOFileIOIOBase)Path)TracebackType)	AnyDictIterableListOptionalTupleTypeUnioncast   )
Encryption)
PageObject)	PdfReader)StrByteTypedeprecate_with_replacementstr_)	PdfWriter)GoToActionArgumentsTypArgumentsTypFitArguments)PagesAttributes)PAGE_FITArrayObjectDestinationDictionaryObjectFitFloatObjectIndirectObject
NameObject
NullObjectNumberObjectOutlineItemTextStringObject
TreeObject)	PageRangePageRangeSpec)
LayoutTypeOutlineTypePagemodeTypez=close() was called and thus the writer cannot be used anymorec                   *    e Zd ZdZdedededdfdZdS )_MergedPagez@Collect necessary information on each page that is being merged.pagedatasrcidreturnNc                 >    || _         || _        d | _        || _        d S N)r2   r1   out_pagedatar3   )selfr1   r2   r3   s       W/lsinfo/ai/hellotax_ai/base_platform/venv/lib/python3.11/site-packages/pypdf/_merger.py__init__z_MergedPage.__init__N   s#          )__name__
__module____qualname____doc__r   r   intr:    r;   r9   r0   r0   K   sJ        JJ )        r;   r0   c                      e Zd ZdZ	 d5dedeeef         ddfdZd6d	Z	d
e
ee                  de
e         de
e         ddfdZ	 	 	 d7dedeeeef         de
e         de
e         deddfdZdeeeef         deee
e         f         fdZ	 	 	 d7deeeef         de
e         dedeeeef         eeeef         ee         f         deddf
dZdeeef         ddfdZd8dZdeeef         ddfdZde ddfdZ!de"ddfdZ#e$de
e"         fd            Z%e%j&        de"ddfd            Z%d ed!eeeeef         f         deeeef         eeeef         ee         f         deeeef                  fd"Z'd ed#e(deeeef         eeeef         ee         f         de(fd$Z)d8d%Z*	 	 d9d#e
e+e,                  d&e
e-         ddfd'Z.dee,e/f         d(e0ddfd)Z1dee0         ddfd*Z2	 d:dee0         d#e
e+e,                  ddfd+Z3	 d:deeef         d,e
e(         de
ee                  fd-Z4dddde5fd.eded&ede-e6f         d/e
ee7e7e7f                  d0ed1ed2e8de6fd3Z9d.ededdfd4Z:dS );	PdfMergerzD
    Use :class:`PdfWriter` instead.

    .. deprecated:: 5.0.0
    F strictfileobjr4   Nc                     t          ddd           g | _        g | _        t                      | _        g | _        g | _        d| _        || _        || _	        d S )NrC   r   5.0.0r   )
r   inputspagesr   outputoutlinenamed_destsid_countrF   rE   )r8   rE   rF   s      r9   r:   zPdfMerger.__init__\   sW     	#;WEEE35 "
+4;;$&&(r;   c                 (    t          ddd           | S )NrC   r   rH   )r   r8   s    r9   	__enter__zPdfMerger.__enter__i   s    ";WEEEr;   exc_typeexc	tracebackc                 p    | j         r|                     | j                    |                                  dS )z*Write to the fileobj and close the merger.N)rF   writeclose)r8   rR   rS   rT   s       r9   __exit__zPdfMerger.__exit__n   s3     < 	%JJt|$$$

r;   Tpage_numberoutline_itemrJ   import_outlinec                 T   |                      |          \  }}t          || j                  }| j                            ||f           |||_        |dt          |j                  f}nwt          |t                    r(|
                    t          |j                            }n:t          |t                    rn$t          |t                    st          d          g }	g }
|r|j        }
|                     ||
|          }
|rTt!          t#          |          t%          | j                  t)          j                              }| xj        ||
gz  c_        n| xj        |
z  c_        |j        }|                     |||          }| xj        |z  c_        t3          | D ]L}|j        |         }| j        }| xj        dz  c_        t5          |||          }|	                    |           M|                     |	           |                     |	           |	| j        ||<   dS )a  
        Merge the pages from the given file into the output file at the
        specified page number.

        Args:
            page_number: 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 an outline item
                (previously referred to as a 'bookmark') to be applied at the
                beginning of the included file by supplying the text of the outline item.
            pages: can be a :class:`PageRange<pypdf.pagerange.PageRange>`
                or a ``(start, stop[, step])`` tuple
                to merge only the specified range of pages from the source
                document into the output document.
                Can also be a list of pages to merge.
           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``.
        )rE   Nr   z0"pages" must be a tuple of (start, stop[, step])r   )_create_streamr   rE   rI   append_encryptionlenrJ   
isinstancer*   indiceslisttuple	TypeErrorrL   _trim_outliner'   r(   r&   rN   r!   fitnamed_destinations_trim_destsrM   ranger0   _associate_dests_to_pages!_associate_outline_items_to_pages)r8   rY   rF   rZ   rJ   r[   streamencryption_objreadersrcpagesrL   outline_item_typdeststrimmed_destsipager3   mps                     r9   mergezPdfMerger.mergey   sE   < "&!4!4W!=!= 6$+666FF+,,,%!/F =FL))*EEy)) 	PMM#fl"3"344EEt$$ 	PE5)) 	PNOOO 	AnG((%@@G 	$* ..T]++		   
 LL-w77LLLLLG#LL)((>>M)  	  	 A<?DBMMQMMT62..BOOB&&x000..x888 /7
;{*+++r;   c                 f   d }t          |t          t          f          rt          |d          }nt          |t                    r|j        r|j        }|j                                        }|j                            d           t          |j        
                                          }|j                            |           nht          |d          rIt          |d          r9|                    d           |
                                }t          |          }nt          d          ||fS )Nrbr   seekreadzPdfMerger.merge 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.)ra   strr   r   r   r_   rm   tellrz   r   r{   hasattrNotImplementedError)r8   rF   rn   rm   	orig_tellfile_contents         r9   r]   zPdfMerger._create_stream   s&    gT{++ 	GT**FF++ 	" 5!(!4++--IN"""W^002233F N	****Wf%% 
	''6*B*B 
	LLOOO"<<>>L\**FF%>   ~%%r;   c                 \    |                      t          | j                  ||||           dS )a  
        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 an outline item
                (previously referred to as a 'bookmark') to be applied at the
                beginning of the included file by supplying the text of the outline item.
            pages: can be a :class:`PageRange<pypdf.pagerange.PageRange>`
                or a ``(start, stop[, step])`` tuple
                to merge only the specified range of pages from the source
                document into the output document.
                Can also be a list of pages to append.
            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``.
        N)rw   r`   rJ   )r8   rF   rZ   rJ   r[   s        r9   r^   zPdfMerger.append   s,    < 	

3tz??G\5.QQQQQr;   c                 \   | j         t          t                    | j        D ]}| j                             |j                   t          t          t          t          f         | j         j
                                                  }| j                             |t          j                 d                                                   |_        |                                  |                                  | j                             |          \  }}|r|                                 dS dS )z
        Write all data that has been merged to the given output file.

        Args:
            fileobj: Output file. Can be a filename or any kind of
                file-like object.
        N)rK   RuntimeErrorERR_CLOSED_WRITERrJ   add_pager1   r   r	   r|   r   _pages
get_objectget_referencePAKIDSr7   _write_dests_write_outlinerV   rW   )r8   rF   ru   	pages_objmy_fileret_fileobjs         r9   rV   zPdfMerger.write  s    ;0111
 J 	 	DK  ///T#s(^T[-?-J-J-L-LMMI $ 9 9"'"2&1133! !D
 	  ${0099 	 	  	 r;   c                 r    g | _         | j        D ]\  }}|                                 g | _        d| _        dS )zHShut all file descriptors (input and output) and clear all memory usage.N)rJ   rI   rW   rK   )r8   fo_readers      r9   rW   zPdfMerger.close2  s?    
; 	 	KBHHJJJJr;   infosc                 p    | j         t          t                    | j                             |           dS )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.
                An example is ``{'/Title': 'My title'}``
        N)rK   r   r   add_metadata)r8   r   s     r9   r   zPdfMerger.add_metadata;  s7     ;0111  '''''r;   layoutc                 p    | j         t          t                    | j                             |           dS )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)rK   r   r   _set_page_layout)r8   r   s     r9   set_page_layoutzPdfMerger.set_page_layoutH  s7    2 ;0111$$V,,,,,r;   modec                     || _         dS )W  
        Set the page mode.

        Args:
            mode: The page mode to use.

        .. list-table:: Valid ``mode`` arguments
           :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
        N)	page_moder8   r   s     r9   set_page_modezPdfMerger.set_page_modee  s    . r;   c                 P    | j         t          t                    | j         j        S )r   rK   r   r   r   rP   s    r9   r   zPdfMerger.page_mode~  s&    0 ;0111{$$r;   c                 T    | j         t          t                    || j         _        d S r6   r   r   s     r9   r   zPdfMerger.page_mode  s)    ;0111 $r;   pdfrr   c                    g }t          |t                    r|nt          t          |           }|                                D ]\  }}|D ]}|j        |                                         |d                                         k    ri|d                                         |t          d          <   t          |          t          |d                   k    sJ |                    |            n|S )z
        Remove named destinations that are not a part of the specified page set.

        Args:
            pdf:
            dests:
            pages:
        /Page/Title)	ra   rc   rj   itemsrJ   r   r$   r   r^   )	r8   r   rr   rJ   	new_destslstkeyobjjs	            r9   ri   zPdfMerger._trim_dests  s     	!%..GeeD4G4G 	 	HC  9Q<**,,G0G0G0I0III/27|/F/F/H/HC
7++,99S](;(;;;;;$$S)))E	 J
 r;   rL   c                 v   g }d}t          |t                    r|nt          t          |           }t          |          D ]\  }}t          |t                    rO|                     |||          }	|	r5|s|                    ||dz
                      |                    |	           id}|D ]}
|d         |j        |
                                         |d                                         k    rC|d                                         |t          d          <   |                    |           d} n|S )z
        Remove outline item entries that are not a part of the specified page set.

        Args:
            pdf:
            outline:
            pages:

        Returns:
            An outline type
        Tr   Fr   )	ra   rc   rj   	enumeraterf   r^   rJ   r   r$   )r8   r   rL   rJ   new_outlineprev_header_addedr   rt   rZ   subr   s              r9   rf   zPdfMerger._trim_outline  s_   "  !%..GeeD4G4G(11 	 	OA|,-- ((lC@@ ,, ;#**71q5>:::&&s+++$)! 	 	A#G,4 y|..00L4I4T4T4V4VVV<H#=$*,, %Z%8%89 $**<888,0) W r;   c                    | j         t          t                    | j        D ]h}d }d|v rDt	          | j                  D ]/\  }}|j        |d         k    r|j        |t          d          <    n0|| j         	                    |           id S )Nr   )
rK   r   r   rM   r   rJ   r3   r7   r$   add_named_destination_object)r8   
named_dest
page_indexru   s       r9   r   zPdfMerger._write_dests  s    ;0111* 		E 		EJJ*$$(1$*(=(=  $Jw*W"555:>:K
:g#6#67 6 %88DDD		E 		Er;   parentc                    | j         t          t                    || j        }|
J d            d }|D ]}t	          |t
                    r|                     ||           .d }d|v rCt          | j                  D ].\  }}|j	        |d         k    r| 
                    ||            n/|!|d= |d= | j                             ||          }d S )Nhint for mypyr   /Type)rK   r   r   rL   ra   rc   r   r   rJ   r3   _write_outline_item_on_pageadd_outline_item_dict)r8   rL   r   
last_addedrZ   page_noru   s          r9   r   zPdfMerger._write_outline  s   
 ;0111?lG""O"""
# 	U 	UL,-- ##L*===G,&&%.tz%:%:  MGTw,w"77788tLLL 8 " )<+@![>>|VTT
	U 	Ur;   ru   c                    t          t          |d                   }t          |j                  t	          |          g}t
          j        t          j        ft
          j	        t          j        ft
          j
        t          j        ft
          j        t          j        ft
          j        t          j        t          j        dft
          j        t          j        t          j        t          j        t          j        fi}|                    |d          D ]o}||v rDt%          ||         t&                    s)|                    t+          ||                              n"|                    t+          d                     ||= pt-          t	          t.          j                  t	          d          t	          t.          j                  t5          |          i          |t	          d          <   d S )Nr   z/ZoomrA   r   z/GoToz/A)r   r|   r&   r3   r$   r   FIT_Hr   TOPFIT_BHFIT_VLEFTFIT_BVXYZFIT_RBOTTOMRIGHTgetra   r%   r^   r"   r    r   SDr   )r8   rZ   ru   oi_typeargsfit2arg_keysarg_keys          r9   r   z%PdfMerger._write_outline_item_on_page  s    sL122TW%%z'':':;!L$4#6"\%5$7!L$5#7"\%6$8,"3\5Ew!O!!#" 	$4
 $''44 	& 	&G,&&zW%z0 0& KW(=>>????KNN+++W%%)9.011:g3F3F.011;t3D3D*
 *
Z%%&&&r;   c                 D   | j         D ]}d }|d         }t          |t                    r"|D ]8}|                                |j                                        k    r|j        }9|t          d|d          d          t          |          |t          d          <   d S )Nr   zUnresolved named destination 'r   ')rM   ra   r&   r   r1   r3   
ValueErrorr$   )r8   rJ   r   r   npru   s         r9   rk   z#PdfMerger._associate_dests_to_pages-  s    * 	G 	GJJG$B"l++  ) )==??dm&>&>&@&@@@!%J! LZ5ILLL   /;:.F.FJz'**++	G 	Gr;   c                    || j         }|
J d            |D ]}t          |t                    r|                     ||           .d }|d         }t          |t                    rN|D ]8}|                                |j                                        k    r|j        }9|t	          |          |t          d          <   d S )Nr   r   )	rL   ra   rc   rl   r&   r   r1   r3   r$   )r8   rJ   rL   rZ   r   outline_item_pageps          r9   rl   z+PdfMerger._associate_outline_items_to_pages?  s     ?lG""O"""# 	M 	ML,-- 66ulKKKJ ,W 5+\::  & &$//11QZ5J5J5L5LLL!"J%4@4L4LZ001!	M 	Mr;   rootc                 &   || j         }t          |          D ]w\  }}t          |t                    r!|                     ||          }|r|g|z   c S ;||k    s1t          t          t          t          f         |d                   |k    r|gc S xd S )Nr   )rL   r   ra   rc   find_outline_itemr   r	   r   )r8   rZ   r   rt   oi_enumress         r9   r   zPdfMerger.find_outline_itemW  s    
 <<D#D// 	 	JAw'4((  ,,\7CC %39$$$% <''S#X(9::lJJ s


 K
 tr;   titlecolorbolditalicrg   c           
      t    | j         }|t          t                    |                    |||d||||          S )aQ  
        Add an outline item (commonly referred to as a "Bookmark") to this 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.
            color: Color of the outline item's font as a red, green, blue tuple
                from 0.0 to 1.0
            bold: Outline item font is bold
            italic: Outline item font is italic
            fit: The fit of the destination page.
        N)rK   r   r   add_outline_item)	r8   r   rY   r   r   r   r   rg   writers	            r9   r   zPdfMerger.add_outline_itemo  sO    0 >0111&&	
 	
 		
r;   c                     t          t          |          t          |          t          j        d                    }| j                            |           dS )z
        Add a destination to the output.

        Args:
            title: Title to use
            page_number: Page number this destination points at.
        i:  )topN)r   r(   r&   r!   fit_horizontallyrM   r^   )r8   r   rY   dests       r9   add_named_destinationzPdfMerger.add_named_destination  sY     U##%% S)))
 

 	%%%%%r;   )FrD   )r4   rC   )NNT)r4   N)NNr6   );r<   r=   r>   r?   boolr   r   r   r:   rQ   r   r   BaseExceptionr   rX   r@   r   r|   r+   rw   r   r   r   r]   r*   r   r^   rV   rW   r	   r   r   r,   r   r.   r   propertyr   setterri   r-   rf   r   r
   r'   r)   r   r   r0   r   rk   rl   r   r   r#   floatr!   r   r   rA   r;   r9   rC   rC   U   s7         IK -243D-E	      
	4./	 m$	 M*		
 
	 	 	 	 '+)-#U7 U7U7 t[)34U7 sm	U7
 &U7 U7 
U7 U7 U7 U7n!&T;	9:!&	vx
++	,!& !& !& !&L '+ #R R{It34R smR )U38_eCcM.BDIM
	R R 
R R R R@ U4#45  $        >   ($sCx. (T ( ( ( (-j -T - - - -:, 4    2 %8L1 % % % X%6 %l %t % % % %
 Cc3h'( U38_eCcM&:DIEF	
 
d38n	   2'' ' U38_eCcM&:DIEF	'
 
' ' ' 'RE E E E  48'+U U(;/0U $U 
	U U U U6 
!+{":; 
CN 
	 
  
  
  
DGtK/@ GT G G G G& TXM M+&M19(;:O1PM	M M M M6 '+ 38n {# 
$s)		   8 ;?6:$
 $
$
 $
 dJ67	$

 eUE123$
 $
 $
 $
 
$
 $
 $
 $
L&& & 
	& & & & & &r;   rC   N);ior   r   r   pathlibr   typesr   typingr   r	   r
   r   r   r   r   r   r   r_   r   _pager   r   r   _utilsr   r   r   _writerr   	constantsr   r   r   r   r   genericr   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   	pageranger*   r+   r,   r-   r.   r   r0   rC   rA   r;   r9   <module>r      s  8 ' & & & & & & & & &            
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 $ # # # # #                     
       I I I I I I I I I I , , , , , ,                              0 / / / / / / / 8 8 8 8 8 8 8 8 8 8S        Q	& Q	& Q	& Q	& Q	& Q	& Q	& Q	& Q	& Q	&r;   