From 233f87475bac6b505dcd19adaef4598803cafa29 Mon Sep 17 00:00:00 2001 From: Andreas Fitzek Date: Fri, 24 Apr 2015 08:46:48 +0200 Subject: Always remove the alpha channel when signing PDF-A1/b dokuments from signatureimage --- .../lib/impl/signing/pdfbox/PADESPDFBOXSigner.java | 5 +- .../pdfbox/PDFAsVisualSignatureBuilder.java | 104 +++++++++++++-------- .../pdfbox/PDFAsVisualSignatureProperties.java | 11 ++- 3 files changed, 76 insertions(+), 44 deletions(-) (limited to 'pdf-as-pdfbox/src/main') diff --git a/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/signing/pdfbox/PADESPDFBOXSigner.java b/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/signing/pdfbox/PADESPDFBOXSigner.java index 45dfbcc5..d1337ce2 100644 --- a/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/signing/pdfbox/PADESPDFBOXSigner.java +++ b/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/signing/pdfbox/PADESPDFBOXSigner.java @@ -316,7 +316,7 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { PDFAsVisualSignatureProperties properties = new PDFAsVisualSignatureProperties( pdfObject.getStatus().getSettings(), pdfObject, (PdfBoxVisualObject) visualObject, - positioningInstruction); + positioningInstruction, signatureProfileSettings); properties.buildSignature(); @@ -665,7 +665,8 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { PDFAsVisualSignatureProperties properties = new PDFAsVisualSignatureProperties( pdfObject.getStatus().getSettings(), pdfObject, - (PdfBoxVisualObject) visualObject, positioningInstruction); + (PdfBoxVisualObject) visualObject, positioningInstruction, + signatureProfileSettings); properties.buildSignature(); PDDocument visualDoc = PDDocument.load(properties 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 d0b71fd0..5c6610fe 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 @@ -23,6 +23,7 @@ ******************************************************************************/ package at.gv.egiz.pdfas.lib.impl.stamping.pdfbox; +import java.awt.Transparency; import java.awt.geom.AffineTransform; import java.awt.geom.Point2D; import java.awt.image.BufferedImage; @@ -70,7 +71,8 @@ import at.gv.egiz.pdfas.common.settings.ISettings; import at.gv.egiz.pdfas.common.utils.ImageUtils; import at.knowcenter.wag.egov.egiz.table.Entry; -public class PDFAsVisualSignatureBuilder extends PDVisibleSigBuilder implements IDGenerator { +public class PDFAsVisualSignatureBuilder extends PDVisibleSigBuilder implements + IDGenerator { private static final Logger logger = LoggerFactory .getLogger(PDFAsVisualSignatureBuilder.class); @@ -126,12 +128,12 @@ public class PDFAsVisualSignatureBuilder extends PDVisibleSigBuilder implements MessageDigest md = MessageDigest.getInstance("SHA-1"); md.reset(); return Hex.encodeHexString(md.digest(value.getBytes("UTF-8"))); - } catch(Throwable e) { + } catch (Throwable e) { logger.warn("Failed to generate ID for Image using value", e); return value; } } - + private void readTableResources(PDFBoxTable table, PDDocument template) throws PdfAsException, IOException { @@ -181,7 +183,8 @@ 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 = ImageUtils.getImage(img_value, settings); + BufferedImage img = ImageUtils.getImage(img_value, + settings); float width = colsSizes[j]; float height = table.getRowHeights()[i] + padding * 2; @@ -215,15 +218,20 @@ public class PDFAsVisualSignatureBuilder extends PDVisibleSigBuilder implements .floor((double) (scaleFactor * origWidth)); iheight = (float) Math .floor((double) (scaleFactor * origHeight)); - + logger.debug("Scaling image to: " + iwidth + " x " + iheight); - if (img.getAlphaRaster() == null && - img.getColorModel().hasAlpha()) { - img = ImageUtils.removeAlphaChannel(img); + if (this.designer.properties + .getSignatureProfileSettings().isPDFA()) { + //img = ImageUtils.removeAlphaChannel(img); + } else { + if (img.getAlphaRaster() == null + && img.getColorModel().hasAlpha()) { + img = ImageUtils.removeAlphaChannel(img); + } } - //img = ImageUtils.convertRGBAToIndexed(img); + // img = ImageUtils.convertRGBAToIndexed(img); PDXObjectImage pdImage = new PDPixelMap(template, img); @@ -240,18 +248,19 @@ public class PDFAsVisualSignatureBuilder extends PDVisibleSigBuilder implements } } - public void createInnerFormStreamPdfAs(PDDocument template) throws PdfAsException { + public void createInnerFormStreamPdfAs(PDDocument template) + throws PdfAsException { try { // Hint we have to create all PDXObjectImages before creating the // PDPageContentStream // only PDFbox developers know why ... - //if (getStructure().getPage().getResources() != null) { - // innerFormResources = getStructure().getPage().getResources(); - //} else { - innerFormResources = new PDResources(); - getStructure().getPage().setResources(innerFormResources); - //} + // if (getStructure().getPage().getResources() != null) { + // innerFormResources = getStructure().getPage().getResources(); + // } else { + innerFormResources = new PDResources(); + getStructure().getPage().setResources(innerFormResources); + // } readTableResources(properties.getMainTable(), template); PDPageContentStream stream = new PDPageContentStream(template, @@ -268,7 +277,8 @@ public class PDFAsVisualSignatureBuilder extends PDVisibleSigBuilder implements } catch (Throwable e) { logger.warn("Failed to create visual signature block", e); - throw new PdfAsException("Failed to create visual signature block", e); + throw new PdfAsException("Failed to create visual signature block", + e); } } @@ -313,15 +323,15 @@ public class PDFAsVisualSignatureBuilder extends PDVisibleSigBuilder implements String holderFormComment = "q " + m00 + " " + m10 + " " + m01 + " " + m11 + " " + m02 + " " + m12 + " cm /" + innerFormName + " Do Q \n"; - + logger.debug("Holder Form Stream: " + holderFormComment); - + // String innerFormComment = "q 1 0 0 1 0 0 cm /" + imageObjectName + // " Do Q\n"; String innerFormComment = getStructure().getInnterFormStream() .getInputStreamAsString(); - //logger.debug("Inner Form Stream: " + innerFormComment); + // logger.debug("Inner Form Stream: " + innerFormComment); // appendRawCommands(getStructure().getInnterFormStream().createOutputStream(), // getStructure().getInnterFormStream().getInputStreamAsString()); @@ -398,55 +408,67 @@ public class PDFAsVisualSignatureBuilder extends PDVisibleSigBuilder implements Point2D llSrc = new Point2D.Float(); llSrc.setLocation(properties.getxAxis(), properties.getPageHeight() - properties.getyAxis() - properties.getHeight()); - + rect.setUpperRightX((float) upSrc.getX()); rect.setUpperRightY((float) upSrc.getY()); rect.setLowerLeftY((float) llSrc.getY()); rect.setLowerLeftX((float) llSrc.getX()); logger.debug("orig rectangle of signature has been created: {}", rect.toString()); - + AffineTransform transform = new AffineTransform(); transform.setToIdentity(); if (degrees % 360 != 0) { transform.setToRotation(Math.toRadians(degrees), llSrc.getX(), llSrc.getY()); } - - + Point2D upDst = new Point2D.Float(); transform.transform(upSrc, upDst); Point2D llDst = new Point2D.Float(); transform.transform(llSrc, llDst); - + float xPos = properties.getxAxis(); float yPos = properties.getPageHeight() - properties.getyAxis(); logger.debug("POS {} x {}", xPos, yPos); - logger.debug("SIZE {} x {}", properties.getWidth(), properties.getHeight()); + logger.debug("SIZE {} x {}", properties.getWidth(), + properties.getHeight()); // translate according to page! rotation int pageRotation = properties.getPageRotation(); AffineTransform translate = new AffineTransform(); - switch(pageRotation) { - case 90: - translate.setToTranslation(properties.getPageHeight() - - (properties.getPageHeight() - - properties.getyAxis()) - properties.getxAxis() + properties.getHeight(), properties.getxAxis() + properties.getHeight() - (properties.getPageHeight() - - properties.getyAxis())); + switch (pageRotation) { + case 90: + translate.setToTranslation( + properties.getPageHeight() + - (properties.getPageHeight() - properties + .getyAxis()) - properties.getxAxis() + + properties.getHeight(), + properties.getxAxis() + + properties.getHeight() + - (properties.getPageHeight() - properties + .getyAxis())); + break; + case 180: + // translate.setToTranslation(properties.getPageWidth() - + // properties.getxAxis() - properties.getxAxis(), + // properties.getPageHeight() - properties.getyAxis() + + // properties.getHeight()); + translate.setToTranslation( + properties.getPageWidth() - 2 * xPos, + properties.getPageHeight() - 2 + * (yPos - properties.getHeight())); + break; + case 270: + translate.setToTranslation(-properties.getHeight() + yPos - xPos, + properties.getPageWidth() - (yPos - properties.getHeight()) + - xPos); break; - case 180: - //translate.setToTranslation(properties.getPageWidth() - properties.getxAxis() - properties.getxAxis(), - // properties.getPageHeight() - properties.getyAxis() + properties.getHeight()); - translate.setToTranslation(properties.getPageWidth() - 2 * xPos,properties.getPageHeight() - 2 * (yPos - properties.getHeight())); - break; - case 270: - translate.setToTranslation(-properties.getHeight() + yPos - xPos, properties.getPageWidth() - (yPos - properties.getHeight()) - xPos); - break; } translate.transform(upDst, upDst); translate.transform(llDst, llDst); - + rect.setUpperRightX((float) upDst.getX()); rect.setUpperRightY((float) upDst.getY()); rect.setLowerLeftY((float) llDst.getY()); diff --git a/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFAsVisualSignatureProperties.java b/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFAsVisualSignatureProperties.java index 55a6d832..d7ef6fae 100644 --- a/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFAsVisualSignatureProperties.java +++ b/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFAsVisualSignatureProperties.java @@ -36,6 +36,7 @@ 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.settings.SignatureProfileSettings; import at.gv.egiz.pdfas.lib.impl.pdfbox.PDFBOXObject; import at.knowcenter.wag.egov.egiz.pdf.PositioningInstruction; @@ -50,10 +51,13 @@ public class PDFAsVisualSignatureProperties extends PDVisibleSigProperties { private PDFAsVisualSignatureDesigner designer; private float rotationAngle = 0; + + private SignatureProfileSettings signatureProfileSettings; public PDFAsVisualSignatureProperties(ISettings settings, PDFBOXObject object, - PdfBoxVisualObject visObj, PositioningInstruction pos) { + PdfBoxVisualObject visObj, PositioningInstruction pos, SignatureProfileSettings signatureProfileSettings) { this.settings = settings; + this.signatureProfileSettings = signatureProfileSettings; try { main = visObj.getTable(); } catch (Throwable e) { @@ -125,5 +129,10 @@ public class PDFAsVisualSignatureProperties extends PDVisibleSigProperties { public PDFAsVisualSignatureDesigner getDesigner() { return designer; } + + public SignatureProfileSettings getSignatureProfileSettings() { + return signatureProfileSettings; + } + } -- cgit v1.2.3