From f6d416f004ed294cff71e25358d7b37a9750a1b6 Mon Sep 17 00:00:00 2001 From: Thomas <> Date: Fri, 13 Jan 2023 14:02:43 +0100 Subject: fix(sigblock): invalid preview in case of sig-blocks with more than half page height Issue: #68 ## Info: This is only a work-around, because it looks like a general issue in methods to create visual signature tables. T --- .../egiz/pdfas/cli/test/SigblockPreviewMain.java | 37 +++++++++ .../test/mains/SignatureBlockParameterTest.java | 87 ---------------------- .../test/stamping/SignatureBlockParameterTest.java | 87 ++++++++++++++++++++++ .../impl/signing/pdfbox2/PADESPDFBOXSigner.java | 15 ++-- .../stamping/pdfbox2/PDFAsTemplateCreator.java | 49 ++++++++---- .../pdfbox2/PDFAsVisualSignatureProperties.java | 25 ++++--- ...SignatureFieldsAndPlaceHolderExtractorTest.java | 16 ++-- 7 files changed, 192 insertions(+), 124 deletions(-) create mode 100644 pdf-as-cli/src/test/java/at/gv/egiz/pdfas/cli/test/SigblockPreviewMain.java delete mode 100644 pdf-as-lib/src/test/java/at/gv/egiz/pdfas/lib/test/mains/SignatureBlockParameterTest.java create mode 100644 pdf-as-lib/src/test/java/at/gv/egiz/pdfas/lib/test/stamping/SignatureBlockParameterTest.java diff --git a/pdf-as-cli/src/test/java/at/gv/egiz/pdfas/cli/test/SigblockPreviewMain.java b/pdf-as-cli/src/test/java/at/gv/egiz/pdfas/cli/test/SigblockPreviewMain.java new file mode 100644 index 00000000..dba52463 --- /dev/null +++ b/pdf-as-cli/src/test/java/at/gv/egiz/pdfas/cli/test/SigblockPreviewMain.java @@ -0,0 +1,37 @@ +package at.gv.egiz.pdfas.cli.test; + +import java.awt.Image; +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.FileInputStream; + +import javax.imageio.ImageIO; + +import at.gv.egiz.pdfas.lib.api.PdfAs; +import at.gv.egiz.pdfas.lib.api.PdfAsFactory; +import at.gv.egiz.pdfas.lib.api.sign.SignParameter; +import iaik.x509.X509Certificate; +import lombok.SneakyThrows; + +public class SigblockPreviewMain { + + private static final String PDFAS_CONFIG_DIR = "/home/tlenz/Projekte/pdfas4/config/default_4.2.0/"; + + + @SneakyThrows + public static void main(String[] args) throws Exception { + String cwd = System.getProperty("user.dir"); + File output = new File(cwd + File.separator + "build/output.png"); + X509Certificate cert = new X509Certificate(new FileInputStream("/home/tlenz/diverses/cert_debug/binding3.pem")); + + PdfAs pdfas = PdfAsFactory.createPdfAs(new File(PDFAS_CONFIG_DIR)); + SignParameter param = PdfAsFactory.createSignParameter(pdfas.getConfiguration(), null, null); + param.setSignatureProfileId("ERROR_LOGO"); + //param.setSignatureProfileId("SIGNATURBLOCK_DE"); + + + Image placeholder = pdfas.generateVisibleSignaturePreview(param, cert, 72 * 4); + ImageIO.write((BufferedImage)placeholder, "png", output); + } + +} diff --git a/pdf-as-lib/src/test/java/at/gv/egiz/pdfas/lib/test/mains/SignatureBlockParameterTest.java b/pdf-as-lib/src/test/java/at/gv/egiz/pdfas/lib/test/mains/SignatureBlockParameterTest.java deleted file mode 100644 index 23422e8e..00000000 --- a/pdf-as-lib/src/test/java/at/gv/egiz/pdfas/lib/test/mains/SignatureBlockParameterTest.java +++ /dev/null @@ -1,87 +0,0 @@ -package at.gv.egiz.pdfas.lib.test.mains; - -import at.gv.egiz.pdfas.common.settings.DefaultSignatureProfileSettings; -import at.gv.egiz.pdfas.common.utils.CheckSignatureBlockParameters; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.assertFalse; -@RunWith(JUnit4.class) -public class SignatureBlockParameterTest { - - @Test - public void testKeyInvalid() { - assertFalse(checkValid( "aaaaaaaaaaaaaaaaaaaaa" , "^([A-za-z]){1,20}$")); - assertFalse(checkValid( "" , "^([A-za-z]){1,20}$")); - assertFalse(checkValid( "a9" , "^([A-za-z]){1,20}$")); - } - - @Test - public void testKeyValid() { - assertTrue(checkValid( "aaa" +"aa", "^([A-za-z]){1,20}$")); - assertTrue(checkValid( "aaa" , "^([A-za-z]){1,20}$")); - assertTrue(checkValid( "aaaaaaaaaaaaaaaaaaaa", "^([A-za-z]){1,20}$")); - assertTrue(checkValid( "AA", "^([A-za-z]){1,20}$")); - } - - @Test - public void testValueValid() { - assertTrue(checkValid( "aaa" +"aa", "^([\\p{Print}]){1,100}$")); - assertTrue(checkValid( "aaa" , "^([\\p{Print}]){1,100}$")); - assertTrue(checkValid( "a!\"$%&/()[]=?aa-_,;.:[]|{}" , "^([\\p{Print}]){1,100}$")); -// assertTrue(checkValid( "a!\"§$%&/()=?aa" , "^([\\p{Print}]){1,100}$")); - assertTrue(checkValid( "aa!%&/()=?a" , "^([\\p{Print}]){1,100}$")); - assertTrue(checkValid( "a{\"a!%&/()=?a" , "^([\\p{Print}]){1,100}$")); - assertTrue(checkValid( "BB" , "^([\\p{Print}]){1,100}$")); - assertTrue(checkValid( "BB " , "^([\\p{Print}]){1,100}$")); - } - - @Test - public void testValueValidWithDefaultRegex() { - assertTrue(checkValid( "aaa" +"aa", DefaultSignatureProfileSettings.SIG_BLOCK_PARAMETER_DEFAULT_VALUE_REGEX)); - assertTrue(checkValid( "aaa" , DefaultSignatureProfileSettings.SIG_BLOCK_PARAMETER_DEFAULT_VALUE_REGEX)); - assertTrue(checkValid( "a!\"$%&/()[]=?aa-_,;.:[]|{}" , DefaultSignatureProfileSettings.SIG_BLOCK_PARAMETER_DEFAULT_VALUE_REGEX)); -// assertTrue(checkValid( "a!\"§$%&/()=?aa" , DefaultSignatureProfileSettings.SIG_BLOCK_PARAMETER_DEFAULT_VALUE_REGEX)); - assertTrue(checkValid( "aa!%&/()=?a" , DefaultSignatureProfileSettings.SIG_BLOCK_PARAMETER_DEFAULT_VALUE_REGEX)); - assertTrue(checkValid( "a{\"a!%&/()=?a" , DefaultSignatureProfileSettings.SIG_BLOCK_PARAMETER_DEFAULT_VALUE_REGEX)); - assertTrue(checkValid( "BB" , DefaultSignatureProfileSettings.SIG_BLOCK_PARAMETER_DEFAULT_VALUE_REGEX)); - assertTrue(checkValid( "BB " , DefaultSignatureProfileSettings.SIG_BLOCK_PARAMETER_DEFAULT_VALUE_REGEX)); - assertTrue(checkValid("! €%&/()?`$\"§", DefaultSignatureProfileSettings.SIG_BLOCK_PARAMETER_DEFAULT_VALUE_REGEX)); - assertTrue(checkValid("€", DefaultSignatureProfileSettings.SIG_BLOCK_PARAMETER_DEFAULT_VALUE_REGEX)); - assertTrue(checkValid("$", DefaultSignatureProfileSettings.SIG_BLOCK_PARAMETER_DEFAULT_VALUE_REGEX)); - assertTrue(checkValid("! €\"§$%&/()=?`", DefaultSignatureProfileSettings.SIG_BLOCK_PARAMETER_DEFAULT_VALUE_REGEX)); - assertTrue(checkValid("#", DefaultSignatureProfileSettings.SIG_BLOCK_PARAMETER_DEFAULT_VALUE_REGEX)); - assertTrue(checkValid("-_", DefaultSignatureProfileSettings.SIG_BLOCK_PARAMETER_DEFAULT_VALUE_REGEX)); - assertTrue(checkValid("'", DefaultSignatureProfileSettings.SIG_BLOCK_PARAMETER_DEFAULT_VALUE_REGEX)); - assertTrue(checkValid("abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!#-_ß?+#*", - DefaultSignatureProfileSettings.SIG_BLOCK_PARAMETER_DEFAULT_VALUE_REGEX)); - } - - @Test - public void testValueInvalid() { - assertFalse(checkValid((char) 13 +"aaa" +"aa", "^([\\p{Print}]){1,100}$")); - assertFalse(checkValid((char) 13 +"", "^([\\p{Print}]){1,100}$")); - assertFalse(checkValid( "aaa" +(char) 13, "^([\\p{Print}]){1,100}$")); - assertFalse(checkValid("", "^([\\p{Print}]){1,100}$")); - assertFalse(checkValid("a", "^([\\p{Print}]){2,100}$")); - assertFalse(checkValid("aaa"+(char) 13 +"aa", "^([\\p{Print}]){1,100}$")); - } - - @Test - public void testUmlauteValid() { - assertTrue(checkValid( "ä", DefaultSignatureProfileSettings.SIG_BLOCK_PARAMETER_DEFAULT_VALUE_REGEX)); - assertTrue(checkValid( "ö", DefaultSignatureProfileSettings.SIG_BLOCK_PARAMETER_DEFAULT_VALUE_REGEX)); - assertTrue(checkValid( "ü", DefaultSignatureProfileSettings.SIG_BLOCK_PARAMETER_DEFAULT_VALUE_REGEX)); - assertTrue(checkValid( "Ä", DefaultSignatureProfileSettings.SIG_BLOCK_PARAMETER_DEFAULT_VALUE_REGEX)); - assertTrue(checkValid( "Ö", DefaultSignatureProfileSettings.SIG_BLOCK_PARAMETER_DEFAULT_VALUE_REGEX)); - assertTrue(checkValid( "Ü", DefaultSignatureProfileSettings.SIG_BLOCK_PARAMETER_DEFAULT_VALUE_REGEX)); - assertTrue(checkValid( "ÄÖÜöäüjhsbdjej", DefaultSignatureProfileSettings.SIG_BLOCK_PARAMETER_DEFAULT_VALUE_REGEX)); - assertTrue(checkValid( "aauacnhuisdc Ä Ü Ö aaxsa ö aÜaÖa", DefaultSignatureProfileSettings.SIG_BLOCK_PARAMETER_DEFAULT_VALUE_REGEX)); - assertTrue(checkValid( "NMS Güssing", DefaultSignatureProfileSettings.SIG_BLOCK_PARAMETER_DEFAULT_VALUE_REGEX)); - } - - public boolean checkValid(String s, String regex) { - return CheckSignatureBlockParameters.isValid(s, regex); - } -} diff --git a/pdf-as-lib/src/test/java/at/gv/egiz/pdfas/lib/test/stamping/SignatureBlockParameterTest.java b/pdf-as-lib/src/test/java/at/gv/egiz/pdfas/lib/test/stamping/SignatureBlockParameterTest.java new file mode 100644 index 00000000..378cc19d --- /dev/null +++ b/pdf-as-lib/src/test/java/at/gv/egiz/pdfas/lib/test/stamping/SignatureBlockParameterTest.java @@ -0,0 +1,87 @@ +package at.gv.egiz.pdfas.lib.test.stamping; + +import at.gv.egiz.pdfas.common.settings.DefaultSignatureProfileSettings; +import at.gv.egiz.pdfas.common.utils.CheckSignatureBlockParameters; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertFalse; +@RunWith(JUnit4.class) +public class SignatureBlockParameterTest { + + @Test + public void testKeyInvalid() { + assertFalse(checkValid( "aaaaaaaaaaaaaaaaaaaaa" , "^([A-za-z]){1,20}$")); + assertFalse(checkValid( "" , "^([A-za-z]){1,20}$")); + assertFalse(checkValid( "a9" , "^([A-za-z]){1,20}$")); + } + + @Test + public void testKeyValid() { + assertTrue(checkValid( "aaa" +"aa", "^([A-za-z]){1,20}$")); + assertTrue(checkValid( "aaa" , "^([A-za-z]){1,20}$")); + assertTrue(checkValid( "aaaaaaaaaaaaaaaaaaaa", "^([A-za-z]){1,20}$")); + assertTrue(checkValid( "AA", "^([A-za-z]){1,20}$")); + } + + @Test + public void testValueValid() { + assertTrue(checkValid( "aaa" +"aa", "^([\\p{Print}]){1,100}$")); + assertTrue(checkValid( "aaa" , "^([\\p{Print}]){1,100}$")); + assertTrue(checkValid( "a!\"$%&/()[]=?aa-_,;.:[]|{}" , "^([\\p{Print}]){1,100}$")); +// assertTrue(checkValid( "a!\"§$%&/()=?aa" , "^([\\p{Print}]){1,100}$")); + assertTrue(checkValid( "aa!%&/()=?a" , "^([\\p{Print}]){1,100}$")); + assertTrue(checkValid( "a{\"a!%&/()=?a" , "^([\\p{Print}]){1,100}$")); + assertTrue(checkValid( "BB" , "^([\\p{Print}]){1,100}$")); + assertTrue(checkValid( "BB " , "^([\\p{Print}]){1,100}$")); + } + + @Test + public void testValueValidWithDefaultRegex() { + assertTrue(checkValid( "aaa" +"aa", DefaultSignatureProfileSettings.SIG_BLOCK_PARAMETER_DEFAULT_VALUE_REGEX)); + assertTrue(checkValid( "aaa" , DefaultSignatureProfileSettings.SIG_BLOCK_PARAMETER_DEFAULT_VALUE_REGEX)); + assertTrue(checkValid( "a!\"$%&/()[]=?aa-_,;.:[]|{}" , DefaultSignatureProfileSettings.SIG_BLOCK_PARAMETER_DEFAULT_VALUE_REGEX)); +// assertTrue(checkValid( "a!\"§$%&/()=?aa" , DefaultSignatureProfileSettings.SIG_BLOCK_PARAMETER_DEFAULT_VALUE_REGEX)); + assertTrue(checkValid( "aa!%&/()=?a" , DefaultSignatureProfileSettings.SIG_BLOCK_PARAMETER_DEFAULT_VALUE_REGEX)); + assertTrue(checkValid( "a{\"a!%&/()=?a" , DefaultSignatureProfileSettings.SIG_BLOCK_PARAMETER_DEFAULT_VALUE_REGEX)); + assertTrue(checkValid( "BB" , DefaultSignatureProfileSettings.SIG_BLOCK_PARAMETER_DEFAULT_VALUE_REGEX)); + assertTrue(checkValid( "BB " , DefaultSignatureProfileSettings.SIG_BLOCK_PARAMETER_DEFAULT_VALUE_REGEX)); + assertTrue(checkValid("! €%&/()?`$\"§", DefaultSignatureProfileSettings.SIG_BLOCK_PARAMETER_DEFAULT_VALUE_REGEX)); + assertTrue(checkValid("€", DefaultSignatureProfileSettings.SIG_BLOCK_PARAMETER_DEFAULT_VALUE_REGEX)); + assertTrue(checkValid("$", DefaultSignatureProfileSettings.SIG_BLOCK_PARAMETER_DEFAULT_VALUE_REGEX)); + assertTrue(checkValid("! €\"§$%&/()=?`", DefaultSignatureProfileSettings.SIG_BLOCK_PARAMETER_DEFAULT_VALUE_REGEX)); + assertTrue(checkValid("#", DefaultSignatureProfileSettings.SIG_BLOCK_PARAMETER_DEFAULT_VALUE_REGEX)); + assertTrue(checkValid("-_", DefaultSignatureProfileSettings.SIG_BLOCK_PARAMETER_DEFAULT_VALUE_REGEX)); + assertTrue(checkValid("'", DefaultSignatureProfileSettings.SIG_BLOCK_PARAMETER_DEFAULT_VALUE_REGEX)); + assertTrue(checkValid("abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!#-_ß?+#*", + DefaultSignatureProfileSettings.SIG_BLOCK_PARAMETER_DEFAULT_VALUE_REGEX)); + } + + @Test + public void testValueInvalid() { + assertFalse(checkValid((char) 13 +"aaa" +"aa", "^([\\p{Print}]){1,100}$")); + assertFalse(checkValid((char) 13 +"", "^([\\p{Print}]){1,100}$")); + assertFalse(checkValid( "aaa" +(char) 13, "^([\\p{Print}]){1,100}$")); + assertFalse(checkValid("", "^([\\p{Print}]){1,100}$")); + assertFalse(checkValid("a", "^([\\p{Print}]){2,100}$")); + assertFalse(checkValid("aaa"+(char) 13 +"aa", "^([\\p{Print}]){1,100}$")); + } + + @Test + public void testUmlauteValid() { + assertTrue(checkValid( "ä", DefaultSignatureProfileSettings.SIG_BLOCK_PARAMETER_DEFAULT_VALUE_REGEX)); + assertTrue(checkValid( "ö", DefaultSignatureProfileSettings.SIG_BLOCK_PARAMETER_DEFAULT_VALUE_REGEX)); + assertTrue(checkValid( "ü", DefaultSignatureProfileSettings.SIG_BLOCK_PARAMETER_DEFAULT_VALUE_REGEX)); + assertTrue(checkValid( "Ä", DefaultSignatureProfileSettings.SIG_BLOCK_PARAMETER_DEFAULT_VALUE_REGEX)); + assertTrue(checkValid( "Ö", DefaultSignatureProfileSettings.SIG_BLOCK_PARAMETER_DEFAULT_VALUE_REGEX)); + assertTrue(checkValid( "Ü", DefaultSignatureProfileSettings.SIG_BLOCK_PARAMETER_DEFAULT_VALUE_REGEX)); + assertTrue(checkValid( "ÄÖÜöäüjhsbdjej", DefaultSignatureProfileSettings.SIG_BLOCK_PARAMETER_DEFAULT_VALUE_REGEX)); + assertTrue(checkValid( "aauacnhuisdc Ä Ü Ö aaxsa ö aÜaÖa", DefaultSignatureProfileSettings.SIG_BLOCK_PARAMETER_DEFAULT_VALUE_REGEX)); + assertTrue(checkValid( "NMS Güssing", DefaultSignatureProfileSettings.SIG_BLOCK_PARAMETER_DEFAULT_VALUE_REGEX)); + } + + public boolean checkValid(String s, String regex) { + return CheckSignatureBlockParameters.isValid(s, regex); + } +} 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 ef1f14b2..42f16598 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 @@ -363,7 +363,7 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { } properties = new PDFAsVisualSignatureProperties(pdfObject.getStatus().getSettings(), pdfObject, - (PdfBoxVisualObject) visualObject, positioningInstruction, signatureProfileSettings); + (PdfBoxVisualObject) visualObject, positioningInstruction, signatureProfileSettings, false); properties.buildSignature(); @@ -825,7 +825,7 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { positioningInstruction = Positioning.determineTablePositioning(new TablePos(), "", origDoc, visualObject, pdfObject.getStatus().getSettings()); } - + origDoc.close(); final SignaturePositionImpl position = new SignaturePositionImpl(); @@ -839,15 +839,16 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { final PDFAsVisualSignatureProperties properties = new PDFAsVisualSignatureProperties( pdfObject.getStatus().getSettings(), pdfObject, (PdfBoxVisualObject) visualObject, - positioningInstruction, signatureProfileSettings); - + positioningInstruction, signatureProfileSettings, true); + + // build visual-signature form properties.buildSignature(); - PDDocument visualDoc; + + PDDocument visualDoc; synchronized (PDDocument.class) { visualDoc = PDDocument.load(properties.getVisibleSignature()); } - final float stdRes = 72; final float targetRes = resolution; final float factor = targetRes / stdRes; @@ -859,7 +860,7 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { visualDoc.close(); pdfRenderer = null; - + final BufferedImage cutOut = new BufferedImage( (int) (position.getWidth() * factor), (int) (position.getHeight() * factor), diff --git a/pdf-as-pdfbox-2/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox2/PDFAsTemplateCreator.java b/pdf-as-pdfbox-2/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox2/PDFAsTemplateCreator.java index b07e6ed5..62ad4dbf 100644 --- a/pdf-as-pdfbox-2/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox2/PDFAsTemplateCreator.java +++ b/pdf-as-pdfbox-2/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox2/PDFAsTemplateCreator.java @@ -23,11 +23,11 @@ ******************************************************************************/ package at.gv.egiz.pdfas.lib.impl.stamping.pdfbox2; -import java.io.*; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; -import at.gv.egiz.pdfas.common.settings.SignatureProfileSettings; -import at.gv.egiz.pdfas.lib.impl.stamping.TableFactory; -import org.apache.pdfbox.cos.COSName; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDPage; import org.apache.pdfbox.pdmodel.PDResources; @@ -42,6 +42,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import at.gv.egiz.pdfas.common.exceptions.PdfAsException; +import at.gv.egiz.pdfas.common.settings.SignatureProfileSettings; public class PDFAsTemplateCreator extends PDFTemplateCreator { @@ -54,7 +55,7 @@ public class PDFAsTemplateCreator extends PDFTemplateCreator { } - public InputStream buildPDF(PDFAsVisualSignatureDesigner properties, PDDocument originalDocument) + public InputStream buildPDF(PDFAsVisualSignatureDesigner properties, PDDocument originalDocument, boolean buildPreviewOnly) throws IOException, PdfAsException { logger.debug("pdf building has been started"); PDFTemplateStructure pdfStructure = pdfBuilder.getStructure(); @@ -80,7 +81,7 @@ public class PDFAsTemplateCreator extends PDFTemplateCreator { // create signature this.pdfBuilder.createSignature(pdSignatureField, page, properties.getSignatureFieldName()); - + // that is /AcroForm/DR entry this.pdfBuilder.createAcroFormDictionary(acroForm, pdSignatureField); @@ -92,6 +93,8 @@ public class PDFAsTemplateCreator extends PDFTemplateCreator { this.pdfBuilder.createSignatureRectangle(pdSignatureField, properties, properties.getRotation() + properties.getPageRotation()); this.pdfBuilder.createFormaterRectangle(properties.getFormaterRectangleParams()); PDRectangle formater = pdfStructure.getFormatterRectangle(); + + //this.pdfBuilder.createSignatureImage(template, properties.getImageStream()); @@ -108,11 +111,13 @@ public class PDFAsTemplateCreator extends PDFTemplateCreator { // inner formstream, form and resource (hlder form containts inner form) this.pdfBuilder.createInnerFormStreamPdfAs(template, originalDocument); + this.pdfBuilder.createInnerFormResource(); PDResources innerFormResource = pdfStructure.getInnerFormResources(); + this.pdfBuilder.createInnerForm(innerFormResource, pdfStructure.getInnerFormStream(), formater); PDFormXObject innerForm = pdfStructure.getInnerForm(); - + // inner form must be in the holder form as we wrote this.pdfBuilder.insertInnerFormToHolerResources(innerForm, holderFormResources); @@ -127,10 +132,10 @@ public class PDFAsTemplateCreator extends PDFTemplateCreator { // now inject procSetArray /*this.pdfBuilder.injectProcSetArray(innerForm, page, innerFormResource, imageFormResources, holderFormResources, pdfStructure.getProcSet());*/ + this.pdfBuilder.injectProcSetArray(innerForm, page, innerFormResource, null, holderFormResources, pdfStructure.getProcSet()); - /*String imgFormName = pdfStructure.getImageFormName(); String imgName = pdfStructure.getImageName();*/ String innerFormName = pdfStructure.getInnerFormName().getName(); @@ -138,13 +143,16 @@ public class PDFAsTemplateCreator extends PDFTemplateCreator { // now create Streams of AP /*this.pdfBuilder.injectAppearanceStreams(holderFormStream, imageFormStream, imageFormStream, imgFormName, imgName, innerFormName, properties);*/ - this.pdfBuilder.injectAppearanceStreams(holderFormStream, null, null, null, - null, innerFormName, properties); + + if (!buildPreviewOnly) { + this.pdfBuilder.injectAppearanceStreams(holderFormStream, null, null, null, + null, innerFormName, properties); + + } + this.pdfBuilder.createVisualSignature(template); this.pdfBuilder.createWidgetDictionary(pdSignatureField, holderFormResources); - - ByteArrayInputStream in = null; //COSDocument doc = pdfStructure.getVisualSignature(); @@ -152,8 +160,8 @@ public class PDFAsTemplateCreator extends PDFTemplateCreator { //in = pdfStructure.getTemplateAppearanceStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); template.save(baos); - baos.close(); - + baos.close(); + SignatureProfileSettings signatureProfileSettings = this.pdfBuilder.signatureProfileSettings; @@ -188,4 +196,17 @@ public class PDFAsTemplateCreator extends PDFTemplateCreator { // return result of the stream return in; } + +// @SneakyThrows +// private void saveDebuggingSnapshot(PDDocument template, String fileName) { +// ByteArrayOutputStream baos = new ByteArrayOutputStream(); +// template.save(baos); +// baos.close(); +// +// FileOutputStream fos = new FileOutputStream("/home/tlenz/Projekte/pdfas4/source/pdf-as-cli/build/" + fileName + ".pdf"); +// fos.write(baos.toByteArray()); +// fos.close(); +// +// } + } diff --git a/pdf-as-pdfbox-2/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox2/PDFAsVisualSignatureProperties.java b/pdf-as-pdfbox-2/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox2/PDFAsVisualSignatureProperties.java index f482f49f..34093a9d 100644 --- a/pdf-as-pdfbox-2/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox2/PDFAsVisualSignatureProperties.java +++ b/pdf-as-pdfbox-2/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox2/PDFAsVisualSignatureProperties.java @@ -23,12 +23,8 @@ ******************************************************************************/ package at.gv.egiz.pdfas.lib.impl.stamping.pdfbox2; -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.pdfbox2.PDFBOXObject; -import at.knowcenter.wag.egov.egiz.pdf.PositioningInstruction; +import java.io.IOException; + import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDPage; import org.apache.pdfbox.pdmodel.PDPageTree; @@ -36,7 +32,12 @@ import org.apache.pdfbox.pdmodel.interactive.digitalsignature.visible.PDVisibleS import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.IOException; +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.pdfbox2.PDFBOXObject; +import at.knowcenter.wag.egov.egiz.pdf.PositioningInstruction; public class PDFAsVisualSignatureProperties extends PDVisibleSigProperties { @@ -56,9 +57,13 @@ public class PDFAsVisualSignatureProperties extends PDVisibleSigProperties { private String alternativeTableCaption=""; + private boolean buildPreviewOnly; + public PDFAsVisualSignatureProperties(ISettings settings, PDFBOXObject object, - PdfBoxVisualObject visObj, PositioningInstruction pos, SignatureProfileSettings signatureProfileSettings) { - this.settings = settings; + PdfBoxVisualObject visObj, PositioningInstruction pos, SignatureProfileSettings signatureProfileSettings, + boolean buildPreviewOnly) { + this.buildPreviewOnly = buildPreviewOnly; + this.settings = settings; this.signatureProfileSettings = signatureProfileSettings; try { main = visObj.getTable(); @@ -113,7 +118,7 @@ public class PDFAsVisualSignatureProperties extends PDVisibleSigProperties { PDFAsVisualSignatureBuilder builder = new PDFAsVisualSignatureBuilder(this, this.settings, designer, this.signatureProfileSettings); PDFAsTemplateCreator creator = new PDFAsTemplateCreator(builder); try { - setVisibleSignature(creator.buildPDF(designer, this.origDoc)); + setVisibleSignature(creator.buildPDF(designer, this.origDoc, buildPreviewOnly)); } catch (PdfAsException e) { throw new PdfAsWrappedIOException(e); } diff --git a/pdf-as-pdfbox-2/src/test/java/at/gv/egiz/pdfas/lib/testpdfbox/SignatureFieldsAndPlaceHolderExtractorTest.java b/pdf-as-pdfbox-2/src/test/java/at/gv/egiz/pdfas/lib/testpdfbox/SignatureFieldsAndPlaceHolderExtractorTest.java index 0d85c82b..40522504 100644 --- a/pdf-as-pdfbox-2/src/test/java/at/gv/egiz/pdfas/lib/testpdfbox/SignatureFieldsAndPlaceHolderExtractorTest.java +++ b/pdf-as-pdfbox-2/src/test/java/at/gv/egiz/pdfas/lib/testpdfbox/SignatureFieldsAndPlaceHolderExtractorTest.java @@ -1,16 +1,20 @@ package at.gv.egiz.pdfas.lib.testpdfbox; -import at.gv.egiz.pdfas.lib.impl.pdfbox2.placeholder.SignatureFieldsAndPlaceHolderExtractor; -import at.gv.egiz.pdfas.lib.impl.placeholder.SignaturePlaceholderData; -import org.apache.pdfbox.pdmodel.PDDocument; -import org.junit.Assert; -import org.junit.Test; - import java.io.File; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import org.apache.pdfbox.pdmodel.PDDocument; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +import at.gv.egiz.pdfas.lib.impl.pdfbox2.placeholder.SignatureFieldsAndPlaceHolderExtractor; +import at.gv.egiz.pdfas.lib.impl.placeholder.SignaturePlaceholderData; + +@RunWith(JUnit4.class) public class SignatureFieldsAndPlaceHolderExtractorTest { public String getPath(String resourceName) { -- cgit v1.2.3