
    :Qgt                     v   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
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mZ ddlmZ ddl m!Z! ddl m"Z# ddl m$Z$m%Z% ddl&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m5Z5m6Z6m7Z7m8Z8m9Z9 dZ: G d d      Z; G d d      Z< G d de<      Z=y)    N)BytesIOFileIOIOBase)Path)TracebackType)	AnyDictIterableListOptionalTupleTypeUnioncast   )
Encryption)
PageObject)	PdfReader)StrByteTypedeprecate_bookmarkdeprecate_with_replacementstr_)	PdfWriter)GoToActionArguments)PagesAttributes)TypArgumentsTypFitArguments)ArrayObjectDestinationDictionaryObjectFloatObjectIndirectObject
NameObject
NullObjectNumberObjectOutlineItemTextStringObject
TreeObject)	PageRangePageRangeSpec)FitType
LayoutTypeOutlineTypePagemodeTypeZoomArgTypez=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y)_MergedPagez@Collect necessary information on each page that is being merged.pagedatasrcidreturnNc                 <    || _         || _        d | _        || _        y N)r3   r2   out_pagedatar4   )selfr2   r3   r4   s       K/var/www/html/answerous/venv/lib/python3.12/site-packages/PyPDF2/_merger.py__init__z_MergedPage.__init__O   s          )__name__
__module____qualname____doc__r   r   intr;    r<   r:   r1   r1   L   s&    J )   r<   r1   c                      e Zd ZdZ ed      	 d>dedeeef   ddfd       Z	d?d	Z
d
eee      dee   dee   ddfdZ edd      	 	 	 	 	 	 d@dee   deeeef   dee   dee   dedee   ddfd       Zdeeeef   deeee   f   fdZ edd      	 	 	 dAd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dBdZdeeef   ddfd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$de%ddfd Z&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*dBd&Z+ ed      	 	 dCdee,e-      d'ee.   ddfd(       Z/ ed)      dee-e0f   d*e1ddfd+       Z2dee1   ddfd,Z3 ed      	 dDdee1   dee,e-      ddfd-       Z4 ed)      	 dDdeeef   d.ee)   deee      fd/       Z5 ed)      	 dDdeeef   d.ee)   deee      fd0       Z6	 	 	 	 	 	 dEdd1d2edee   d'ede.e7f   d3eee8e8e8f      d4ed5ed6e9d7e:d8ee   de7fd9Z;	 	 	 	 	 dFd2ed8ed'ede.e7f   d3eee8e8e8f      d4ed5ed6e9d7e:de7fd:Z<	 	 	 	 	 dFd2ed8ed'ede.e7f   d3eee8e8e8f      d4ed5ed6e9d7e:de7fd;Z=d2ed8eddfd<Z>	 	 dCd2edee   d8ee   ddfd=Z?y)G	PdfMergeraI  
    Initialize a ``PdfMerger`` object.

    ``PdfMerger`` merges multiple PDFs into a single PDF.
    It can concatenate, slice, insert, or any combination of the above.

    See the functions :meth:`merge()<merge>` (or :meth:`append()<append>`)
    and :meth:`write()<write>` for usage information.

    :param bool strict: Determines whether user should be warned of all
            problems and also causes some correctable problems to be fatal.
            Defaults to ``False``.
    :param fileobj: Output file. Can be a filename or any kind of
            file-like object.
    outline)	bookmarksstrictfileobjr5   Nc                     g | _         g | _        t               | _        g | _        g | _        d| _        || _        || _        y )Nr   )	inputspagesr   outputrE   named_destsid_countrH   rG   )r9   rG   rH   s      r:   r;   zPdfMerger.__init__g   s?     46 "
+4;$&&(r<   c                     | S r7   rB   )r9   s    r:   	__enter__zPdfMerger.__enter__t   s    r<   exc_typeexc	tracebackc                 r    | j                   r| j                  | j                          | j                          y)z*Write to the fileobj and close the merger.N)rH   writeclose)r9   rQ   rR   rS   s       r:   __exit__zPdfMerger.__exit__x   s$     <<JJt||$

r<   outline_itemimport_outline)bookmarkimport_bookmarkspage_numberrK   positionc                    |0|#|}d}d}t        j                  | d| d       nt        d      |t        d      |t        d	      | j                  |      \  }	}
t	        |	| j
                  
      }| j                  j                  |	|f       |
