Package com.pd4ml.pdf

Class PdfDocumentBase

  • Direct Known Subclasses:
    PdfDocument

    public abstract class PdfDocumentBase
    extends java.lang.Object
    • Field Detail

      • pdf

        public com.pd4ml.pdf.parser.PdfDoc pdf
    • Constructor Detail

      • PdfDocumentBase

        public PdfDocumentBase​(byte[] bytes,
                               java.lang.String password)
                        throws InvokeException
        Parameters:
        bytes - PDF document as an array of bytes
        password - if any. "null" for not protected PDF documents
        Throws:
        InvokeException - by invalid call parameters
      • PdfDocumentBase

        public PdfDocumentBase​(java.io.InputStream stream,
                               java.lang.String password)
                        throws InvokeException
        Parameters:
        stream - PDF document input stream
        password - if any. "null" for not protected PDF documents
        Throws:
        InvokeException - by invalid call parameters
      • PdfDocumentBase

        public PdfDocumentBase​(java.net.URL url,
                               java.lang.String password)
                        throws InvokeException
        Parameters:
        url - PDF document URL
        password - if any. "null" for not protected PDF documents
        Throws:
        InvokeException - by invalid call parameters
    • Method Detail

      • write

        public void write​(java.io.OutputStream out)
                   throws java.io.IOException,
                          InvokeException
        writes current PDF document state to given output stream.
        Parameters:
        out - an output stream to write to
        Throws:
        java.io.IOException - write errors
        InvokeException - if the document is already appended to another one (document objects reassigned to another root).
      • write

        public void write​(java.io.OutputStream out,
                          java.lang.String password,
                          int permissions)
                   throws java.io.IOException,
                          InvokeException,
                          InvalidPasswordException
        writes current PDF document state to given output stream.
        Parameters:
        out - an output stream to write to
        password - password string. null if no password to be applied
        permissions - PDF file access permissions See PD4Constants.Allow* constants. Example AllowAnnotate | AllowDegradedPrint. Use PD4Constants#DefaultPermissions for defaults.
        Throws:
        java.io.IOException - write errors
        InvalidPasswordException - invalid password given to the object constructor, i.e. PdfDocumentBase(byte[], String).
        InvokeException - if the document is already appended to another one (document objects reassigned to another root).
        See Also:
        Constants.DefaultPermissions
      • append

        public void append​(PdfDocumentBase toMerge)
                    throws java.io.IOException
        Parameters:
        toMerge - another PDF document to append to the current one. Implicitly invokes read() if has not been triggered before.
        Throws:
        java.io.IOException - reflects a variety of PDF read/parse errors by read()
        InvalidPasswordException - invalid password given to the object constructor, i.e. PdfDocumentBase(byte[], String).
        InvokeException - by an attempt to merge with more than one document
      • overlay

        public void overlay​(PdfDocumentBase anotherDoc,
                            int opacity)
                     throws java.io.IOException
        Parameters:
        anotherDoc - another PDF document whose page content is to be combined with the pages of the current document. Implicitly invokes read() if has not been triggered before. The method applies all pages of anotherDoc to all pages of this doc.
        opacity - transparency of the overlay: 0 = fully transparent, 255 (or any negative value) = opaque
        Throws:
        java.io.IOException - reflects a variety of PDF read/parse errors by read()
        InvalidPasswordException - invalid password given to the object constructor, i.e. PdfDocumentBase(byte[], String).
        InvokeException - by an attempt to merge with more than one document
        See Also:
        computeScope(String, int)
      • overlay

        public void overlay​(PdfDocumentBase anotherDoc,
                            java.lang.String srcScope,
                            java.lang.String destScope,
                            int opacity)
                     throws java.io.IOException
        Parameters:
        anotherDoc - another PDF document whose page content is to be combined with the pages of the current document. Implicitly invokes read() if has not been triggered before.
        srcScope - a page range of an overlay document to use.
        destScope - a page range in the current document the overlay is to apply to.
        opacity - transparency of the overlay: 0 = fully transparent, 255 (or any negative value) = opaque
        Throws:
        java.io.IOException - reflects a variety of PDF read/parse errors by read()
        InvalidPasswordException - invalid password given to the object constructor, i.e. PdfDocumentBase(byte[], String).
        InvokeException - by an attempt to merge with more than one document
        See Also:
        computeScope(String, int)
      • underlay

        public void underlay​(PdfDocumentBase anotherDoc,
                             int opacity)
                      throws java.io.IOException
        Parameters:
        anotherDoc - another PDF document whose page content is to be placed under of the current document content. Implicitly invokes read() if has not been triggered before. The method applies all pages of anotherDoc to all pages of this doc.
        opacity - transparency of the overlay: 0 = fully transparent, 255 (or any negative value) = opaque
        Throws:
        java.io.IOException - reflects a variety of PDF read/parse errors by read()
        InvalidPasswordException - invalid password given to the object constructor, i.e. PdfDocumentBase(byte[], String).
        InvokeException - by an attempt to merge with more than one document
        See Also:
        computeScope(String, int)
      • underlay

        public void underlay​(PdfDocumentBase anotherDoc,
                             java.lang.String srcScope,
                             java.lang.String destScope,
                             int opacity)
                      throws java.io.IOException
        Parameters:
        anotherDoc - another PDF document whose page content is to be placed under of the current document content. Implicitly invokes read() if has not been triggered before.
        srcScope - a page range of an underlay document to use.
        destScope - a page range in the current document the underlay is to apply to.
        opacity - transparency of the overlay: 0 = fully transparent, 255 (or any negative value) = opaque
        Throws:
        java.io.IOException - reflects a variety of PDF read/parse errors by read()
        InvalidPasswordException - invalid password given to the object constructor, i.e. PdfDocumentBase(byte[], String).
        InvokeException - by an attempt to merge with more than one document
        See Also:
        computeScope(String, int)
      • setAuthor

        public void setAuthor​(java.lang.String author)
                       throws java.io.IOException
        Parameters:
        author - a value to override document "Author" name. Implicitly invokes read() if has not been triggered before.
        Throws:
        java.io.IOException - reflects a variety of PDF read/parse errors by read()
        InvalidPasswordException - invalid password given to the object constructor, i.e. PdfDocumentBase(byte[], String).
      • getPageSize

        public PageSize getPageSize​(int pageIndex)
                             throws InvalidPasswordException,
                                    java.io.IOException
        returns page size
        Parameters:
        pageIndex - page number (starting from 1)
        Returns:
        page size object
        Throws:
        InvalidPasswordException - thrown for encrypted documents in a case given password does not match
        java.io.IOException - general document read exception
      • getPageBox

        public float[] getPageBox​(int pageIndex,
                                  java.lang.String boxName)
                           throws InvalidPasswordException,
                                  java.io.IOException
        returns PDF page boundaries array.
        Parameters:
        boxName - "ArtBox", "BleedBox", "TrimBox" – used for printing; "MediaBox" – target paper size, i.e., finished size of printed document; "CropBox" – page visible in the viewer, i.e., what the user sees on the screen; "BBox" – the bounds of the actual content on the page
        pageIndex - page number (starting from 1)
        Returns:
        array of four numbers
        Throws:
        InvalidPasswordException - thrown for encrypted documents in a case given password does not match
        java.io.IOException - general document read exception
      • getPageContent

        public java.lang.String getPageContent​(int pageIndex)
                                        throws InvalidPasswordException,
                                               java.io.IOException
        Parameters:
        pageIndex - page number (0 is the first page)
        Returns:
        page content stream
        Throws:
        java.io.IOException - read/parsing errors
        InvalidPasswordException - PDF encryption password do not match
      • deletePage

        public int deletePage​(int pageNumber)
                       throws InvalidPasswordException,
                              java.io.IOException
        deletes a page of a loaded document. If the document has only one page or not existent page number is given, the method call takes no effect.
        Parameters:
        pageNumber - index of page to delete (first page is 1)
        Returns:
        number of pages remain in the doc.
        Throws:
        java.io.IOException - read/parsing errors
        InvalidPasswordException - PDF encryption password do not match
      • computeScope

        public static boolean[] computeScope​(java.lang.String scope,
                                             int pageNum)
        Utility method converts page scope description to a boolean array of flags. "true" = page number matches the scope, "false" = does not. Note: in scope definition the first page is "1"; it corresponds to '0" index in the returned array.
        Parameters:
        scope - "null" = all pages. Allowed syntax: "1", "3-5", "2+", "last", "even", "odd", "1+,skiplast", "5+,even"
        pageNum - number of pages
        Returns:
        an array of scope match flags. Each array item corresponds a page.