o
    @@iE                     @   s   d Z ddlZddlZddlmZ ddlmZ ddlmZ ddlmZ ddlm	Z	 ddlm
Z
 ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ G dd deZdS )z&
This class represents a PDF document
    N)Decimal)
Dictionary)List)Name)Stream)String)ConformanceLevel)DestinationType)NameTree)PageDocumentInfoXMPDocumentInfo)PlainTextXREFc                       sj  e Zd ZdZd1deje f fddZd2dd	Zd
e	de
dd fddZde	dd fddZ					d3de	dedededeje deje deje deje deje dd fddZdedd fddZdefdd Zd
e	deje
 fd!d"Zdeje	e
f fd#d$Zd%edefd&d'Zdefd(d)Zdefd*d+Zd1ded,eje dd fd-d.Zd,edd fd/d0Z  ZS )4Documentz.
    This class represents a PDF document
    Nconformance_levelc                    s   t t|   || _d S N)superr   __init___conformance_level_upon_create)selfr   	__class__ X/home/ubuntu/transcripts/venv/lib/python3.10/site-packages/borb/pdf/document/document.pyr   #   s   zDocument.__init__documentreturnc                 C   s8   t |  pd}td|D ]
}| || q| S )zB
        This method appends another Document to this one
        r   )intget_document_infoget_number_of_pagesrangeadd_pageget_page)r   r   number_of_pages_in_otherir   r   r   add_document1   s   zDocument.add_document	file_name
file_bytesc                 C   s   t  }td|td< ||td< tt|td |td< t }t |td< ||td td< t||td< td|td< t| tdd	|| | S )
a1  
        If a PDF file contains file specifications that refer to an external file and the PDF file is archived or transmitted,
        some provision should be made to ensure that the external references will remain valid. One way to do this is to
        arrange for copies of the external files to accompany the PDF file. Embedded file streams (PDF 1.3) address
        this problem by allowing the contents of referenced files to be embedded directly within the body of the PDF
        file. This makes the PDF file a self-contained unit that can be stored or transmitted as a single entity. (The
        embedded files are included purely for convenience and need not be directly processed by any conforming reader.)
        This method embeds a file (specified by its name and bytes) into this Document
        EmbeddedFileTypeBytesLengthEFFFilespecEmbeddedFilesname)r   r   bDecimallenr   r   r
   put)r   r'   r(   streamleafr   r   r   add_embedded_file<   s   zDocument.add_embedded_file
javascriptc                 C   s   t  }td|td< t|d|td< t|td d|td< tt|td |td< td|td	< t }td|td
< ||td< t| tdd}|	d
t|| | S )z
        This function appends embedded JavaScript to this Document, returning self.
        :param javascript:  the Javascript str to be appended to this Document
        :return:            self
        
JavaScriptr*   latin1DecodedBytes	   r+   r,   FlateDecodeFilterSJSr1   zscript-{0:03d}.js)r   r   byteszlibcompressr3   r4   r   r
   r5   format)r   r9   r6   r7   ntr   r   r   add_embedded_javascriptZ   s   z Document.add_embedded_javascripttextleveldestination_typepage_nrtoprightbottomleftzoomc
                    s  t  }
|
d |
t| |
|j |tjkrE|dur.|du r.|du r.|dur.|	dus0J |
t| |
t| |
t|	 |tjkr`|du r^|du r^|du r^|du r^|	du s`J |tjkr|du ry|du ry|du ry|dury|	du s{J |
t| |tj	kr|dur|du r|du r|du r|	du sJ |
t| |tj
kr|dur|dur|dur|dur|	du sJ |
t| |
t| |
t| |
t| |tjkr|du r|du r|du r|dur|	du sJ |
t| |tjkr%|dur|du r|du r|du r|	du sJ |
t| d| d d d vr^t }|| d d d td< || d d d td  td|td< td|td	< t }|
|td
< d|td< t||td< | d d d d }d|vsd|vr||td< ||td< td|td	< ||td< | S dtfdd}g }d|fg}t|dkr|d }|| |d ||d D ]}||d d |f qt|dkst fdd|D fdd|D d }d|v r	|d }||td< ||td< d|vr||td< d	|vr'td|td	< ||td< |}|rPt|d	 td |td	< d|v rJ|d }n	 | S |s2| S )u  
        A PDF document may contain a document outline that the conforming reader may display on the screen,
        allowing the user to navigate interactively from one part of the document to another. The outline consists of a
        tree-structured hierarchy of outline items (sometimes called bookmarks), which serve as a visual table of
        contents to display the document’s structure to the user.
        This function adds an outline to this Document
        TNOutlinesXRefTrailerRootr*   r   CountDestParentTitleFirstLast   xc                 S   sL   d| vrg S | d g}|d | d kr$| |d d  |d | d ks|S )NrY   rZ   Next)append)r\   childrenr   r   r   	_children   s   
z'Document.add_outline.<locals>._childrenr]   c                    s    g | ]}|d   k r|d  qS )r   r   .0r\   )rI   r   r   
<listcomp>  s     z(Document.add_outline.<locals>.<listcomp>c                    s    g | ]}|d   kr|d qS )r   r[   r   rb   )parent_levelr   r   rd     s    r^   )r   set_is_inliner_   r3   valuer	   X_Y_ZFITFIT_HFIT_VFIT_RFIT_B_HFIT_B_Vr   r   
set_parentr   r4   popmaxr   )r   rH   rI   rJ   rK   rL   rM   rN   rO   rP   destinationoutline_dictionaryoutlinera   outlines_doneoutlines_todotcparent_outline_dictionarysiblingoutline_dictionary_to_updater   )rI   re   r   add_outlineu   s   




















	zDocument.add_outlinepagec                 C   s
   |  |S )zU
        This method appends a page (from another Document) to this Document
        )insert_page)r   r}   r   r   r   r"   !  s   
