From 8233cbd41a3aa41236ec505ab173437995287637 Mon Sep 17 00:00:00 2001 From: Andreas Fitzek Date: Fri, 6 Jun 2014 15:31:09 +0200 Subject: Visual Block changes + Configuration updates --- .../pdfbox/PDFAsVisualSignatureBuilder.java | 15 +++--- .../lib/impl/stamping/pdfbox/PDFBoxTable.java | 58 +++++++++++++++++++--- 2 files changed, 60 insertions(+), 13 deletions(-) (limited to 'pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib') diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFAsVisualSignatureBuilder.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFAsVisualSignatureBuilder.java index e64d735a..cd3fd716 100644 --- a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFAsVisualSignatureBuilder.java +++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFAsVisualSignatureBuilder.java @@ -209,7 +209,7 @@ public class PDFAsVisualSignatureBuilder extends PDVisibleSigBuilder { } String text = (String) cell.getValue(); - float ttexty = texty - padding - fontSize; + float ttexty = texty - fontSize - padding * 0.5f; // COSName name = COSName.getPDFName("ANDI_TAG!"); // contentStream.beginMarkedContentSequence(COSName.ALT, // name); @@ -260,17 +260,18 @@ public class PDFAsVisualSignatureBuilder extends PDVisibleSigBuilder { float columnWidth = (colsSizes != null) ? colsSizes[j] : colWidth; float maxWidth = 0; for (int k = 0; k < tlines.length; k++) { - - float fwidth; + float lineWidth; if (textFont instanceof PDType1Font) { - fwidth = textFont.getFontDescriptor().getFontBoundingBox().getWidth() - / 1000.0f * fontSize; + lineWidth = textFont.getStringWidth(tlines[k]) / 1000.0f * fontSize; + //fwidth = textFont.getFontDescriptor().getFontBoundingBox().getWidth() + // / 1000.0f * fontSize; } else { - fwidth = textFont.getStringWidth("abcdefghijklmnopqrstuvwxyz ") / 1000.0f * fontSize; + float fwidth = textFont.getStringWidth("abcdefghijklmnopqrstuvwxyz ") / 1000.0f * fontSize; fwidth = fwidth / (float)"abcdefghijklmnopqrstuvwxyz".length(); + lineWidth = tlines[k].length() * fwidth; } - float lineWidth = tlines[k].length() * fwidth; + //float w = textFont.getStringWidth(tlines[k]) / 1000 * fontSize; if (maxWidth < lineWidth) { diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFBoxTable.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFBoxTable.java index da228dae..31c68ebf 100644 --- a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFBoxTable.java +++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFBoxTable.java @@ -6,7 +6,9 @@ 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; @@ -308,6 +310,47 @@ public class PDFBoxTable { } return v; } + + private String[] breakString(String value, float maxwidth, PDFont font, float fontSize) throws IOException { + String[] words = value.split(" "); + List lines = new ArrayList(); + String cLineValue = ""; + for (int i = 0; i < words.length; i++) { + String word = words[i]; + String[] lineBreaks = word.split("\n"); + if (lineBreaks.length > 1) { + for (int j = 0; j < lineBreaks.length; j++) { + String subword = lineBreaks[j]; + // if (cLine + subword.length() > maxline) { + if(j == 0 && word.startsWith("\n")) { + lines.add(cLineValue.trim()); + cLineValue = ""; + } else if(j != 0) { + lines.add(cLineValue.trim()); + cLineValue = ""; + } + // } + String tmpLine = cLineValue + subword; + float size = font.getStringWidth(tmpLine) / 1000.0f * fontSize; + if (size > maxwidth && cLineValue.length() != 0) { + lines.add(cLineValue.trim()); + cLineValue = ""; + } + cLineValue += subword + " "; + } + } else { + String tmpLine = cLineValue + word; + float size = font.getStringWidth(tmpLine) / 1000.0f * fontSize; + if (size > maxwidth && cLineValue.length() != 0) { + lines.add(cLineValue.trim()); + cLineValue = ""; + } + cLineValue += word + " "; + } + } + lines.add(cLineValue.trim()); + return lines.toArray(new String[0]); + } private String[] breakString(String value, int maxline) { String[] words = value.split(" "); @@ -393,11 +436,14 @@ public class PDFBoxTable { c = font.getFont(null); fontSize = font.getFontSize(); } - + /* float fwidth; if (c instanceof PDType1Font) { fwidth = c.getFontDescriptor().getFontBoundingBox().getWidth() - / 1000.0f * fontSize; + / 1000.0f * fontSize * 0.9f; + } else if (c instanceof PDTrueTypeFont) { + PDTrueTypeFont t = (PDTrueTypeFont)c; + fwidth = t.getAverageFontWidth() / 1000.0f * fontSize; } else { fwidth = c.getStringWidth("abcdefghijklmnopqrstuvwxyz ") / 1000.0f * fontSize; fwidth = fwidth / (float)"abcdefghijklmnopqrstuvwxyz".length(); @@ -405,14 +451,14 @@ public class PDFBoxTable { logger.debug("Font Width: {}", fwidth); int maxcharcount = (int) ((width - padding * 2) / fwidth) - 1; - logger.debug("Max {} chars per line!", maxcharcount); + logger.debug("Max {} chars per line!", maxcharcount); */ float fheight = c.getFontDescriptor().getFontBoundingBox() .getHeight() - / 1000 * fontSize; + / 1000 * fontSize * 0.9f; - String[] lines = breakString(string, maxcharcount); + String[] lines = breakString(string, (width - padding * 2.0f), c, fontSize); cell.setValue(concatLines(lines)); - return fheight * lines.length; + return fheight * lines.length;// - padding; case Entry.TYPE_IMAGE: if(style != null && style.getImageScaleToFit() != null) { if( style.getImageScaleToFit().getHeight() < width) { -- cgit v1.2.3