From fa05e97f3e4343694041be24e8ff779a76cd36f6 Mon Sep 17 00:00:00 2001 From: ferbas Date: Wed, 2 Dec 2009 16:47:24 +0000 Subject: added adobe sig attribute git-svn-id: https://joinup.ec.europa.eu/svn/pdf-as/trunk@474 7b5415b0-85f9-ee4d-85bd-d5d0c3b42d1c --- .../wag/egov/egiz/pdf/BinarySignature.java | 80 +++++++++++++++++++++- 1 file changed, 77 insertions(+), 3 deletions(-) (limited to 'src/main/java/at/knowcenter') diff --git a/src/main/java/at/knowcenter/wag/egov/egiz/pdf/BinarySignature.java b/src/main/java/at/knowcenter/wag/egov/egiz/pdf/BinarySignature.java index d0cd951..f07d8c6 100644 --- a/src/main/java/at/knowcenter/wag/egov/egiz/pdf/BinarySignature.java +++ b/src/main/java/at/knowcenter/wag/egov/egiz/pdf/BinarySignature.java @@ -28,9 +28,13 @@ import java.security.cert.X509Certificate; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; +import java.util.HashMap; import java.util.Iterator; import java.util.List; +import javax.xml.datatype.DatatypeFactory; +import javax.xml.datatype.XMLGregorianCalendar; + import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -40,6 +44,7 @@ import at.gv.egiz.pdfas.exceptions.pdf.CaptionNotFoundException; import at.gv.egiz.pdfas.exceptions.pdf.KZSettingNotFoundException; import at.gv.egiz.pdfas.framework.input.PdfDataSource; import at.gv.egiz.pdfas.framework.output.DataSink; +import at.gv.egiz.pdfas.framework.signator.SignatorInformation; import at.knowcenter.wag.egov.egiz.cfg.SettingsReader; import at.knowcenter.wag.egov.egiz.exceptions.PDFDocumentException; import at.knowcenter.wag.egov.egiz.exceptions.PlaceholderException; @@ -58,6 +63,7 @@ import com.lowagie.text.Rectangle; import com.lowagie.text.pdf.PRStream; import com.lowagie.text.pdf.PdfArray; import com.lowagie.text.pdf.PdfContentByte; +import com.lowagie.text.pdf.PdfDate; import com.lowagie.text.pdf.PdfDictionary; import com.lowagie.text.pdf.PdfIndirectObject; import com.lowagie.text.pdf.PdfIndirectReference; @@ -66,6 +72,8 @@ import com.lowagie.text.pdf.PdfNumber; import com.lowagie.text.pdf.PdfObject; import com.lowagie.text.pdf.PdfPTable; import com.lowagie.text.pdf.PdfReader; +import com.lowagie.text.pdf.PdfSignature; +import com.lowagie.text.pdf.PdfSignatureAppearance; import com.lowagie.text.pdf.PdfStamper; import com.lowagie.text.pdf.PdfStamperImp; import com.lowagie.text.pdf.PdfString; @@ -775,7 +783,7 @@ public abstract class BinarySignature * Forwarded exception. */ public static IncrementalUpdateInformation writeIncrementalUpdate(PdfDataSource original_document, DataSink written_pdf, PdfPTable pdf_table, String profile, PositioningInstruction pi, - List variable_field_definitions, List all_field_definitions, List invisible_field_definitions, String invisibleKZString, TimeStamper timeStamper) throws PresentableException + List variable_field_definitions, List all_field_definitions, List invisible_field_definitions, String invisibleKZString, TimeStamper timeStamper, SignatorInformation si) throws PresentableException { try { @@ -800,7 +808,8 @@ public abstract class BinarySignature // IMPORTANT: append the new content to the original document using // incremental updated // The stamper allows this by setting append = true - PdfStamper stamper = new PdfStamper(reader, baos, '\0', true); + //PdfStamper stamper = new PdfStamper(reader, baos, '\0', true); + PdfStamper stamper = PdfStamper.createSignature(reader, baos, '\0', null, true); if (pi.isMakeNewPage()) { @@ -895,7 +904,8 @@ public abstract class BinarySignature createEgizDict(stamper, table_template, iui, variable_field_definitions, all_field_definitions, invisible_field_definitions, invisibleKZString); } - stamper.close(); + //stamper.close(); + createAdobeSigAttrib(stamper, si, atp); // here..., stamper is closed // System.out.println("wprinz: STAMPING FINISHED"); // just to make sure the stream is really closed @@ -916,6 +926,70 @@ public abstract class BinarySignature } } + /** + * Schreibt Adobe-pdf signatur attribut mittels itext + * @param stamper + * @param si + * @param atp + */ + public static void createAdobeSigAttrib(PdfStamper stamper, SignatorInformation si, ActualTablePos atp) { + + try { + logger.info("create adobe signature field"); + PdfSignatureAppearance sap = stamper.getSignatureAppearance(); + + sap.setCrypto(null, null, null, null); + // supress overlay text for visible signatures + sap.setLayer2Text(""); + sap.setLayer4Text(""); + + // the following line marks the sig block as adobe sig + //sap.setVisibleSignature(createRectangleFromTablePos(iui.actualTablePos), iui.actualTablePos.page, "PDF-AS-Signatur"); + sap.setVisibleSignature(new Rectangle(0, 0, 0, 0), atp.page, "PDF-AS-Signatur"); + String subfilter = "urn:pdfsigfilter:bka.gv.at:binaer:v1.1.0"; + if (si != null) { + subfilter = si.getSignSignatureObject().kz; + } + PdfSignature sig = new PdfSignature(new PdfName("Adobe.PDF-AS"), new PdfName(subfilter)); + // the following fields are not shown by the reader, because its is no Standard filter + //sig.setLocation("location is not visible"); + //sig.setReason("reason is not visible"); + //sig.setContact("contact is not visible"); + sig.setDate(new PdfDate()); + if (si != null) { + XMLGregorianCalendar c = DatatypeFactory.newInstance().newXMLGregorianCalendar(si.getSignSignatureObject().date); + sig.setDate(new PdfDate(c.toGregorianCalendar())); + } + String cn = null; + if (si != null) { + cn = si.getSignSignatureObject().certSubjectDNPart("CN"); + } + if (cn == null) { + cn = "PDF-AS"; + } + sig.setName(cn); + sap.setCryptoDictionary(sig); + + // content element is mandatory but empty + HashMap exc = new HashMap(); + exc.put(PdfName.CONTENTS, new Integer(2)); + sap.preClose(exc); // *2+2 + + PdfDictionary dic = new PdfDictionary(); + dic.put(PdfName.CONTENTS, new PdfString((String)null).setHexWriting(true)); + sap.close(dic); + } catch (Exception ex) { + logger.error("error", ex); + throw new RuntimeException(ex ); + } + + //stamper.close(); + } + + private static Rectangle createRectangleFromTablePos(ActualTablePos pos) { + return new Rectangle(pos.x, pos.y, pos.x + pos.width, pos.y - pos.height); + } + /** * Creates the EGIZ Dictionary and adds it to the document. * -- cgit v1.2.3