|
|_        |dt        |j                        f}nat        |t              r%|j                  t        |j                              }n,t        |t              rnt        |t              st!        d      g }g }|r|j"                  }| j%                  |||      }|rXt'        t)        |      t+        | j,                        t/        t0        j2                              }| xj"                  ||gz  c_        n| xj"                  |z  c_        |j4                  }| j7                  |||      }| xj8                  |z  c_        t;        | D ]P  }|j                  |   }| j,                  }| xj,                  dz  c_        t=        |||      }|j                  |       R | j?                  |       | jA                  |       || j                  || y)a  
        Merge the pages from the given file into the output file at the
        specified page number.

        :param int page_number: The *page number* to insert this file. File will
            be inserted after the given number.

        :param 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.

        :param str 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.

        :param pages: can be a :class:`PageRange<PyPDF2.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.

        :param bool 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``.
        Nr]   r\   # is deprecated as an argument. Use  insteadmessagezCThe argument position of merge is deprecated. Use page_number only.page_number may not be Nonezfileobj may not be None)rG   r   z0"pages" must be a tuple of (start, stop[, step])r   )!warningswarn
ValueError_create_streamr   rG   rJ   append_encryptionlenrK   
isinstancer)   indiceslisttuple	TypeErrorrE   _trim_outliner&   r'   r%   rN   r#   r   FITnamed_destinations_trim_destsrM   ranger1   _associate_dests_to_pages!_associate_outline_items_to_pages)r9   r\   rH   rX   rK   rY   r]   old_termnew_termstreamencryption_objreadersrcpagesrE   outline_item_typdeststrimmed_destsipager4   mps                        r:   mergezPdfMerger.merge   sT   F "&%(#*$GzQYZ !Y   :;;? 677!%!4!4W!= 6$++6FF+,%!/F =FLL)*Ey)MM#fll"34Et$E5)NOOnnG((%@G* .T]]+?../ 
 LL-w77LLLG#L))((>M)  	 A<<?DBMMQMT62.BOOB	  	&&x0..x8 /7

;{+r<   c                 6   d }t        |t        t        f      rt        |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      )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.)rk   strr   r   r   ri   ry   tellr   r   r   hasattrNotImplementedError)r9   rH   rz   ry   	orig_tellfilecontents         r:   rg   zPdfMerger._create_stream   s    gT{+GT*F* ~%%) +""!(!4!4++-INN"W^^0023F NN	* ~%% Wf%''6*BLLO!,,.K[)F ~%% &> r<   c                 T    | j                  t        | j                        ||||       y)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.

        :param 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.

        :param str 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.

        :param pages: can be a :class:`PageRange<PyPDF2.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.

        :param bool 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)r   rj   rK   )r9   rH   rX   rK   rY   s        r:   rh   zPdfMerger.append  s!    B 	

3tzz?G\5.Qr<   c                 J   | j                   t        t              | j                  D ]  }| j                   j	                  |j
                         t        t        t        t        f   | j                   j                  j                               }| j                   j                  |t        j                     d   j                               |_         | j!                          | j#                          | j                   j%                  |      \  }}|r|j'                          yy)z
        Write all data that has been merged to the given output file.

        :param fileobj: Output file. Can be a filename or any kind of
            file-like object.
        N)rL   RuntimeErrorERR_CLOSED_WRITERrK   add_pager2   r   r	   r   r   _pages
get_objectget_referencePAKIDSr8   _write_dests_write_outlinerU   rV   )r9   rH   r   	pages_objmy_fileret_fileobjs         r:   rU   zPdfMerger.write=  s     ;;011
 JJ 	DKK  /T#s(^T[[-?-?-J-J-LMI $ 9 9"''"2&113!D	 	  ${{009 r<   c                 v    g | _         | j                  D ]  \  }}|j                           g | _        d| _        y)zHShut all file descriptors (input and output) and clear all memory usage.N)rK   rJ   rV   rL   )r9   fo_readers      r:   rV   zPdfMerger.close]  s9    
