From 009b3970136978f29e6a25a71036a06692c65660 Mon Sep 17 00:00:00 2001 From: Christian Maierhofer Date: Mon, 13 Jun 2016 15:49:36 +0200 Subject: added config params for bg-detection and debug rendering --- .../lib/impl/pdfbox2/positioning/Positioning.java | 12 ++-- .../impl/signing/pdfbox2/PADESPDFBOXSigner.java | 6 +- .../wag/egov/egiz/pdfbox2/pdf/PDFUtilities.java | 83 ++++------------------ 3 files changed, 25 insertions(+), 76 deletions(-) (limited to 'pdf-as-pdfbox-2/src/main/java/at') diff --git a/pdf-as-pdfbox-2/src/main/java/at/gv/egiz/pdfas/lib/impl/pdfbox2/positioning/Positioning.java b/pdf-as-pdfbox-2/src/main/java/at/gv/egiz/pdfas/lib/impl/pdfbox2/positioning/Positioning.java index 0966ba11..4fbedac7 100644 --- a/pdf-as-pdfbox-2/src/main/java/at/gv/egiz/pdfas/lib/impl/pdfbox2/positioning/Positioning.java +++ b/pdf-as-pdfbox-2/src/main/java/at/gv/egiz/pdfas/lib/impl/pdfbox2/positioning/Positioning.java @@ -35,6 +35,8 @@ import org.slf4j.LoggerFactory; import at.gv.egiz.pdfas.common.exceptions.PDFIOException; import at.gv.egiz.pdfas.common.exceptions.PdfAsException; +import at.gv.egiz.pdfas.common.settings.ISettings; +import at.gv.egiz.pdfas.common.settings.Settings; import at.gv.egiz.pdfas.lib.impl.pdfbox2.utils.PdfBoxUtils; import at.gv.egiz.pdfas.lib.impl.stamping.IPDFVisualObject; import at.knowcenter.wag.egov.egiz.pdf.PositioningInstruction; @@ -73,15 +75,16 @@ public class Positioning { * The pdf. * @param pdf_table * The pdf table to be written. + * @param settings * @return Returns the PositioningInformation. * @throws PdfAsException * F.e. */ public static PositioningInstruction determineTablePositioning( TablePos pos, String signature_type, PDDocument pdfDataSource, - IPDFVisualObject pdf_table, boolean legacy32, boolean legacy40) throws PdfAsException { + IPDFVisualObject pdf_table, boolean legacy32, boolean legacy40, ISettings settings) throws PdfAsException { return adjustSignatureTableandCalculatePosition(pdfDataSource, - pdf_table, pos, legacy32, legacy40); + pdf_table, pos, legacy32, legacy40, settings); } private static PDRectangle rotateBox(PDRectangle cropBox, int rotation) { @@ -149,13 +152,14 @@ public class Positioning { * The PDF document. * @param pdf_table * The PDFPTable to be placed. + * @param settings * @return Returns the position where the PDFPTable should be placed. * @throws PdfAsException * F.e. */ public static PositioningInstruction adjustSignatureTableandCalculatePosition( final PDDocument pdfDataSource, IPDFVisualObject pdf_table, - TablePos pos, boolean legacy32, boolean legacy40) throws PdfAsException { + TablePos pos, boolean legacy32, boolean legacy40, ISettings settings) throws PdfAsException { PdfBoxUtils.checkPDFPermissions(pdfDataSource); // get pages of currentdocument @@ -271,7 +275,7 @@ public class Positioning { float pre_page_length = Float.NEGATIVE_INFINITY; try { - pre_page_length = PDFUtilities.getMaxYPosition(pdfDataSource, page-1, pdf_table, SIGNATURE_MARGIN_VERTICAL, footer_line); + pre_page_length = PDFUtilities.getMaxYPosition(pdfDataSource, page-1, pdf_table, SIGNATURE_MARGIN_VERTICAL, footer_line, settings); //pre_page_length = PDFUtilities.getFreeTablePosition(pdfDataSource, page-1, pdf_table,SIGNATURE_MARGIN_VERTICAL); } catch (IOException e) { logger.warn("Could not determine page length, using -INFINITY"); diff --git a/pdf-as-pdfbox-2/src/main/java/at/gv/egiz/pdfas/lib/impl/signing/pdfbox2/PADESPDFBOXSigner.java b/pdf-as-pdfbox-2/src/main/java/at/gv/egiz/pdfas/lib/impl/signing/pdfbox2/PADESPDFBOXSigner.java index d105174b..6412efb9 100644 --- a/pdf-as-pdfbox-2/src/main/java/at/gv/egiz/pdfas/lib/impl/signing/pdfbox2/PADESPDFBOXSigner.java +++ b/pdf-as-pdfbox-2/src/main/java/at/gv/egiz/pdfas/lib/impl/signing/pdfbox2/PADESPDFBOXSigner.java @@ -266,7 +266,7 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { */ PositioningInstruction positioningInstruction = Positioning.determineTablePositioning(tablePos, "", - doc, visualObject, legacy32Position, legacy40Position); + doc, visualObject, legacy32Position, legacy40Position, pdfObject.getStatus().getSettings()); logger.debug("Positioning: {}", positioningInstruction.toString()); @@ -733,10 +733,10 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { PositioningInstruction positioningInstruction = null; if (signaturePosString != null) { positioningInstruction = Positioning.determineTablePositioning(new TablePos(signaturePosString), "", - origDoc, visualObject, false, false); + origDoc, visualObject, false, false, pdfObject.getStatus().getSettings()); } else { positioningInstruction = Positioning.determineTablePositioning(new TablePos(), "", origDoc, - visualObject, false, false); + visualObject, false, false, pdfObject.getStatus().getSettings()); } origDoc.close(); diff --git a/pdf-as-pdfbox-2/src/main/java/at/knowcenter/wag/egov/egiz/pdfbox2/pdf/PDFUtilities.java b/pdf-as-pdfbox-2/src/main/java/at/knowcenter/wag/egov/egiz/pdfbox2/pdf/PDFUtilities.java index 79e75b99..08ef64b7 100644 --- a/pdf-as-pdfbox-2/src/main/java/at/knowcenter/wag/egov/egiz/pdfbox2/pdf/PDFUtilities.java +++ b/pdf-as-pdfbox-2/src/main/java/at/knowcenter/wag/egov/egiz/pdfbox2/pdf/PDFUtilities.java @@ -55,41 +55,38 @@ import java.io.IOException; import java.util.HashMap; import org.apache.pdfbox.pdmodel.PDDocument; -import org.apache.pdfbox.pdmodel.common.PDRectangle; -import org.apache.pdfbox.rendering.PDFRenderer; import org.apache.pdfbox.tools.imageio.ImageIOUtil; +import at.gv.egiz.pdfas.common.settings.ISettings; +import at.gv.egiz.pdfas.lib.api.IConfigurationConstants; import at.gv.egiz.pdfas.lib.impl.pdfbox2.positioning.PositioningRenderer; import at.gv.egiz.pdfas.lib.impl.stamping.IPDFVisualObject; -public abstract class PDFUtilities { +public abstract class PDFUtilities implements IConfigurationConstants{ public static Color MAGIC_COLOR = new Color(152,254,52);// green-ish background public static float getMaxYPosition( - PDDocument pdfDataSource, int page, IPDFVisualObject pdfTable, float signatureMarginVertical, float footer_line) throws IOException { - long t0 = System.currentTimeMillis(); + PDDocument pdfDataSource, int page, IPDFVisualObject pdfTable, float signatureMarginVertical, float footer_line, ISettings settings) throws IOException { + PositioningRenderer renderer = new PositioningRenderer(pdfDataSource); //BufferedImage bim = renderer.renderImage(page); - long t1 = System.currentTimeMillis(); + int width = (int) pdfDataSource.getPage(page).getCropBox().getWidth(); int height = (int) pdfDataSource.getPage(page).getCropBox().getHeight(); BufferedImage bim = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); - long t2 = System.currentTimeMillis(); + Graphics2D graphics = bim.createGraphics(); - long t3 = System.currentTimeMillis(); -// graphics.setPaint(MAGIC_COLOR); -// graphics.fillRect(0, 0, width, height); graphics.setBackground(MAGIC_COLOR); renderer.renderPageToGraphics(page, graphics); - long t4 = System.currentTimeMillis(); + Color bgColor = MAGIC_COLOR; - if(true){ //only used if background color should be determined automatically + if("true".equals(settings.getValue(BG_COLOR_DETECTION))){ //only used if background color should be determined automatically bgColor = determineBackgroundColor(bim); } - long t5 = System.currentTimeMillis(); + int yCoord = bim.getHeight() - 1 - (int)footer_line; for(int row = yCoord; row >= 0; row--){ @@ -102,65 +99,13 @@ public abstract class PDFUtilities { } } } - long t6 = System.currentTimeMillis(); - - System.out.println("new Renderer: "+ (t1-t0)); - System.out.println("new BI: "+ (t2-t1)); - System.out.println("Create Graphics: "+ (t3-t2)); - System.out.println("Render to Graphics: "+ (t4-t3)); - System.out.println("Determined bg color: "+ (t5-t4)); - System.out.println("Calc y: "+ (t6-t5)); - -// for(int i=0; i < bim.getWidth(); i++){ -// bim.setRGB(i, yCoord, 255); -// } -// - ImageIOUtil.writeImage(bim, "/home/cmaierhofer/temp/bufferer.png", 72); + String outFile = settings.getValue(SIG_PLACEMENT_DEBUG_OUTPUT); + if(outFile!=null){ + ImageIOUtil.writeImage(bim, outFile, 72); + } return yCoord; } - -// public static float getFreeTablePosition( -// PDDocument pdfDataSource, int page, IPDFVisualObject pdfTable, float signatureMarginVertical) throws IOException { -// -// float table_height = pdfTable.getHeight(); -// -// PDFRenderer renderer = new PDFRenderer(pdfDataSource); -// -// BufferedImage bim = renderer.renderImage(page); -// -// Color bgColor = determineBackgroundColor(bim); -// float posY = bim.getHeight(); -// -// for(int row=0; row= table_height+signatureMarginVertical){ -// posY = row; -// row=bim.getHeight(); -// break; -// } -// } -// } -// } -// -// if(posY == -1) -// return Float.NEGATIVE_INFINITY; -// return posY; -// } public static Color determineBackgroundColor(BufferedImage bim){ -- cgit v1.2.3