aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Fitzek <andreas.fitzek@iaik.tugraz.at>2014-10-24 09:44:56 +0200
committerAndreas Fitzek <andreas.fitzek@iaik.tugraz.at>2014-10-24 09:44:56 +0200
commita69453749e6317f15c6bcb6c1da5b33e31244f65 (patch)
treed9074ddda4231ef5b676b98c23701024d08101ba
parent714381323f5808c9519a9b40818265ba5b8c17c5 (diff)
downloadpdf-as-4-a69453749e6317f15c6bcb6c1da5b33e31244f65.tar.gz
pdf-as-4-a69453749e6317f15c6bcb6c1da5b33e31244f65.tar.bz2
pdf-as-4-a69453749e6317f15c6bcb6c1da5b33e31244f65.zip
Image Scaling
-rw-r--r--pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/utils/ImageUtils.java60
-rw-r--r--pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/TableFactory.java4
-rw-r--r--pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFAsVisualSignatureBuilder.java19
-rw-r--r--pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFBoxTable.java51
-rw-r--r--pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PdfBoxStamper.java7
-rw-r--r--pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PdfBoxVisualObject.java14
6 files changed, 119 insertions, 36 deletions
diff --git a/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/utils/ImageUtils.java b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/utils/ImageUtils.java
index c6ad2858..5dd0f636 100644
--- a/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/utils/ImageUtils.java
+++ b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/utils/ImageUtils.java
@@ -25,15 +25,33 @@ package at.gv.egiz.pdfas.common.utils;
import java.awt.AlphaComposite;
import java.awt.Color;
+import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.IndexColorModel;
import java.awt.image.WritableRaster;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Iterator;
+
+import javax.imageio.ImageIO;
+import javax.imageio.ImageReader;
+import javax.imageio.stream.ImageInputStream;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import at.gv.egiz.pdfas.common.exceptions.PdfAsException;
+import at.gv.egiz.pdfas.common.settings.ISettings;
public class ImageUtils {
+ private static final Logger logger = LoggerFactory
+ .getLogger(ImageUtils.class);
+
public static BufferedImage removeAlphaChannel(BufferedImage src) {
if (src.getColorModel().hasAlpha())
{
@@ -89,4 +107,46 @@ public class ImageUtils {
return new BufferedImage(icm2, raster, image.isAlphaPremultiplied(),
null);
}
+
+ public static Dimension getImageDimensions(InputStream is) throws IOException {
+ ImageInputStream in = ImageIO.createImageInputStream(is);
+ try {
+ final Iterator<ImageReader> readers = ImageIO.getImageReaders(in);
+ if (readers.hasNext()) {
+ ImageReader reader = readers.next();
+ try {
+ reader.setInput(in);
+ return new Dimension(reader.getWidth(0), reader.getHeight(0));
+ } finally {
+ reader.dispose();
+ }
+ }
+ throw new IOException("Failed to read Image file");
+ } finally {
+ if (in != null) in.close();
+ }
+ }
+
+ public static File getImageFile(String imageFile, ISettings settings) throws PdfAsException, IOException {
+ File img_file = new File(imageFile);
+ if (!img_file.isAbsolute()) {
+ logger.debug("Image file declaration is relative. Prepending path of resources directory.");
+ logger.debug("Image Location: "
+ + settings.getWorkingDirectory()
+ + File.separator + imageFile);
+ img_file = new File(settings.getWorkingDirectory()
+ + File.separator + imageFile);
+ } 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");
+ }
+
+ return img_file;
+ }
}
diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/TableFactory.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/TableFactory.java
index 939c2b3a..b851af88 100644
--- a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/TableFactory.java
+++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/TableFactory.java
@@ -200,7 +200,7 @@ public class TableFactory implements IProfileConstants {
resolver.resolve(key, value, profile), key);
if (entry != null)
{
- entry.setColSpan(2);
+ //entry.setColSpan(2);
entry.setStyle(defaultValueStyle_);
row.add(entry);
}
@@ -214,7 +214,7 @@ public class TableFactory implements IProfileConstants {
resolver.resolve(key, value, profile), key);
if (entry != null)
{
- entry.setColSpan(2);
+ //entry.setColSpan(2);
entry.setStyle(defaultCaptionStyle_);
row.add(entry);
}
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);
}
}