zDocument.add_pagec                 C      t | S )zI
        This function returns the DocumentInfo of this Document
        r   r   r   r   r   r   '     zDocument.get_document_infoc                 C   s<   t | td D ]\}}||kr|d d d   S q	dS )a)  
        If a PDF file contains file specifications that refer to an external file and the PDF file is archived or transmitted,
        some provision should be made to ensure that the external references will remain valid. One way to do this is to
        arrange for copies of the external files to accompany the PDF file. Embedded file streams (PDF 1.3) address
        this problem by allowing the contents of referenced files to be embedded directly within the body of the PDF
        file. This makes the PDF file a self-contained unit that can be stored or transmitted as a single entity. (The
        embedded files are included purely for convenience and need not be directly processed by any conforming reader.)
        This method returns the embedded file specified by the given file_name
        r0   r-   r.   r<   Nr
   r   items)r   r'   kvr   r   r   get_embedded_file-  s
   zDocument.get_embedded_filec                 C   s   dd t | td D S )a.  
        If a PDF file contains file specifications that refer to an external file and the PDF file is archived or transmitted,
        some provision should be made to ensure that the external references will remain valid. One way to do this is to
        arrange for copies of the external files to accompany the PDF file. Embedded file streams (PDF 1.3) address
        this problem by allowing the contents of referenced files to be embedded directly within the body of the PDF
        file. This makes the PDF file a self-contained unit that can be stored or transmitted as a single entity. (The
        embedded files are included purely for convenience and need not be directly processed by any conforming reader.)
        This method returns all embedded files, as a dictionary of names unto bytes
        c                 S   s&   i | ]\}}t ||d  d d qS )r-   r.   r<   )str)rc   r   r   r   r   r   
<dictcomp>I  s    z/Document.get_embedded_files.<locals>.<dictcomp>r0   r   r   r   r   r   get_embedded_files?  s   
zDocument.get_embedded_filespage_numberc                 C   s   | d d d d d | S )zZ
        This function returns a Page (at given page_number) within this Document
        rR   rS   rT   PagesKidsr   )r   r   r   r   r   r#   N  s   zDocument.get_pagec                 C   r   )zL
        This function returns the XMPDocumentInfo of this Document
        r   r   r   r   r   get_xmp_document_infoT  r   zDocument.get_xmp_document_infoc                 C   sX   z%d| d d d v o$d| d d d d v o$| d d d d d dkW S    Y dS )u  
        A PDF document may contain a document outline that the conforming reader may display on the screen,
        allowing the user to navigate interactively from one part of the document to another. The outline consists of a
        tree-structured hierarchy of outline items (sometimes called bookmarks), which serve as a visual table of
        contents to display the document’s structure to the user.
        This function returns True if this Document has outlines, false otherwise
        rQ   rR   rS   rT   rU   r   Fr   r   r   r   r   has_outlinesZ  s   zDocument.has_outlinesindexc                 C   s  d| vrt  | td< | td |  d| d vr8t | d td< td| d td< | d d | d  d| d d vrht | d d td< td| d d d td< | d d d | d d  d| d d d vrt | d d td td< td| d d td td td	< t | d d td td td
< td| d d td td td< | d d d d | d d d  | d d d d d
 | d d d d  | d d d d d
 }|dusJ t|tsJ |du rt|}||| | d d d d d	 }t|d | d d d d td	< | d d d d |td< || | S )zf
        This method appends a page (from another Document) to this Document at a given index
        rR   rS   r   SizerT   Catalogr*   r   rU   r   Nr[   rW   )	r   r   ro   r   r3   r   
isinstancer4   insert)r   r}   r   kids
prev_countr   r   r   r~   k  s>   (&(&.

zDocument.insert_pagec                 C   s   d| vr| S d| d vr| S d| d d vr| S d| d d d vr$| S d| d d d d vr2| S d| d d d d vr@| S | d d d d d }|dusRJ t |tsYJ |dk sc|t|kre| S || tt|| d d d d td< | S )	zx
        This method removes a Page from this Document at a given index.
        It then returns this Document.
        rR   rS   rT   r   r   rU   Nr   )r   r   r4   rp   r3   r   )r   r   r   r   r   r   pop_page  s(   
$zDocument.pop_pager   )r   r   r   r   )NNNNN) __name__
__module____qualname____doc__typingOptionalr   r   r&   r   rB   r8   rG   r   r	   r   r|   r   r"   r   r   r   Dictr   r#   r   r   boolr   r~   r   __classcell__r   r   r   r   r      sT    
!	

 -.r   )r   r   rC   decimalr   borb.io.read.typesr3   r   r   r   r   r   borb.io.write.conformance_levelr   1borb.pdf.canvas.layout.annotation.link_annotationr	   borb.pdf.document.name_treer
   borb.pdf.page.pager   borb.pdf.trailer.document_infor   r   borb.pdf.xref.plaintext_xrefr   r   r   r   r   r   <module>   s$   