diff options
Diffstat (limited to 'pdf-as-pdfbox-2')
4 files changed, 59 insertions, 42 deletions
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 534c6c40..7942edcd 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 @@ -26,11 +26,14 @@ package at.gv.egiz.pdfas.lib.impl.pdfbox2.positioning; import java.awt.geom.AffineTransform; import java.awt.geom.Point2D; import java.io.IOException; +import java.util.ArrayList; +import java.util.List; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDPage; import org.apache.pdfbox.pdmodel.PDPageTree; import org.apache.pdfbox.pdmodel.common.PDRectangle; +import org.apache.pdfbox.pdmodel.interactive.form.PDSignatureField; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -159,27 +162,44 @@ public class Positioning { public static PositioningInstruction adjustSignatureTableandCalculatePosition( final PDDocument pdfDataSource, IPDFVisualObject pdf_table, TablePos pos, ISettings settings) throws PdfAsException { - + List<PDSignatureField> pdSignatureFieldList = new ArrayList<>(); PdfBoxUtils.checkPDFPermissions(pdfDataSource); - // get pages of currentdocument + int counter = 0; + try { + //count signature fields with signatures + pdSignatureFieldList = pdfDataSource.getSignatureFields(); + for (PDSignatureField signatureField : pdSignatureFieldList) + { + if(signatureField.getSignature()!=null){ + counter++; + } + } + } catch (IOException e) { + e.printStackTrace(); + } + // get pages of currentdocument int doc_pages = pdfDataSource.getNumberOfPages(); int page = doc_pages; boolean make_new_page = pos.isNewPage(); + + //we cannot add new page if a document is already signed + + if (!(pos.isNewPage() || pos.isPauto())) { // we should posit signaturtable on this page - page = pos.getPage(); // System.out.println("XXXXPAGE="+page+" doc_pages="+doc_pages); - if (page > doc_pages) { - make_new_page = true; - page = doc_pages; - - } - + if (page > doc_pages ) { + make_new_page = true; + page = doc_pages; + } } - + if(make_new_page && counter!=0) + { + make_new_page = false; + } PDPage pdPage = pdfDataSource.getPage(page-1); @@ -259,7 +279,7 @@ public class Positioning { // pos_y is auto if (make_new_page) { // ignore footer in new page - page++; + page++; pos_y = page_height - SIGNATURE_MARGIN_VERTICAL; return new PositioningInstruction(make_new_page, page, pos_x, pos_y, pos.rotation); @@ -287,12 +307,16 @@ public class Positioning { // no text --> SIGNATURE_BORDER pos_y = page_height - SIGNATURE_MARGIN_VERTICAL; if (pos_y - footer_line <= table_height) { - make_new_page = true; + if(counter!=0) + make_new_page = false; + else{ + make_new_page = true; + page++; + } if (!pos.isPauto()) { // we have to correct pagenumber page = pdfDataSource.getNumberOfPages(); } - page++; // no text --> SIGNATURE_BORDER pos_y = page_height - SIGNATURE_MARGIN_VERTICAL; } @@ -303,20 +327,23 @@ public class Positioning { // we do have text take SIGNATURE_MARGIN pos_y = page_height - page_length - SIGNATURE_MARGIN_VERTICAL; if (pos_y - footer_line <= table_height) { - make_new_page = true; + ///TODO: new page should not be created when signature exists + if(counter!=0) + make_new_page = false; + else{ + make_new_page = true; + page++; + } if (!pos.isPauto()) { // we have to correct pagenumber in case of absolute page and // not enough // space page = pdfDataSource.getNumberOfPages(); } - page++; // no text --> SIGNATURE_BORDER pos_y = page_height - SIGNATURE_MARGIN_VERTICAL; } return new PositioningInstruction(make_new_page, page, pos_x, pos_y, pos.rotation); - } - } diff --git a/pdf-as-pdfbox-2/src/main/java/at/gv/egiz/pdfas/lib/impl/pdfbox2/utils/PdfBoxUtils.java b/pdf-as-pdfbox-2/src/main/java/at/gv/egiz/pdfas/lib/impl/pdfbox2/utils/PdfBoxUtils.java index 85073568..1a6499ac 100644 --- a/pdf-as-pdfbox-2/src/main/java/at/gv/egiz/pdfas/lib/impl/pdfbox2/utils/PdfBoxUtils.java +++ b/pdf-as-pdfbox-2/src/main/java/at/gv/egiz/pdfas/lib/impl/pdfbox2/utils/PdfBoxUtils.java @@ -64,9 +64,7 @@ public class PdfBoxUtils { } count++; - logger.debug("Returning sig number: " + count); - return count; } 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 c37002c4..c99bb608 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 @@ -132,6 +132,9 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { try { doc = pdfObject.getDocument(); + //if signature already exists dont create new page + List<PDSignatureField> pdSignatureFieldList = doc.getSignatureFields(); + SignaturePlaceholderData signaturePlaceholderData = PlaceholderFilter .checkPlaceholderSignature(pdfObject.getStatus(), pdfObject.getStatus().getSettings()); @@ -172,14 +175,12 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { String sigFieldName = pdfObject.getStatus().getSettings().getValue(SIGNATURE_FIELD_NAME); signature = findExistingSignature(doc, sigFieldName); //signature = findExistingSignature(doc, "ownerSignature"); - if (signature == null) - { + if (signature == null) { // create signature dictionary signature = new PDSignature(); } - else - { + else { isAdobeSigForm = true; } @@ -304,8 +305,9 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { logger.debug("Positioning: {}", positioningInstruction.toString()); - if(!isAdobeSigForm){ + if(!isAdobeSigForm) { if (positioningInstruction.isMakeNewPage()) { + int last = doc.getNumberOfPages() - 1; PDDocumentCatalog root = doc.getDocumentCatalog(); PDPage lastPage = root.getPages().get(last); @@ -318,12 +320,13 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { // handle rotated page int targetPageNumber = positioningInstruction.getPage(); + logger.debug("Target Page: " + targetPageNumber); PDPage targetPage = doc.getPages().get(targetPageNumber - 1); int rot = targetPage.getRotation(); logger.debug("Page rotation: " + rot); // positioningInstruction.setRotation(positioningInstruction.getRotation() - // + rot); + // // + rot); logger.debug("resulting Sign rotation: " + positioningInstruction.getRotation()); SignaturePositionImpl position = new SignaturePositionImpl(); @@ -349,14 +352,10 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { */ if (signaturePlaceholderData != null) { - // Placeholder found! - // replace placeholder - InputStream fis = PADESPDFBOXSigner.class.getResourceAsStream("/placeholder/empty.jpg"); PDImageXObject img = JPEGFactory.createFromStream(doc, fis); - img.getCOSObject().setNeedToBeUpdated(true); // PDDocumentCatalog root = doc.getDocumentCatalog(); // PDPageNode rootPages = root.getPages(); @@ -414,12 +413,11 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { doc.addSignature(signature, signer, options); - //String sigFieldName = "ownerSignature"; - if (sigFieldName == null) { sigFieldName = "PDF-AS Signatur"; } + int count = PdfBoxUtils.countSignatures(doc, sigFieldName); sigFieldName = sigFieldName + count; @@ -434,7 +432,7 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { // pdstRoot); //this is not used for Adobe signature fields - if(!isAdobeSigForm){ + if(!isAdobeSigForm) { PDSignatureField signatureField = null; if (acroFormm != null) { @SuppressWarnings("unchecked") @@ -474,7 +472,6 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { } } - PDSignatureField signatureField = null; PDAcroForm acroForm = doc.getDocumentCatalog().getAcroForm(); if (acroForm != null) { @@ -646,7 +643,7 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { } catch (IOException e) { logger.warn(MessageResolver.resolveMessage("error.pdf.sig.01"), e); throw new PdfAsException("error.pdf.sig.01", e); - } finally { + } finally { if (doc != null) { try { doc.close(); @@ -708,7 +705,7 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { throw new PdfAsException("Failed validating PDF Document RuntimeException."); } finally { if (document != null) { - IOUtils.closeQuietly((Closeable) document); + IOUtils.closeQuietly(document); } } } @@ -752,11 +749,10 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { public Image generateVisibleSignaturePreview(SignParameter parameter, java.security.cert.X509Certificate cert, int resolution, OperationStatus status, RequestedSignature requestedSignature) throws PDFASError { try { - PDFBOXObject pdfObject = (PDFBOXObject) status.getPdfObject(); + PDFBOXObject pdfObject = (PDFBOXObject) status.getPdfObject(); PDDocument origDoc = new PDDocument(); - origDoc.addPage(new PDPage(PDRectangle.A4)); ByteArrayOutputStream baos = new ByteArrayOutputStream(); origDoc.save(baos); @@ -779,7 +775,7 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { .getSignatureProfileConfiguration(requestedSignature.getSignatureProfileID()); String signaturePosString = signatureProfileConfiguration.getDefaultPositioning(); - PositioningInstruction positioningInstruction = null; + PositioningInstruction positioningInstruction; if (signaturePosString != null) { positioningInstruction = Positioning.determineTablePositioning(new TablePos(signaturePosString), "", origDoc, visualObject, pdfObject.getStatus().getSettings()); @@ -886,9 +882,6 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { if (signature == null) { signature = new PDSignature(); - // after solving PDFBOX-3524 - // signatureField.setValue(signature) - // until then: signatureField.getCOSObject().setItem(COSName.V, signature); } else diff --git a/pdf-as-pdfbox-2/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox2/PDFAsVisualSignatureBuilder.java b/pdf-as-pdfbox-2/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox2/PDFAsVisualSignatureBuilder.java index d283c3e3..b797fec8 100644 --- a/pdf-as-pdfbox-2/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox2/PDFAsVisualSignatureBuilder.java +++ b/pdf-as-pdfbox-2/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox2/PDFAsVisualSignatureBuilder.java @@ -657,7 +657,6 @@ public class PDFAsVisualSignatureBuilder extends PDVisibleSigBuilder implements public void removeCidSet(PDDocument document) throws IOException { PDDocumentCatalog catalog = document.getDocumentCatalog(); - COSName cidSet = COSName.getPDFName("CIDSet"); Iterator<PDPage> pdPageIterator = catalog.getPages().iterator(); |