;; 	KBHHJ	 r<   infosc                 p    | j                   t        t              | j                   j                  |       y)z
        Add custom metadata to the output.

        :param dict infos: a Python dictionary where each key is a field
            and each value is your new metadata.
            Example: ``{u'/Title': u'My title'}``
        N)rL   r   r   add_metadatar9   r   s     r:   r   zPdfMerger.add_metadataf  s,     ;;011  'r<   c                 >    t        dd       | j                  |       y)zW
        .. deprecated:: 1.28.0

            Use :meth:`add_metadata` instead.
        addMetadatar   N)r   r   r   s     r:   r   zPdfMerger.addMetadatar  s     	#=.A% r<   layoutc                 >    t        dd       | j                  |       y)zZ
        .. deprecated:: 1.28.0

            Use :meth:`set_page_layout` instead.
        setPageLayoutset_page_layoutN)r   r   r9   r   s     r:   r   zPdfMerger.setPageLayout{  s     	#?4EFV$r<   c                 p    | j                   t        t              | j                   j                  |       y)a  
        Set the page layout.

        :param str 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)rL   r   r   _set_page_layoutr   s     r:   r   zPdfMerger.set_page_layout  s,    0 ;;011$$V,r<   modec                 >    t        dd       | j                  |       y)zX
        .. deprecated:: 1.28.0

            Use :meth:`set_page_mode` instead.
        setPageModeset_page_modeN)r   r   r9   r   s     r:   r   zPdfMerger.setPageMode  s     	#=/B4 r<   c                 p    | j                   t        t              | j                   j                  |       y)aP  
        Set the page mode.

        :param str 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)rL   r   r   r   r   s     r:   r   zPdfMerger.set_page_mode  s,    , ;;011!!$'r<   pdfr~   c                    g }t        |t              r|nt        t        |       }|j                         D ]  \  }}|D ]  }|j                  |   j                         |d   j                         k(  s5|d   j                         |t        d      <   t        |      t        |d         k(  sJ |j                  |          |S )zHRemove named destinations that are not a part of the specified page set./Page/Title)	rk   rm   rt   itemsrK   r   r#   r   rh   )	r9   r   r~   rK   	new_destslstkeyobjjs	            r:   rs   zPdfMerger._trim_dests  s     	!%.eD4G 	HC 99Q<**,G0G0G0II/27|/F/F/HC
7+,9S](;;;;$$S)	 r<   c                    g }d}t        |t              r|nt        t        |       }t        |      D ]  \  }}t        |t              rA| j	                  |||      }	|	s,|s|j                  ||dz
            |j                  |	       Wd}|D ]m  }
|d   	|j                  |
   j                         |d   j                         k(  s;|d   j                         |t        d      <   |j                  |       d}   |S )zJRemove outline item entries that are not a part of the specified page set.Tr   Fr   )	rk   rm   rt   	enumeraterp   rh   rK   r   r#   )r9   r   rE   rK   new_outlineprev_header_addedr   r   rX   subr   s              r:   rp   zPdfMerger._trim_outline  s     !%.eD4G(1 	OA|,-((lC@,#**71q5>:&&s+$)! 	A#G,4 yy|..0L4I4T4T4VV<H#=$*, %Z%89 $**<8,0)		& r<   c                 6   | j                   t        t              | j                  D ]o  }d }d|v rIt	        | j
                        D ]1  \  }}|j                  |d   k(  s|j                  |t        d      <    n |U| j                   j                  |       q y )Nr   )
rL   r   r   rM   r   rK   r4   r8   r#   add_named_destination_object)r9   
named_destpagenor   s       r:   r   zPdfMerger._write_dests  s    ;;011** 		EJF*$$-djj$9 LFDww*W"55:>:K:K
:g#67
 !88D		Er<   parentc                    | j                   t        t              || j                  }|J d       d }|D ]  }t	        |t
              r| j                  ||       &d }d|v rCt        | j                        D ]+  \  }}|j                  |d   k(  s| j                  ||        n |r|d= |d= | j                   j                  ||      } y )Nhint for mypyr   /Type)rL   r   r   rE   rk   rm   r   r   rK   r4   _write_outline_item_on_pageadd_outline_item_dict)r9   rE   r   
