Package com.pd4ml
Class PdfDocument
- java.lang.Object
-
- com.pd4ml.pdf.PdfDocumentBase
-
- com.pd4ml.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 toPD4ML.merge(PdfDocument, int, int, boolean)to combine it with a conversion result.
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.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); }
-
-
Constructor Summary
Constructors Constructor Description PdfDocument(byte[] bytes, java.lang.String password)Creates a document from PDF bytes.PdfDocument(com.pd4ml.pdf.parser.PdfDoc writer)Creates a document from a document built with PD4ML's internal PDF writer.PdfDocument(java.io.InputStream stream, java.lang.String password)Creates a document from a stream.PdfDocument(java.net.URL url, java.lang.String password)Creates a document from a URL (file:,http:,https:orjava:for classpath resources).
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidmergePDFs(java.io.InputStream pdf1, java.io.InputStream pdf2, java.io.OutputStream result)Concatenates two PDF documents: writes all pages ofpdf1followed by all pages ofpdf2toresult.-
Methods inherited from class com.pd4ml.pdf.PdfDocumentBase
append, computeScope, deletePage, dumpOutlines, getAuthor, getCreationDate, getCreator, getKeywords, getMetadata, getModDate, getNumberOfPages, getPageBox, getPageContent, getPageSize, getPdfFormatVersion, getPermissions, getProducer, getSubject, getTitle, overlay, overlay, read, setAuthor, setCreator, setKeywords, setModDate, setSubject, setTitle, underlay, underlay, write, write
-
-
-
-
Constructor Detail
-
PdfDocument
public PdfDocument(byte[] bytes, java.lang.String password) throws InvokeExceptionCreates a document from PDF bytes.- Parameters:
bytes- the PDF document; the array is used directly, not copiedpassword- the password of an encrypted document, ornullif it is not encrypted- Throws:
InvokeException- ifbytesisnullor too short to be a PDF document
-
PdfDocument
public PdfDocument(java.io.InputStream stream, java.lang.String password) throws InvokeExceptionCreates a document from a stream. The stream is read completely and closed when the document is first needed.- Parameters:
stream- the PDF document streampassword- the password of an encrypted document, ornullif it is not encrypted- Throws:
InvokeException- ifstreamisnull
-
PdfDocument
public PdfDocument(java.net.URL url, java.lang.String password) throws InvokeExceptionCreates a document from a URL (file:,http:,https:orjava:for classpath resources). The document is loaded when it is first needed.- Parameters:
url- the PDF document locationpassword- the password of an encrypted document, ornullif it is not encrypted- Throws:
InvokeException- ifurlisnull
-
PdfDocument
public PdfDocument(com.pd4ml.pdf.parser.PdfDoc writer) throws InvokeExceptionCreates 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.IOExceptionConcatenates two PDF documents: writes all pages ofpdf1followed by all pages ofpdf2toresult. Documents that need a password to open are not supported. For more control (metadata, encryption) usePdfDocumentBase.append(PdfDocumentBase).- Parameters:
pdf1- the first documentpdf2- the document to appendresult- 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
-
-