aboutsummaryrefslogtreecommitdiff
path: root/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox
diff options
context:
space:
mode:
Diffstat (limited to 'pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox')
-rw-r--r--pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFAsVisualSignatureBuilder.java125
-rw-r--r--pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFBoxTable.java38
2 files changed, 147 insertions, 16 deletions
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 82116474..3885169b 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
@@ -105,6 +105,52 @@ public class PDFAsVisualSignatureBuilder extends PDVisibleSigBuilder {
// draw if boarder > 0
if (border != 0) {
+ float nexty = y + tableHeight;
+ float lasty = nexty;
+ for (int i = 0; i < rows; i++) {
+ ArrayList<Entry> row = abstractTable.getRow(i);
+ // Draw row border!
+ logger.debug("ROW LINE: {} {} {} {}", x, nexty, x + tableWidth,
+ nexty);
+ contentStream.drawLine(x, nexty, x + tableWidth, nexty);
+ lasty = nexty;
+ if (i < abstractTable.getRowHeights().length) {
+ nexty -= abstractTable.getRowHeights()[i] + padding * 2;
+ }
+
+ if (subtable && i + 1 == abstractTable.getRowHeights().length) {
+ nexty -= padding;
+ }
+
+ float nextx = x;
+ float ypos = y;
+ float yheight = y + abstractTable.getHeight();
+ if (subtable) {
+ ypos -= padding;
+ yheight = y + abstractTable.getHeight();
+ }
+
+ for (int j = 0; j < row.size(); j++) {
+ Entry cell = (Entry) row.get(j);
+
+ if (subtable && j == cols) {
+ continue;
+ }
+ logger.debug("COL LINE: {} {} {} {}", nextx, ypos, nextx,
+ yheight);
+ contentStream.drawLine(nextx, lasty, nextx, nexty);
+ for(int k = 0; k < cell.getColSpan(); k++) {
+ if (k + j < colsSizes.length) {
+ nextx += (colsSizes != null) ? colsSizes[k + j] : colWidth;
+ }
+ }
+ }
+ contentStream.drawLine(nextx, lasty, nextx, nexty);
+ }
+
+ contentStream.drawLine(x, nexty, x + tableWidth, nexty);
+
+ /*
// draw the rows
float nexty = y + tableHeight;
for (int i = 0; i <= rows; i++) {
@@ -137,7 +183,7 @@ public class PDFAsVisualSignatureBuilder extends PDVisibleSigBuilder {
if (i < colsSizes.length) {
nextx += (colsSizes != null) ? colsSizes[i] : colWidth;
}
- }
+ }*/
}
float textx = x + padding;
@@ -146,6 +192,10 @@ public class PDFAsVisualSignatureBuilder extends PDVisibleSigBuilder {
ArrayList<Entry> row = abstractTable.getRow(i);
for (int j = 0; j < row.size(); j++) {
Entry cell = (Entry) row.get(j);
+
+ Style inherit_style = Style.doInherit(abstractTable.style, cell.getStyle());
+ cell.setStyle(inherit_style);
+
if (cell.getType() == Entry.TYPE_CAPTION
|| cell.getType() == Entry.TYPE_VALUE) {
@@ -175,9 +225,71 @@ public class PDFAsVisualSignatureBuilder extends PDVisibleSigBuilder {
} else {
contentStream.setFont(textFont, fontSize);
}
- logger.debug("Writing: " + textx + " : " + ttexty + " = "
+
+ // TODO: use halign and valgin
+
+ float fheight = textFont.getFontDescriptor().getFontBoundingBox()
+ .getHeight()
+ / 1000 * fontSize;
+
+ String[] tlines = text.split("\n");
+ float textHeight = fheight * tlines.length;
+
+ Style cellStyle = cell.getStyle();
+ String valign = null;
+ String halign = null;
+
+ if (cell.getType() == Entry.TYPE_CAPTION && cellStyle != null) {
+ valign = cellStyle.getVAlign();
+ halign = cellStyle.getHAlign();
+ } else if (cell.getType() == Entry.TYPE_VALUE && cellStyle != null) {
+ valign = cellStyle.getValueVAlign();
+ halign = cellStyle.getValueHAlign();
+ }
+ float ty = ttexty;
+ if(Style.BOTTOM.equals(valign)) {
+ float bottom_offset = abstractTable.getRowHeights()[i] - textHeight;
+ ty -= bottom_offset;
+ } else if(Style.MIDDLE.equals(valign)) {
+ float bottom_offset = abstractTable.getRowHeights()[i] - textHeight;
+ bottom_offset = bottom_offset / 2.0f;
+ ty -= bottom_offset;
+ }
+
+ float columnWidth = (colsSizes != null) ? colsSizes[j] : colWidth;
+ float maxWidth = 0;
+ for (int k = 0; k < tlines.length; k++) {
+
+ float fwidth;
+ if (textFont instanceof PDType1Font) {
+ fwidth = textFont.getFontDescriptor().getFontBoundingBox().getWidth()
+ / 1000.0f * fontSize;
+ } else {
+ fwidth = textFont.getStringWidth("abcdefghijklmnopqrstuvwxyz ") / 1000.0f * fontSize;
+ fwidth = fwidth / (float)"abcdefghijklmnopqrstuvwxyz".length();
+ }
+
+ float lineWidth = tlines[k].length() * fwidth;
+
+ //float w = textFont.getStringWidth(tlines[k]) / 1000 * fontSize;
+ if (maxWidth < lineWidth) {
+ maxWidth = lineWidth;
+ }
+ }
+
+ float tx = textx;
+ if(Style.CENTER.equals(halign)) {
+ float offset = columnWidth - maxWidth - 2 * padding;
+ offset = offset / 2.0f;
+ tx += offset;
+ } else if(Style.RIGHT.equals(halign)) {
+ float offset = columnWidth - maxWidth - 2 * padding;
+ tx += offset;
+ }
+
+ logger.debug("Writing: " + tx + " : " + ty + " = "
+ text + " as " + cell.getType() + " w " + fontName);
- contentStream.moveTextPositionByAmount(textx, ttexty);
+ contentStream.moveTextPositionByAmount(tx, ty);
if (text.contains("\n")) {
String[] lines = text.split("\n");
@@ -207,6 +319,8 @@ public class PDFAsVisualSignatureBuilder extends PDVisibleSigBuilder {
// contentStream.beginMarkedContentSequence(COSName.ALT,
// name);
+ // TODO: use HAlign
+
float imgy = texty;
if (cell.getStyle().getImageVAlign() != null
&& cell.getStyle().getImageVAlign()
@@ -230,12 +344,17 @@ public class PDFAsVisualSignatureBuilder extends PDVisibleSigBuilder {
// contentStream.endMarkedContentSequence();
} else if (cell.getType() == Entry.TYPE_TABLE) {
+
float tableY = texty - abstractTable.getRowHeights()[i]
- padding;
float tableX = textx;
// texty = texty - padding;
tableX = textx - padding;
PDFBoxTable tbl_value = (PDFBoxTable) cell.getValue();
+
+ Style inherit_styletab = Style.doInherit(abstractTable.style, cell.getStyle());
+ tbl_value.table.setStyle(inherit_styletab);
+
logger.debug("Table: " + tableX + " : " + tableY);
drawTable(page, contentStream, tableX, tableY, tbl_value,
doc, true);
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 e76c849e..1bbfbe9a 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
@@ -11,6 +11,8 @@ import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import com.sun.org.apache.bcel.internal.generic.Type;
+
import at.gv.egiz.pdfas.common.exceptions.PdfAsException;
import at.gv.egiz.pdfas.common.exceptions.PdfAsWrappedIOException;
import at.gv.egiz.pdfas.common.settings.ISettings;
@@ -71,12 +73,11 @@ public class PDFBoxTable {
} catch(PdfAsException e) {
throw new PdfAsWrappedIOException(e);
}
- if (abstractTable.getStyle() != null) {
- style = abstractTable.getStyle();
- }
- if (style == null && parent != null) {
- style = parent.style;
+ if (parent != null) {
+ style = Style.doInherit(abstractTable.getStyle(), parent.style);
+ } else {
+ style = abstractTable.getStyle();
}
if (style == null) {
@@ -87,10 +88,6 @@ public class PDFBoxTable {
String vfontString = style.getValueFont();
- if(fontString == null || vfontString == null) {
-
- }
-
if (parent != null && style == parent.style) {
font = parent.getFont();
@@ -158,14 +155,26 @@ public class PDFBoxTable {
ArrayList<Entry> row = this.table.getRows().get(i);
for (int j = 0; j < row.size(); j++) {
Entry cell = (Entry) row.get(j);
-
- float cellheight = getCellHeight(cell, colWidths[j]);
+
+ float colWidth = colWidths[j];
+
+ int colsleft = cell.getColSpan() - 1;
+
+ if(j + colsleft > colWidths.length) {
+ throw new IOException("Configuration is wrong. Cannot determine column width!");
+ }
+
+ for(int k = 0; k < colsleft; k++) {
+ colWidth = colWidth + colWidths[j+k];
+ }
+
+ float cellheight = getCellHeight(cell, colWidth);
if (rowHeights[i] < cellheight) {
rowHeights[i] = cellheight;
}
- logger.debug("ROW: {} COL: {} Width: {} Height: {}", i, j,
+ logger.debug("ROW: {} COL: {} Width: {} Height: {}", i, j, colWidth,
cellheight);
int span = cell.getColSpan() - 1;
@@ -386,7 +395,7 @@ public class PDFBoxTable {
c = font.getFont(null);
fontSize = font.getFontSize();
}
-
+
float fwidth;
if (c instanceof PDType1Font) {
fwidth = c.getFontDescriptor().getFontBoundingBox().getWidth()
@@ -536,6 +545,9 @@ public class PDFBoxTable {
for (int i = 0; i < colWidths.length; i++) {
logger.debug("\t[{}] : {}", i, this.colWidths[i]);
}
+ logger.debug("\t================================");
+ logger.debug("\tTable:");
+ logger.debug("\t" + this.table.toString());
logger.debug("=====================================================================");
}