Package com.lowagie.text.pdf

Examples of com.lowagie.text.pdf.PdfSignatureAppearance$RangeStream


        //createSignature(PdfReader reader, OutputStream os, char pdfVersion)
        //pdfVersion - the new pdf version or '\0' to keep the same version as
        // the original document

        PdfStamper stp = PdfStamper.createSignature(reader, fout, '\0');
        PdfSignatureAppearance sap = stp.getSignatureAppearance();

       
        //setCrypto(PrivateKey privKey, Certificate[] certChain, CRL[] crlList, PdfName filter)
        // CRL - certificate revocation lists (CRLs) that have different formats but important common uses.
        //     For example, all CRLs share the functionality of listing revoked certificates, and can be queried on whether or not they list a given certificate.
        // PdfName
        // SELF_SIGNED      -   The self signed filter
        // VERISIGN_SIGNED  -   The VeriSign filter
        // WINCER_SIGNED    -   The Windows Certificate Security
        sap.setCrypto(privateKey, chain, null,PdfSignatureAppearance.SELF_SIGNED);
        //sap.setCrypto(privateKey, chain, null,PdfSignatureAppearance.WINCER_SIGNED);

        sap.setReason(SOSPDFSignatur.reason);
        sap.setContact(SOSPDFSignatur.contact);
        sap.setLocation(SOSPDFSignatur.location);

        //GregorianCalendar cal = new GregorianCalendar();
        //sap.setSignDate(cal);

        //             comment next line to have an invisible signature
