Search in sources :

Example 1 with MemoryUsageSetting

use of com.tom_roush.pdfbox.io.MemoryUsageSetting in project PdfBox-Android by TomRoush.

the class PDFMergerUtility method legacyMergeDocuments.

/**
 * Merge the list of source documents, saving the result in the destination
 * file.
 *
 * @param memUsageSetting defines how memory is used for buffering PDF streams;
 *                        in case of <code>null</code> unrestricted main memory is used
 *
 * @throws IOException If there is an error saving the document.
 */
private void legacyMergeDocuments(MemoryUsageSetting memUsageSetting) throws IOException {
    PDDocument destination = null;
    if (sources != null && sources.size() > 0) {
        // Make sure that:
        // - first Exception is kept
        // - destination is closed
        // - all PDDocuments are closed
        // - all FileInputStreams are closed
        // - there's a way to see which errors occurred
        List<PDDocument> tobeclosed = new ArrayList<PDDocument>();
        try {
            MemoryUsageSetting partitionedMemSetting = memUsageSetting != null ? memUsageSetting.getPartitionedCopy(sources.size() + 1) : MemoryUsageSetting.setupMainMemoryOnly();
            destination = new PDDocument(partitionedMemSetting);
            for (Object sourceObject : sources) {
                PDDocument sourceDoc = null;
                if (sourceObject instanceof File) {
                    sourceDoc = PDDocument.load((File) sourceObject, partitionedMemSetting);
                } else {
                    sourceDoc = PDDocument.load((InputStream) sourceObject, partitionedMemSetting);
                }
                tobeclosed.add(sourceDoc);
                appendDocument(destination, sourceDoc);
            }
            // optionally set meta data
            if (destinationDocumentInformation != null) {
                destination.setDocumentInformation(destinationDocumentInformation);
            }
            if (destinationMetadata != null) {
                destination.getDocumentCatalog().setMetadata(destinationMetadata);
            }
            if (destinationStream == null) {
                destination.save(destinationFileName);
            } else {
                destination.save(destinationStream);
            }
        } finally {
            if (destination != null) {
                IOUtils.closeAndLogException(destination, "PDDocument", null);
            }
            for (PDDocument doc : tobeclosed) {
                IOUtils.closeAndLogException(doc, "PDDocument", null);
            }
        }
    }
}
Also used : InputStream(java.io.InputStream) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) ArrayList(java.util.ArrayList) MemoryUsageSetting(com.tom_roush.pdfbox.io.MemoryUsageSetting) COSObject(com.tom_roush.pdfbox.cos.COSObject) File(java.io.File)

Aggregations

COSObject (com.tom_roush.pdfbox.cos.COSObject)1 MemoryUsageSetting (com.tom_roush.pdfbox.io.MemoryUsageSetting)1 PDDocument (com.tom_roush.pdfbox.pdmodel.PDDocument)1 File (java.io.File)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1