From 23a283834becf8323489231ed5c48725f9716bb6 Mon Sep 17 00:00:00 2001 From: Andreas Fitzek Date: Wed, 25 Feb 2015 12:50:41 +0100 Subject: fixed base64 image value --- .../pdfbox/PDFAsVisualSignatureBuilder.java | 26 +----- .../lib/impl/stamping/pdfbox/PDFBoxTable.java | 100 ++++++++++----------- .../gv/egiz/pdfas/lib/testpdfbox/TTFFontTest.java | 45 ++++++++++ 3 files changed, 92 insertions(+), 79 deletions(-) create mode 100644 pdf-as-pdfbox/src/test/java/at/gv/egiz/pdfas/lib/testpdfbox/TTFFontTest.java (limited to 'pdf-as-pdfbox/src') 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 de944d43..d0b71fd0 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 @@ -181,31 +181,7 @@ public class PDFAsVisualSignatureBuilder extends PDVisibleSigBuilder implements String img_value = (String) cell.getValue(); String img_ref = createHashedId(img_value); if (!images.containsKey(img_ref)) { - BufferedImage img = null; - - try { - File img_file = ImageUtils.getImageFile(img_value, settings); - - try { - img = ImageIO.read(img_file); - } catch (IOException e) { - throw new PdfAsException("error.pdf.stamp.04", e); - } - } catch(PdfAsException | IOException e) { - ByteArrayInputStream bais = null; - try { - bais = new ByteArrayInputStream(Base64.decodeBase64(img_value)); - img = ImageIO.read(bais); - bais.close(); - } catch(Throwable e1) { - // Ignore value is not base 64! - logger.debug("Value is not base64: ", e1); - // rethrow e - throw e; - } finally { - IOUtils.closeQuietly(bais); - } - } + BufferedImage img = ImageUtils.getImage(img_value, settings); float width = colsSizes[j]; float height = table.getRowHeights()[i] + padding * 2; 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 22947643..8959d631 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 @@ -69,7 +69,7 @@ public class PDFBoxTable { float[] colWidths; PDDocument originalDoc; - + private void normalizeContent(Table abstractTable) throws PdfAsException { try { int rows = abstractTable.getRows().size(); @@ -93,8 +93,8 @@ public class PDFBoxTable { } } - private void initializeStyle(Table abstractTable, PDFBoxTable parent, PDDocument originalDoc) - throws IOException { + private void initializeStyle(Table abstractTable, PDFBoxTable parent, + PDDocument originalDoc) throws IOException { this.table = abstractTable; try { normalizeContent(abstractTable); @@ -148,7 +148,8 @@ public class PDFBoxTable { } public PDFBoxTable(Table abstractTable, PDFBoxTable parent, float fixSize, - ISettings settings, PDDocument originalDoc) throws IOException, PdfAsException { + ISettings settings, PDDocument originalDoc) throws IOException, + PdfAsException { this.settings = settings; this.originalDoc = originalDoc; initializeStyle(abstractTable, parent, originalDoc); @@ -177,19 +178,22 @@ public class PDFBoxTable { } } calculateHeightsBasedOnWidths(); - - logger.debug("Generating Table with fixed With {} got width {}", fixSize, getWidth()); + + logger.debug("Generating Table with fixed With {} got width {}", + fixSize, getWidth()); } public PDFBoxTable(Table abstractTable, PDFBoxTable parent, - ISettings settings, PDDocument originalDoc) throws IOException, PdfAsException { + ISettings settings, PDDocument originalDoc) throws IOException, + PdfAsException { this.settings = settings; this.originalDoc = originalDoc; initializeStyle(abstractTable, parent, originalDoc); this.calculateWidthHeight(); } - private void calculateHeightsBasedOnWidths() throws IOException, PdfAsException { + private void calculateHeightsBasedOnWidths() throws IOException, + PdfAsException { int rows = this.table.getRows().size(); rowHeights = new float[rows]; addPadding = new boolean[rows]; @@ -203,7 +207,7 @@ public class PDFBoxTable { for (int j = 0; j < row.size(); j++) { Entry cell = (Entry) row.get(j); - float colWidth = 0;//colWidths[j]; + float colWidth = 0;// colWidths[j]; int colsleft = cell.getColSpan(); @@ -288,16 +292,15 @@ public class PDFBoxTable { ArrayList row = this.table.getRows().get(i); for (int j = 0; j < row.size(); j++) { Entry cell = (Entry) row.get(j); - if(cell.getType() == Entry.TYPE_TABLE) { - PDFBoxTable tbl = (PDFBoxTable)cell.getValue(); - if(rowHeights[i] != tbl.getHeight()) - { + if (cell.getType() == Entry.TYPE_TABLE) { + PDFBoxTable tbl = (PDFBoxTable) cell.getValue(); + if (rowHeights[i] != tbl.getHeight()) { tbl.setHeight(rowHeights[i]); } } } } - + this.tableWidth = 0; for (int i = 0; i < colWidths.length; i++) { @@ -489,27 +492,31 @@ public class PDFBoxTable { float maxLineHeight = 0; try { byte[] linebytes = StringUtils.applyWinAnsiEncoding(lines[i]); - for(int j = 0; j < linebytes.length; j++) { - float he = c.getFontHeight(linebytes, j, 1) / 1000 * fontSize; - if(he > maxLineHeight) { + for (int j = 0; j < linebytes.length; j++) { + float he = c.getFontHeight(linebytes, j, 1) / 1000 + * fontSize; + if (he > maxLineHeight) { maxLineHeight = he; } } } catch (UnsupportedEncodingException e) { logger.warn("failed to determine String height", e); - maxLineHeight = c.getFontDescriptor().getCapHeight() / 1000 * fontSize; + maxLineHeight = c.getFontDescriptor().getCapHeight() / 1000 + * fontSize; } catch (IOException e) { logger.warn("failed to determine String height", e); - maxLineHeight = c.getFontDescriptor().getCapHeight() / 1000 * fontSize; + maxLineHeight = c.getFontDescriptor().getCapHeight() / 1000 + * fontSize; } - + heights[i] = maxLineHeight; } - + return heights; } - private float getCellHeight(Entry cell, float width) throws IOException, PdfAsException { + private float getCellHeight(Entry cell, float width) throws IOException, + PdfAsException { boolean isValue = true; switch (cell.getType()) { case Entry.TYPE_CAPTION: @@ -530,27 +537,19 @@ public class PDFBoxTable { fontSize); cell.setValue(concatLines(lines)); float[] heights = getStringHeights(lines, c, fontSize); - return fontSize * heights.length + padding * 2; + return fontSize * heights.length + padding * 2; case Entry.TYPE_IMAGE: - String imageFile = (String)cell.getValue(); - File img_file = ImageUtils.getImageFile(imageFile, settings); + String imageFile = (String) cell.getValue(); if (style != null && style.getImageScaleToFit() != null) { - //if (style.getImageScaleToFit().getHeight() < width) { - return style.getImageScaleToFit().getHeight() + padding * 2; - //} + // if (style.getImageScaleToFit().getHeight() < width) { + return style.getImageScaleToFit().getHeight() + 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(); - } + Dimension dim = ImageUtils.getImageDimensions(imageFile, settings); + if (dim.getHeight() > 80.0f) { + return width + padding * 2; } + return (float) dim.getHeight() + padding * 2; case Entry.TYPE_TABLE: PDFBoxTable pdfBoxTable = null; if (cell.getValue() instanceof Table) { @@ -598,23 +597,16 @@ public class PDFBoxTable { return fontSize + padding * 2; } case Entry.TYPE_IMAGE: - String imageFile = (String)cell.getValue(); - File img_file = ImageUtils.getImageFile(imageFile, settings); + String imageFile = (String) cell.getValue(); if (style != null && style.getImageScaleToFit() != null) { return style.getImageScaleToFit().getHeight() + 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(); - } + Dimension dim = ImageUtils.getImageDimensions(imageFile, settings); + if (dim.getHeight() > 80.0f) { + return 80.0f + padding * 2; } + return (float) dim.getHeight() + padding * 2; + case Entry.TYPE_TABLE: PDFBoxTable pdfBoxTable = null; if (cell.getValue() instanceof Table) { @@ -648,10 +640,10 @@ public class PDFBoxTable { public float getHeight() { return tableHeight; } - + public void setHeight(float height) { float diff = height - this.getHeight(); - if(diff > 0) { + if (diff > 0) { this.rowHeights[rowHeights.length - 1] += diff; calcTotals(); } else { diff --git a/pdf-as-pdfbox/src/test/java/at/gv/egiz/pdfas/lib/testpdfbox/TTFFontTest.java b/pdf-as-pdfbox/src/test/java/at/gv/egiz/pdfas/lib/testpdfbox/TTFFontTest.java new file mode 100644 index 00000000..fdc59ab8 --- /dev/null +++ b/pdf-as-pdfbox/src/test/java/at/gv/egiz/pdfas/lib/testpdfbox/TTFFontTest.java @@ -0,0 +1,45 @@ +package at.gv.egiz.pdfas.lib.testpdfbox; + +import java.io.File; +import java.util.Iterator; +import java.util.List; + +import org.apache.pdfbox.cos.COSBase; +import org.apache.pdfbox.cos.COSName; +import org.apache.pdfbox.cos.COSObject; +import org.apache.pdfbox.pdmodel.PDDocument; + +public class TTFFontTest { + + public static void main(String[] args) { + try { + PDDocument doc = PDDocument.load(new File("/home/afitzek/Downloads/pdf_groesse/willenserklaerung_signedByUser.pdf")); + + List cosObjects = doc.getDocument().getObjectsByType(COSName.FONT); + + Iterator cosObjectIt = cosObjects.iterator(); + + while(cosObjectIt.hasNext()) { + COSObject cosObject = cosObjectIt.next(); + COSBase subType = cosObject.getItem(COSName.SUBTYPE); + COSBase baseFont = cosObject.getItem(COSName.BASE_FONT); + COSBase aTest = cosObject.getItem(COSName.A); + + System.out.println(aTest); + + if(subType.equals(COSName.TRUE_TYPE)) { + System.out.println("Object Number: " + cosObject.getObjectNumber().intValue() + + subType.toString()); + System.out.println(" BaseFont: " + baseFont.toString()); + } + + + } + + + } catch(Throwable e) { + e.printStackTrace(); + } + } + +} -- cgit v1.2.3