Example usage for org.apache.pdfbox.cos COSStream COSStream

List of usage examples for org.apache.pdfbox.cos COSStream COSStream

Introduction

In this page you can find the example usage for org.apache.pdfbox.cos COSStream COSStream.

Prototype

public COSStream() 

Source Link

Document

Creates a new stream with an empty dictionary.

Usage

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;/*from w  ww  .j av  a2s.  c o  m*/
    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);
    }
}

From source file:org.apache.fop.render.pdf.pdfbox.PDFBoxAdapter.java

License:Apache License

private PDStream getContents(PDPage page) throws IOException {
    PDStream pdStream = new PDStream(new COSStream());
    OutputStream os = pdStream.createOutputStream();
    IOUtils.copy(page.getContents(), os);
    os.close();//from w  ww. ja  v  a 2s.co m
    return pdStream;
}

From source file:us.kagome.pdfbox.PDFMergerExample.java

License:Apache License

/**
 * Creates a compound PDF document from a list of input documents.
 * <p>//  w ww  . j av  a2s. c o m
 * The merged document is PDF/A-1b compliant, provided the source documents are as well. It
 * contains document properties title, creator and subject, currently hard-coded.
 *
 * @param sources list of source PDF document streams.
 * @return compound PDF document as a readable input stream.
 * @throws IOException if anything goes wrong during PDF merge.
 */
public InputStream merge(final List<InputStream> sources) throws IOException {
    String title = "My title";
    String creator = "Alexander Kriegisch";
    String subject = "Subject with umlauts ";

    ByteArrayOutputStream mergedPDFOutputStream = null;
    COSStream cosStream = null;
    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 (Exception e) {
        throw new IOException("PDF merge problem", e);
    } finally {
        for (InputStream source : sources) {
            IOUtils.closeQuietly(source);
        }
        IOUtils.closeQuietly(cosStream);
        IOUtils.closeQuietly(mergedPDFOutputStream);
    }
}