aboutsummaryrefslogtreecommitdiff
path: root/pdf-as-lib/src/main/java/at/knowcenter/wag/egov/egiz/pdf/PDFUtilities.java
diff options
context:
space:
mode:
Diffstat (limited to 'pdf-as-lib/src/main/java/at/knowcenter/wag/egov/egiz/pdf/PDFUtilities.java')
-rw-r--r--pdf-as-lib/src/main/java/at/knowcenter/wag/egov/egiz/pdf/PDFUtilities.java25
1 files changed, 15 insertions, 10 deletions
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_);
}
}