last_addedrX   page_nor   s          r:   r   zPdfMerger._write_outline  s     ;;011?llG"3O3"
# 	UL,-##L*=G,&%.tzz%: MGTww,w"7788tL " )<+@![[>>|VT
	Ur<   )rZ   r   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}|j#                  |t%                     D ]T  }||v r1t'        ||   t(              s|j+                  t-        ||                n|j+                  t-        d             ||= V t/        t	        t0        j2                        t	        d      t	        t0        j4                        t7        |      i      |t	        d      <   y )Nr   z/Zoomr   z/GoToz/A)r   r   r%   r4   r#   r   FIT_Hr   TOPFIT_BHFIT_VLEFTFIT_BVXYZFIT_RBOTTOMRIGHTgetrn   rk   r$   rh   r!   r    r   SDr   )r9   rX   r   oi_typeargsfit2arg_keysarg_keys          r:   r   z%PdfMerger._write_outline_item_on_page  s    sL12TWW%z'':;!!L$4$4#6""\%5%5$7!!L$5$5#7""\%6%6$8,"3"3\5E5Ew!O!!!!##""  	$4
 $''9 	&G,&zW%z0 KW(=>?KN+W%	& *:.001:g3F.001;t3D*
Z%&r<   c                 *   | j                   D ]  }d }|d   }t        |t              r|D ]:  }|j                         |j                  j                         k(  s/|j
                  }< |t        d|d    d      t        |      |t        d      <    y )Nr   zUnresolved named destination 'r   ')rM   rk   r%   r   r2   r4   rf   r#   )r9   rK   r   r   npr   s         r:   ru   z#PdfMerger._associate_dests_to_pagesA  s    ** 	CJFG$B"l+ %==?dmm&>&>&@@!WWF% ~ 4Z5I4J!L  /;6.BJz'*+	Cr<   c                 h   || j                   }|J d       |D ]  }t        |t              r| j                  ||       &d }|d   }t        |t              r>|D ]:  }|j                         |j                  j                         k(  s/|j                  }< |t	        |      |t        d      <    y )Nr   r   )	rE   rk   rm   rv   r%   r   r2   r4   r#   )r9   rK   rE   rX   r   outline_item_pageps          r:   rv   z+PdfMerger._associate_outline_items_to_pagesS  s     ?llG"3O3"# 	IL,-66ulKF ,W 5+\: "$//1QZZ5J5J5LLTTF" !4@4HZ01!	Ir<   rootc                     || j                   }t        |      D ]_  \  }}t        |t              r| j	                  ||      }|s+|g|z   c S ||k(  s$t        t        t        t        f   |d         |k(  s\|gc S  y )Nr   )rE   r   rk   rm   find_outline_itemr   r	   r   )r9   rX   r   r   oi_enumress         r:   r   zPdfMerger.find_outline_iteml  s     <<<D#D/ 	JAw'4( ,,\7C39$<'S#X(9:lJ s
	 r<   c                 &    | j                  ||      S )zZ
        .. deprecated:: 2.9.0
            Use :meth:`find_outline_item` instead.
        )r   )r9   rX   r   s      r:   find_bookmarkzPdfMerger.find_bookmark  s     %%lD99r<   )pagenumtitlecolorbolditalicfitr   r   c          	          ||t        d      |"d}
