From db52e4d66d60184d53a27ba4d6772461daacc03d Mon Sep 17 00:00:00 2001 From: tknall Date: Fri, 22 Mar 2013 08:57:51 +0000 Subject: Maintenance update (bugfixes, new features, cleanup...) Refer to /dok/RELEASE_NOTES-3.3.txt for further information. git-svn-id: https://joinup.ec.europa.eu/svn/pdf-as/pdf-as/trunk@931 7b5415b0-85f9-ee4d-85bd-d5d0c3b42d1c --- .../knowcenter/wag/egov/egiz/pdf/PDFUtilities.java | 25 +++++++++++++--------- 1 file changed, 15 insertions(+), 10 deletions(-) (limited to 'pdf-as-lib/src/main/java/at/knowcenter/wag/egov/egiz/pdf/PDFUtilities.java') diff --git a/pdf-as-lib/src/main/java/at/knowcenter/wag/egov/egiz/pdf/PDFUtilities.java b/pdf-as-lib/src/main/java/at/knowcenter/wag/egov/egiz/pdf/PDFUtilities.java index bdc1078..b3adb71 100644 --- a/pdf-as-lib/src/main/java/at/knowcenter/wag/egov/egiz/pdf/PDFUtilities.java +++ b/pdf-as-lib/src/main/java/at/knowcenter/wag/egov/egiz/pdf/PDFUtilities.java @@ -32,6 +32,7 @@ import java.util.List; import at.gv.egiz.pdfas.framework.input.PdfDataSource; import at.gv.egiz.pdfas.impl.input.ByteArrayPdfDataSourceImpl; +import at.gv.egiz.pdfas.utils.PDFASUtils; import org.pdfbox.pdfparser.PDFParser; import org.pdfbox.pdmodel.PDDocument; @@ -44,7 +45,7 @@ import com.lowagie.text.DocumentException; /** * Contains useful helpers for accessing PDF documents. - * + * * @author wprinz * @author mruhmer */ @@ -52,12 +53,13 @@ public abstract class PDFUtilities { public static float calculatePageLength(final PdfDataSource pdfDataSource, int page, float effectivePageHeight, int pagerotation) throws PDFDocumentException { + PDDocument pdfDocument_ = null; try { //ByteArrayInputStream original_bais = new ByteArrayInputStream(pdf); //byte [] normalized_pdf = TextualSignature.normalizePDF(original_bais); byte [] normalized_pdf = TextualSignature.normalizePDF(pdfDataSource); - + // PERF: The whole PDF normalization process is costy ByteArrayInputStream bais = new ByteArrayInputStream(normalized_pdf); @@ -66,9 +68,8 @@ public abstract class PDFUtilities parser.setTempDirectory(temporary_dir); parser.parse(); - PDDocument pdfDocument_ = parser.getPDDocument(); + pdfDocument_ = parser.getPDDocument(); float page_length = calculatePageLength(pdfDocument_, page, effectivePageHeight, pagerotation); - pdfDocument_.close(); return page_length; } catch (IOException e) @@ -78,8 +79,10 @@ public abstract class PDFUtilities catch (DocumentException e) { throw new PDFDocumentException(201, e); + } finally { + PDFASUtils.closeQuietly(pdfDocument_); } - } + } public static float calculatePageLength(PDDocument document, int page, float effectivePageHeight, int pagerotation) throws IOException { //int last_page_id = document.getNumberOfPages(); @@ -87,8 +90,8 @@ public abstract class PDFUtilities PDPage pdpage = (PDPage) allPages.get(page); pdpage.setRotation(pagerotation); return calculatePageLength(pdpage, effectivePageHeight); - } - + } + /** * @deprecated * @param pdf @@ -98,12 +101,13 @@ public abstract class PDFUtilities */ public static float calculateLastPageLength(final byte[] pdf, float effectivePageHeight) throws PDFDocumentException { + PDDocument pdfDocument_ = null; try { //ByteArrayInputStream original_bais = new ByteArrayInputStream(pdf); PdfDataSource dataSource = new ByteArrayPdfDataSourceImpl(pdf); byte [] normalized_pdf = TextualSignature.normalizePDF(dataSource); - + ByteArrayInputStream bais = new ByteArrayInputStream(normalized_pdf); PDFParser parser = new PDFParser(bais); @@ -111,9 +115,8 @@ public abstract class PDFUtilities parser.setTempDirectory(temporary_dir); parser.parse(); - PDDocument pdfDocument_ = parser.getPDDocument(); + pdfDocument_ = parser.getPDDocument(); float last_page_length = calculateLastPageLength(pdfDocument_, effectivePageHeight); - pdfDocument_.close(); return last_page_length; } @@ -124,6 +127,8 @@ public abstract class PDFUtilities catch (DocumentException e) { throw new PDFDocumentException(201, e); + } finally { + PDFASUtils.closeQuietly(pdfDocument_); } } -- cgit v1.2.3