Package com.pd4ml

Class PdfDocument


  • public class PdfDocument
    extends PdfDocumentBase
    An existing PDF document, for post-processing: reading and changing document properties, appending another document, putting the pages of another document over or under its pages, deleting pages, and saving it, optionally encrypted. It can also be passed to PD4ML.merge(PdfDocument, int, int, boolean) to combine it with a conversion result.
    
     PdfDocument doc = new PdfDocument(new File("report.pdf").toURI().toURL(), null);
     PdfDocument letterhead = new PdfDocument(new File("letterhead.pdf").toURI().toURL(), null);
     doc.underlay(letterhead, "1", "1+", 255);   // page 1 of the letterhead under every page
     doc.setTitle("Annual report");
     try (OutputStream os = new FileOutputStream("result.pdf")) {
         doc.write(os);
     }
     
    The document is parsed when it is first needed, so the constructors do not fail on invalid content; the first method that needs the content throws instead.
    • Constructor Detail

      • PdfDocument

        public PdfDocument​(byte[] bytes,
                           java.lang.String password)
                    throws InvokeException
        Creates a document from PDF bytes.
        Parameters:
        bytes - the PDF document; the array is used directly, not copied
        password - the password of an encrypted document, or null if it is not encrypted
        Throws:
        InvokeException - if bytes is null or too short to be a PDF document
      • PdfDocument

        public PdfDocument​(java.io.InputStream stream,
                           java.lang.String password)
                    throws InvokeException
        Creates a document from a stream. The stream is read completely and closed when the document is first needed.
        Parameters:
        stream - the PDF document stream
        password - the password of an encrypted document, or null if it is not encrypted
        Throws:
        InvokeException - if stream is null
      • PdfDocument

        public PdfDocument​(java.net.URL url,
                           java.lang.String password)
                    throws InvokeException
        Creates a document from a URL (file:, http:, https: or java: for classpath resources). The document is loaded when it is first needed.
        Parameters:
        url - the PDF document location
        password - the password of an encrypted document, or null if it is not encrypted
        Throws:
        InvokeException - if url is null
      • PdfDocument

        public PdfDocument​(com.pd4ml.pdf.parser.PdfDoc writer)
                    throws InvokeException
        Creates a document from a document built with PD4ML's internal PDF writer. For internal use.
        Parameters:
        writer - the internal PDF document
        Throws:
        InvokeException - if the document cannot be serialized
    • Method Detail

      • mergePDFs

        public static void mergePDFs​(java.io.InputStream pdf1,
                                     java.io.InputStream pdf2,
                                     java.io.OutputStream result)
                              throws java.io.IOException
        Concatenates two PDF documents: writes all pages of pdf1 followed by all pages of pdf2 to result. Documents that need a password to open are not supported. For more control (metadata, encryption) use PdfDocumentBase.append(PdfDocumentBase).
        Parameters:
        pdf1 - the first document
        pdf2 - the document to append
        result - the stream to write the combined document to; it is closed afterwards
        Throws:
        java.io.IOException - if a document cannot be read or parsed, or the result cannot be written