d}t        j                  |
 d| d       |}|t        d      | j                  }|t	        t
               |j                  |||||||g|	 S )a  
        Add an outline item (commonly referred to as a "Bookmark") to this PDF file.

        :param str title: Title to use for this outline item.
        :param int page_number: Page number this outline item will point to.
        :param parent: A reference to a parent outline item to create nested
            outline items.
        :param tuple color: Color of the outline item's font as a red, green, blue tuple
            from 0.0 to 1.0
        :param bool bold: Outline item font is bold
        :param bool italic: Outline item font is italic
        :param str fit: The fit of the destination page. See
            :meth:`add_link()<add_link>` for details.
        zMThe argument pagenum of add_outline_item is deprecated. Use page_number only.r   r\   r_   r`   ra   rc   )rf   rd   re   rL   r   r   add_outline_item)r9   r   r\   r   r   r   r   r   r   r   rw   rx   writers                r:   r   zPdfMerger.add_outline_item  s    4 "w':_   H$HMMj CH:XV
 "K:;;>011&v&&;tVS
CG
 	
r<   c           	      L    t        dd        | j                  |||||||g| S )zZ
        .. deprecated:: 1.28.0
            Use :meth:`add_outline_item` instead.
        addBookmarkr   r   r   	r9   r   r   r   r   r   r   r   r   s	            r:   r   zPdfMerger.addBookmark  ;     	#=2DE$t$$7FE4
?C
 	
r<   c           	      L    t        dd        | j                  |||||||g| S )zY
        .. deprecated:: 2.9.0
            Use :meth:`add_outline_item` instead.
        r   r   r   r   s	            r:   add_bookmarkzPdfMerger.add_bookmark  r   r<   c                 >    t        dd       | j                  ||      S )z_
        .. deprecated:: 1.28.0
            Use :meth:`add_named_destination` instead.
        addNamedDestinationadd_named_destination)r   r   )r9   r   r   s      r:   r   zPdfMerger.addNamedDestination  s#    
 	##8:QR))%99r<   c                 :   ||t        d      |"d}d}t        j                  | d| d       |}|t        d      t        t	        |      t        |      t        t        j                        t        d	            }| j                  j                  |       y)
z
        Add a destination to the output.

        :param str title: Title to use
        :param int page_number: Page number this destination points at.
        NzRThe argument pagenum of add_named_destination is deprecated. Use page_number only.r   r\   r_   r`   ra   rc   i:  )rf   rd   re   r   r'   r%   r#   r   r   rM   rh   )r9   r   r\   r   rw   rx   dests          r:   r   zPdfMerger.add_named_destination  s     "w':d   H$HMMj CH:XV
 "K:;;U#%,,-	
 	%r<   )F )r5   rD   )NNNNTN)NNT)r5   N)NNr7   )NNNFF/Fit)NNFFr  )@r=   r>   r?   r@   r   boolr   r   r   r;   rP   r   r   BaseExceptionr   rW   rA   r   r   r*   r   r   r   r   rg   r)   r   rh   rU   rV   r	   r   r   r   r,   r   r   r.   r   r   rs   r-   rp   r   r
   r&   r(   r   r   r1   r   ru   rv   r   r   r"   floatr+   r/   r   r   r   r   r   rB   r<   r:   rD   rD   V   s     ),HJ

-243D-E
	
 -
	4./	 m$	 M*		
 
	 BRS &*7;&*)-#"&q7c]q7 t[)34q7 sm	q7
 &q7 q7 3-q7 
q7 Tq7f!&T;	9:!&	vx
++	,!&F BRS '+ # R{It34 R sm R )U38_eCcM.BDIM
	 R  R 
 R T RD U4#45  $  @
($sCx. 
(T 
(!c3h !D !%J %4 %-j -T -8! ! !(, (4 (4 Cc3h'( U38_eCcM&:DIEF	
 
d38n	$  U38_eCcM&:DIEF	
 
>E ), 48'+U(;/0U $U 
	U -U6 0 
!+{":; 
CN 
	 
 1 
DCtK/@ CT C$ ),SWI+&I19(;:O1PI	I -I0 0 '+38n {# 
$s)		 10 0 '+	:38n	: {#	: 
$s)			: 1	: &*:>6:.
 "&.
.
 c].
 dJ67	.

 eUE123.
 .
 .
 .
 .
 #.
 
.
h ;?6:

 
 dJ67	

 eUE123
 
 
 
 
 

0 ;?6:

 
 dJ67	

 eUE123
 
 
 
 
 

(: :s :t : &*!%	!&!& c]!& #	!&
 
!&r<   rD   c                   ,     e Zd Zdededdf fdZ xZS )PdfFileMergerr   kwargsr5   Nc                 j    t        dd       d|vrt        |      dk  rd|d<   t        |   |i | y )Nr	  rD   rG   r   T)r   rj   superr;   )r9   r   r
  	__class__s      r:   r;   zPdfFileMerger.__init__  s;    "?K@6!c$i!m#F8$)&)r<   )r=   r>   r?   r   r;   __classcell__)r  s   @r:   r	  r	    s"    *c *S *T * *r<   r	  )>rd   ior   r   r   pathlibr   typesr   typingr   r	   r
   r   r   r   r   r   r   ri   r   _pager   r   r   _utilsr   r   r   r   _writerr   	constantsr   r   r   r   r   genericr   r   r    r!   r"   r#   r$   r%   r&   r'   r(   	pageranger)   r*   r+   r,   r-   r.   r/   r   r1   rD   r	  rB   r<   r:   <module>r     s   8  & &  
 
 
 $     * , 4    0 N NS  |
& |
&~*I *r<   