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);
}
}
}
}
Aggregations