From a69453749e6317f15c6bcb6c1da5b33e31244f65 Mon Sep 17 00:00:00 2001 From: Andreas Fitzek Date: Fri, 24 Oct 2014 09:44:56 +0200 Subject: Image Scaling --- .../pdfbox/PDFAsVisualSignatureBuilder.java | 19 +------- .../lib/impl/stamping/pdfbox/PDFBoxTable.java | 51 +++++++++++++++++----- .../lib/impl/stamping/pdfbox/PdfBoxStamper.java | 7 ++- .../impl/stamping/pdfbox/PdfBoxVisualObject.java | 14 ++++-- 4 files changed, 57 insertions(+), 34 deletions(-) (limited to 'pdf-as-pdfbox/src/main/java') diff --git a/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFAsVisualSignatureBuilder.java b/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFAsVisualSignatureBuilder.java index e3ee19f6..f5776c0a 100644 --- a/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFAsVisualSignatureBuilder.java +++ b/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFAsVisualSignatureBuilder.java @@ -551,24 +551,7 @@ public class PDFAsVisualSignatureBuilder extends PDVisibleSigBuilder { if (cell.getType() == Entry.TYPE_IMAGE) { String img_ref = (String) cell.getValue(); if (!images.containsKey(img_ref)) { - File img_file = new File(img_ref); - if (!img_file.isAbsolute()) { - logger.debug("Image file declaration is relative. Prepending path of resources directory."); - logger.debug("Image Location: " - + settings.getWorkingDirectory() - + File.separator + img_ref); - img_file = new File(settings.getWorkingDirectory() - + File.separator + img_ref); - } else { - logger.debug("Image file declaration is absolute. Skipping file relocation."); - } - - if (!img_file.exists()) { - logger.debug("Image file \"" - + img_file.getCanonicalPath() - + "\" doesn't exist."); - throw new PdfAsException("error.pdf.stamp.04"); - } + File img_file = ImageUtils.getImageFile(img_ref, settings); BufferedImage img = null; try { diff --git a/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFBoxTable.java b/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFBoxTable.java index f83d099a..2fec7083 100644 --- a/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFBoxTable.java +++ b/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFBoxTable.java @@ -24,21 +24,22 @@ package at.gv.egiz.pdfas.lib.impl.stamping.pdfbox; import java.awt.Color; +import java.awt.Dimension; +import java.io.File; +import java.io.FileInputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.List; -import org.apache.fontbox.ttf.TrueTypeFont; import org.apache.pdfbox.pdmodel.font.PDFont; -import org.apache.pdfbox.pdmodel.font.PDTrueTypeFont; -import org.apache.pdfbox.pdmodel.font.PDType1Font; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import at.gv.egiz.pdfas.common.exceptions.PdfAsException; import at.gv.egiz.pdfas.common.exceptions.PdfAsWrappedIOException; import at.gv.egiz.pdfas.common.settings.ISettings; +import at.gv.egiz.pdfas.common.utils.ImageUtils; import at.gv.egiz.pdfas.common.utils.StringUtils; import at.knowcenter.wag.egov.egiz.table.Entry; import at.knowcenter.wag.egov.egiz.table.Style; @@ -144,7 +145,7 @@ public class PDFBoxTable { } public PDFBoxTable(Table abstractTable, PDFBoxTable parent, float fixSize, - ISettings settings) throws IOException { + ISettings settings) throws IOException, PdfAsException { this.settings = settings; initializeStyle(abstractTable, parent); float[] relativSizes = abstractTable.getColsRelativeWith(); @@ -177,13 +178,13 @@ public class PDFBoxTable { } public PDFBoxTable(Table abstractTable, PDFBoxTable parent, - ISettings settings) throws IOException { + ISettings settings) throws IOException, PdfAsException { this.settings = settings; initializeStyle(abstractTable, parent); this.calculateWidthHeight(); } - private void calculateHeightsBasedOnWidths() throws IOException { + private void calculateHeightsBasedOnWidths() throws IOException, PdfAsException { int rows = this.table.getRows().size(); rowHeights = new float[rows]; addPadding = new boolean[rows]; @@ -227,7 +228,7 @@ public class PDFBoxTable { calcTotals(); } - private void calculateWidthHeight() throws IOException { + private void calculateWidthHeight() throws IOException, PdfAsException { int cols = this.table.getMaxCols(); colWidths = new float[cols]; @@ -299,7 +300,7 @@ public class PDFBoxTable { } } - private float getCellWidth(Entry cell) throws IOException { + private float getCellWidth(Entry cell) throws IOException, PdfAsException { boolean isValue = true; switch (cell.getType()) { case Entry.TYPE_CAPTION: @@ -503,7 +504,7 @@ public class PDFBoxTable { return heights; } - private float getCellHeight(Entry cell, float width) throws IOException { + private float getCellHeight(Entry cell, float width) throws IOException, PdfAsException { boolean isValue = true; switch (cell.getType()) { case Entry.TYPE_CAPTION: @@ -526,12 +527,25 @@ public class PDFBoxTable { float[] heights = getStringHeights(lines, c, fontSize); return fontSize * heights.length + padding * 2; case Entry.TYPE_IMAGE: + String imageFile = (String)cell.getValue(); + File img_file = ImageUtils.getImageFile(imageFile, settings); if (style != null && style.getImageScaleToFit() != null) { //if (style.getImageScaleToFit().getHeight() < width) { return style.getImageScaleToFit().getHeight() + padding * 2; //} } - return width + padding * 2; + FileInputStream fis = new FileInputStream(img_file); + try { + Dimension dim = ImageUtils.getImageDimensions(fis); + if(dim.getHeight() > 80.0f) { + return width + padding * 2; + } + return (float)dim.getHeight() + padding * 2; + } finally { + if(fis != null) { + fis.close(); + } + } case Entry.TYPE_TABLE: PDFBoxTable pdfBoxTable = null; if (cell.getValue() instanceof Table) { @@ -554,7 +568,7 @@ public class PDFBoxTable { return 0; } - private float getCellHeight(Entry cell) throws IOException { + private float getCellHeight(Entry cell) throws IOException, PdfAsException { boolean isValue = true; switch (cell.getType()) { case Entry.TYPE_CAPTION: @@ -579,10 +593,23 @@ public class PDFBoxTable { return fontSize + padding * 2; } case Entry.TYPE_IMAGE: + String imageFile = (String)cell.getValue(); + File img_file = ImageUtils.getImageFile(imageFile, settings); if (style != null && style.getImageScaleToFit() != null) { return style.getImageScaleToFit().getHeight() + padding * 2; } - return 80.f + padding * 2; + FileInputStream fis = new FileInputStream(img_file); + try { + Dimension dim = ImageUtils.getImageDimensions(fis); + if(dim.getHeight() > 80.0f) { + return 80.0f + padding * 2; + } + return (float)dim.getHeight() + padding * 2; + } finally { + if(fis != null) { + fis.close(); + } + } case Entry.TYPE_TABLE: PDFBoxTable pdfBoxTable = null; if (cell.getValue() instanceof Table) { diff --git a/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PdfBoxStamper.java b/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PdfBoxStamper.java index c60c4283..5c190883 100644 --- a/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PdfBoxStamper.java +++ b/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PdfBoxStamper.java @@ -26,6 +26,7 @@ package at.gv.egiz.pdfas.lib.impl.stamping.pdfbox; import java.io.IOException; import at.gv.egiz.pdfas.common.exceptions.PdfAsException; +import at.gv.egiz.pdfas.common.exceptions.PdfAsWrappedIOException; import at.gv.egiz.pdfas.common.settings.ISettings; import at.gv.egiz.pdfas.lib.impl.stamping.IPDFStamper; import at.gv.egiz.pdfas.lib.impl.stamping.IPDFVisualObject; @@ -44,7 +45,11 @@ public class PdfBoxStamper implements IPDFStamper { } public IPDFVisualObject createVisualPDFObject(PDFObject pdf, Table table) throws IOException { - return new PdfBoxVisualObject(table, pdf.getStatus().getSettings()); + try { + return new PdfBoxVisualObject(table, pdf.getStatus().getSettings()); + } catch (PdfAsException e) { + throw new PdfAsWrappedIOException(e); + } } public byte[] writeVisualObject(IPDFVisualObject visualObject, diff --git a/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PdfBoxVisualObject.java b/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PdfBoxVisualObject.java index c7623cf9..cc2912ea 100644 --- a/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PdfBoxVisualObject.java +++ b/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PdfBoxVisualObject.java @@ -25,12 +25,19 @@ package at.gv.egiz.pdfas.lib.impl.stamping.pdfbox; import java.io.IOException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import at.gv.egiz.pdfas.common.exceptions.PdfAsException; import at.gv.egiz.pdfas.common.settings.ISettings; import at.gv.egiz.pdfas.lib.impl.stamping.IPDFVisualObject; import at.knowcenter.wag.egov.egiz.table.Table; public class PdfBoxVisualObject implements IPDFVisualObject { + private static final Logger logger = LoggerFactory + .getLogger(PdfBoxVisualObject.class); + private Table abstractTable; private PDFBoxTable table; private float width; @@ -40,7 +47,7 @@ public class PdfBoxVisualObject implements IPDFVisualObject { private ISettings settings; public PdfBoxVisualObject(Table table, ISettings settings) - throws IOException { + throws IOException, PdfAsException { this.abstractTable = table; this.table = new PDFBoxTable(table, null, settings); this.settings = settings; @@ -54,8 +61,9 @@ public class PdfBoxVisualObject implements IPDFVisualObject { try { table = new PDFBoxTable(abstractTable, null, this.width, settings); } catch (IOException e) { - // should not occur - e.printStackTrace(); + logger.error("Failed to fix width of Table!", e); + } catch (PdfAsException e) { + logger.error("Failed to fix width of Table!", e); } } -- cgit v1.2.3