View Full Code Here


            | (options.isRightModifyAnnotations() ? PdfWriter.ALLOW_MODIFY_ANNOTATIONS : 0)
            | (options.isRightModifyContents() ? PdfWriter.ALLOW_MODIFY_CONTENTS : 0);
        stp.setEncryption(true, options.getPdfUserPwdStr(), options.getPdfOwnerPwdStr(), tmpRight);
      }

      final PdfSignatureAppearance sap = stp.getSignatureAppearance();
      sap.setCrypto(key, chain, null, PdfSignatureAppearance.WINCER_SIGNED);
      if (!StringUtils.isEmpty(options.getReason())) {
        options.log("console.setReason", options.getReason());
        sap.setReason(options.getReason());
      }
      if (!StringUtils.isEmpty(options.getLocation())) {
        options.log("console.setLocation", options.getLocation());
        sap.setLocation(options.getLocation());
      }
      if (!StringUtils.isEmpty(options.getContact())) {
        options.log("console.setContact", options.getContact());
        sap.setContact(options.getContact());
      }
      options.log("console.setCertificationLevel");
      sap.setCertificationLevel(options.getCertLevelX().getLevel());

      if (options.isVisible()) {
        // visible signature is enabled
        options.log("console.configureVisible");
        options.log("console.setAcro6Layers");
        sap.setAcro6Layers(true);

        final String tmpImgPath = options.getImgPath();
        if (tmpImgPath != null) {
          options.log("console.createImage", tmpImgPath);
          final Image img = Image.getInstance(tmpImgPath);
          options.log("console.setSignatureGraphic");
          sap.setSignatureGraphic(img);
        }
        final String tmpBgImgPath = options.getBgImgPath();
        if (tmpBgImgPath != null) {
          options.log("console.createImage", tmpBgImgPath);
          final Image img = Image.getInstance(tmpBgImgPath);
          options.log("console.setImage");
          sap.setImage(img);
        }
        options.log("console.setImageScale");
        sap.setImageScale(options.getBgImgScale());
        options.log("console.setL2Text");
        if (options.getL2Text() != null) {
          sap.setLayer2Text(options.getL2Text());
        } else {
          final StringBuilder buf = new StringBuilder();
          buf.append(res.get("default.l2text.signedBy")).append(" ");
          buf.append(PdfPKCS7.getSubjectFields((X509Certificate) chain[0]).getField("CN")).append('\n');
          final SimpleDateFormat sd = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss z");
          buf.append(res.get("default.l2text.date")).append(" ").append(
              sd.format(sap.getSignDate().getTime()));
          if (StringUtils.hasLength(options.getReason()))
            buf.append('\n').append(res.get("default.l2text.reason")).append(" ").append(
                options.getReason());
          if (StringUtils.hasLength(options.getLocation()))
            buf.append('\n').append(res.get("default.l2text.location")).append(" ").append(
                options.getLocation());
          sap.setLayer2Text(buf.toString());
          ;
        }
        if (FontUtils.getL2BaseFont() != null) {
          sap.setLayer2Font(new Font(FontUtils.getL2BaseFont(), options.getL2TextFontSize()));
        }
        options.log("console.setL4Text");
        sap.setLayer4Text(options.getL4Text());
        options.log("console.setRender");
        sap.setRender(options.getRenderMode().getRender());
        options.log("console.setVisibleSignature");
        sap.setVisibleSignature(new Rectangle(options.getPositionLLX(), options.getPositionLLY(), options
            .getPositionURX(), options.getPositionURY()), options.getPage(), null);
      }

      options.log("console.processing");
      final PdfSignature dic = new PdfSignature(PdfName.ADOBE_PPKLITE, new PdfName("adbe.pkcs7.detached"));
      dic.setReason(sap.getReason());
      dic.setLocation(sap.getLocation());
      dic.setContact(sap.getContact());
      dic.setDate(new PdfDate(sap.getSignDate()));
      sap.setCryptoDictionary(dic);

      final Proxy tmpProxy = options.createProxy();

      final CRLInfo crlInfo = new CRLInfo(options, chain);

      // CRLs are stored twice in PDF c.f.
      // PdfPKCS7.getAuthenticatedAttributeBytes
      final int contentEstimated = (int) (Constants.DEFVAL_SIG_SIZE + 2L * crlInfo.getByteCount());
      HashMap exc = new HashMap();
      exc.put(PdfName.CONTENTS, new Integer(contentEstimated * 2 + 2));
      sap.preClose(exc);

      PdfPKCS7 sgn = new PdfPKCS7(key, chain, crlInfo.getCrls(), hashAlgorithm.getAlgorithmName(), null, false);
      InputStream data = sap.getRangeStream();
      final MessageDigest messageDigest = MessageDigest.getInstance(hashAlgorithm.getAlgorithmName());
      byte buf[] = new byte[8192];
      int n;
      while ((n = data.read(buf)) > 0) {
        messageDigest.update(buf, 0, n);
      }
      byte hash[] = messageDigest.digest();
      Calendar cal = Calendar.getInstance();
      byte[] ocsp = null;
      if (options.isOcspEnabledX() && chain.length >= 2) {
        options.log("console.getOCSPURL");
        String url = PdfPKCS7.getOCSPURL((X509Certificate) chain[0]);
        if (url != null && url.length() > 0) {
          options.log("console.readingOCSP");
          final OcspClientBouncyCastle ocspClient = new OcspClientBouncyCastle((X509Certificate) chain[0],
              (X509Certificate) chain[1], url);
          ocspClient.setProxy(tmpProxy);
          ocsp = ocspClient.getEncoded();
        }
      }
      byte sh[] = sgn.getAuthenticatedAttributeBytes(hash, cal, ocsp);
      sgn.update(sh, 0, sh.length);

      TSAClientBouncyCastle tsc = null;
      if (options.isTimestampX() && !StringUtils.isEmpty(options.getTsaUrl())) {
        options.log("console.creatingTsaClient");
        tsc = new TSAClientBouncyCastle(options.getTsaUrl(), StringUtils.emptyNull(options.getTsaUser()),
            StringUtils.emptyNull(options.getTsaPasswd()));
        tsc.setProxy(tmpProxy);
        final String policyOid = options.getTsaPolicy();
        if (StringUtils.hasLength(policyOid)) {
          options.log("console.settingTsaPolicy", policyOid);
          tsc.setPolicy(policyOid);
        }
      }
      byte[] encodedSig = sgn.getEncodedPKCS7(hash, cal, tsc, ocsp);

      if (contentEstimated + 2 < encodedSig.length) {
        System.err.println("SigSize - contentEstimated=" + contentEstimated + ", sigLen=" + encodedSig.length);
        throw new Exception("Not enough space");
      }

      byte[] paddedSig = new byte[contentEstimated];
      System.arraycopy(encodedSig, 0, paddedSig, 0, encodedSig.length);

      PdfDictionary dic2 = new PdfDictionary();
      dic2.put(PdfName.CONTENTS, new PdfString(paddedSig).setHexWriting(true));
      options.log("console.closeStream");
      sap.close(dic2);
      fout.close();

    } catch (Exception e) {
      options.log("console.exception");
      e.printStackTrace(options.getPrintWriter());
View Full Code Here

            PdfReader reader = new PdfReader(originalBytes);
            ByteArrayOutputStream os = new ByteArrayOutputStream();                

            PdfStamper stamper = PdfStamper.createSignature(reader, os, '\0');
            PdfSignatureAppearance appearance = stamper.getSignatureAppearance();
            appearance.setCrypto(key, chain, null,
                                 PdfSignatureAppearance.SELF_SIGNED);
           
            appearance.setReason(reason);
            appearance.setLocation(location);
           
            appearance.setVisibleSignature(field);
            stamper.close();     
           
            return os.toByteArray();
        } catch (Exception e) {
            throw new RuntimeException(e);
View Full Code Here

            PdfReader reader = new PdfReader(originalBytes);
            ByteArrayOutputStream os = new ByteArrayOutputStream();                

            PdfStamper stamper = PdfStamper.createSignature(reader, os, '\0');
            PdfSignatureAppearance appearance = stamper.getSignatureAppearance();
            appearance.setCrypto(key, chain, null,
                                 PdfSignatureAppearance.SELF_SIGNED);
           
            appearance.setReason(reason);
            appearance.setLocation(location);
           
            appearance.setVisibleSignature(field);
            stamper.close();     
           
            return os.toByteArray();
        } catch (Exception e) {
            throw new RuntimeException(e);
View Full Code Here

         PdfReader reader = new PdfReader(originalBytes);
         ByteArrayOutputStream os = new ByteArrayOutputStream();

         PdfStamper stamper = PdfStamper.createSignature(reader, os, '\0');
         PdfSignatureAppearance appearance = stamper.getSignatureAppearance();
         appearance.setCrypto(key, chain, null, PdfSignatureAppearance.SELF_SIGNED);

         appearance.setReason(reason);
         appearance.setLocation(location);

         appearance.setVisibleSignature(field);
         stamper.close();

         return os.toByteArray();
      } catch (Exception e) {
         throw new RuntimeException(e);
View Full Code Here

TOP

Related Classes of com.lowagie.text.pdf.PdfSignatureAppearance$RangeStream

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.