List of usage examples for org.apache.pdfbox.multipdf PDFMergerUtility setDestinationDocumentInformation
public void setDestinationDocumentInformation(PDDocumentInformation info)
From source file:io.github.qwefgh90.akka.pdf.PDFUtilWrapper.java
License:Apache License
public static InputStream merge(final List<InputStream> sources, String _title, String _creator, String _subject) throws IOException { String title = _title == null ? "" : _title; String creator = _creator == null ? "" : _creator; String subject = _subject == null ? "" : _subject; ByteArrayOutputStream mergedPDFOutputStream = null; COSStream cosStream = null;/*w w w.ja v a 2 s. c om*/ try { // If you're merging in a servlet, you can modify this example to use the outputStream only // as the response as shown here: http://stackoverflow.com/a/36894346/535646 mergedPDFOutputStream = new ByteArrayOutputStream(); cosStream = new COSStream(); PDFMergerUtility pdfMerger = createPDFMergerUtility(sources, mergedPDFOutputStream); // PDF and XMP properties must be identical, otherwise document is not PDF/A compliant PDDocumentInformation pdfDocumentInfo = createPDFDocumentInfo(title, creator, subject); PDMetadata xmpMetadata = createXMPMetadata(cosStream, title, creator, subject); pdfMerger.setDestinationDocumentInformation(pdfDocumentInfo); pdfMerger.setDestinationMetadata(xmpMetadata); LOG.info("Merging " + sources.size() + " source documents into one PDF"); pdfMerger.mergeDocuments(MemoryUsageSetting.setupMainMemoryOnly()); LOG.info("PDF merge successful, size = {" + mergedPDFOutputStream.size() + "} bytes"); return new ByteArrayInputStream(mergedPDFOutputStream.toByteArray()); } catch (BadFieldValueException e) { throw new IOException("PDF merge problem", e); } catch (TransformerException e) { throw new IOException("PDF merge problem", e); } finally { for (InputStream source : sources) { IOUtils.closeQuietly(source); } IOUtils.closeQuietly(cosStream); IOUtils.closeQuietly(mergedPDFOutputStream); } }