Fix chiusura file nel merge dei pdf

This commit is contained in:
Silvano Trinchero 2018-04-10 17:12:02 +02:00
parent 887e0846be
commit 9c5045333e
1 changed files with 34 additions and 17 deletions

View File

@ -24,6 +24,7 @@ import java.io.IOException;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -592,27 +593,43 @@ public final class AEnv
DocumentException, FileNotFoundException { DocumentException, FileNotFoundException {
Document document = null; Document document = null;
PdfWriter copy = null; PdfWriter copy = null;
for (File f : pdfList)
{ List<PdfReader> pdfReaders = new ArrayList<PdfReader>();
PdfReader reader = new PdfReader(f.getAbsolutePath());
if (document == null) try
{
for (File f : pdfList)
{ {
document = new Document(reader.getPageSizeWithRotation(1)); PdfReader reader = new PdfReader(f.getAbsolutePath());
copy = PdfWriter.getInstance(document, new FileOutputStream(outFile));
document.open(); pdfReaders.add(reader);
if (document == null)
{
document = new Document(reader.getPageSizeWithRotation(1));
copy = PdfWriter.getInstance(document, new FileOutputStream(outFile));
document.open();
}
int pages = reader.getNumberOfPages();
PdfContentByte cb = copy.getDirectContent();
for (int i = 1; i <= pages; i++) {
document.newPage();
copy.newPage();
PdfImportedPage page = copy.getImportedPage(reader, i);
cb.addTemplate(page, 0, 0);
copy.releaseTemplate(page);
}
} }
int pages = reader.getNumberOfPages(); document.close();
PdfContentByte cb = copy.getDirectContent(); }
for (int i = 1; i <= pages; i++) { finally
document.newPage(); {
copy.newPage(); for(PdfReader reader:pdfReaders)
PdfImportedPage page = copy.getImportedPage(reader, i); {
cb.addTemplate(page, 0, 0); reader.close();
copy.releaseTemplate(page);
} }
} }
document.close(); }
}
/** /**
* Get window title * Get window title