public IVFile sign(final IVFile pdf) {
    IVFile outFile = null;
    OutputStream fout = null;
    try {
      outFile = new TempVFile(pdf.getName(), pdf.getVarName());
      fout = outFile.getOutputStream();

      // Verificar se � para rubricar todas as paginas
      if (LoadImageAction.getFlagRub()) {
        // Verificar se � para utilizar a mesma imagem na rubrica e na assinatura
        if (LoadImageAction.rubimgSameass) {
          String n = rubricarTodas(pdf);
          InputStream input = new FileInputStream(n);
          byte[] buffer = new byte[8192];
          int r;
          while ((r = input.read(buffer)) > 0) fout.write(buffer, 0, r);
          input.close();
          deleteFile(n);
        } else {
          String read = rubricarTodas(pdf);
          String write = copy(read);
          String signFile = hashSignExternalTimestamp(read, write);

          InputStream input = new FileInputStream(signFile);
          byte[] buffer = new byte[8192];
          int r;
          while ((r = input.read(buffer)) > 0) fout.write(buffer, 0, r);
          input.close();
          deleteFile(signFile);
        }
      } else {
        String signFile = hashSignExternalTimestamp(pdf);
        InputStream input = new FileInputStream(signFile);
        byte[] buffer = new byte[8192];
        int r;
        while ((r = input.read(buffer)) > 0) fout.write(buffer, 0, r);
        input.close();
        deleteFile(signFile);
      }

    } catch (Exception e) {
      log.error("Error signing data", e);
      outFile = null;
    } finally {
      try {
        if (fout != null) fout.close();
      } catch (IOException e) {
      }
    }

    return outFile;
  }
  public void insertImageRubrica(PdfReader reader, int pageCount, String fileWrite) {
    int iniY = 841 - 10;
    int iniX = 595 - 10;

    try {

      // Verificar qual � a imagem para utilizar na rubrica
      Image img = null;
      if (LoadImageAction.rubimgSameass) img = LoadImageAction.getAssImagePDF();
      else img = LoadImageAction.getRubImagePDF();

      // Criar Modelo para as Rubricas
      ByteArrayOutputStream out = new ByteArrayOutputStream();

      PdfStamper stp1 = new PdfStamper(reader, out, '\3', true);
      PdfFormField sig = PdfFormField.createSignature(stp1.getWriter());

      if (LoadImageAction.posRubSame) {

        int[] coord = LoadImageAction.getImageXY();

        if (!LoadImageAction.posMatriz) { // Se for por coordenadas do sample
          coord[0] = LoadImageAction.getAssX();
          coord[1] = LoadImageAction.getAssY();
        }

        sig.setWidget(
            new Rectangle(
                coord[0], coord[1], coord[0] + img.getWidth(), coord[1] + img.getHeight()),
            null);
      } else {
        sig.setWidget(
            new Rectangle(iniX - img.getWidth(), iniY - img.getHeight(), iniX, iniY), null);
      }

      sig.setFlags(PdfAnnotation.FLAGS_PRINT);
      sig.put(PdfName.DA, new PdfString("/Helv 0 Tf 0 g"));
      sig.setFieldName("Assinaturas");
      sig.setPage(1);

      // Se a imagem da rubrica n for a mesma da assinatura n�o mete na ultima pag
      if (!LoadImageAction.rubimgSameass) pageCount = pageCount - 1;

      // Inserir em todas as paginas o Modelo
      for (int i = 1; i <= pageCount; i++) stp1.addAnnotation(sig, i);

      stp1.close();

      // Guardar/Ler PDF com modelos inseridos
      reader = new PdfReader(out.toByteArray());
      File outputFile = new File(fileWrite);

      // Preencher Modelo com Dados
      PdfStamper stp = PdfStamper.createSignature(reader, null, '\0', outputFile, true);

      PdfSignatureAppearance sap = stp.getSignatureAppearance();
      sap.setAcro6Layers(true);
      reader.close();
      sap.setVisibleSignature("Assinaturas");
      sap.setLayer2Text("\n\n(Doc. assinado digitalmente)");
      sap.setImage(img);
      PdfSignature dic =
          new PdfSignature(
              PdfName.ADOBE_PPKLITE, new PdfName("adbe.pkcs7.detached")); // $NON-NLS-1$
      dic.setReason(sap.getReason());
      dic.setLocation(sap.getLocation());
      dic.setContact(sap.getContact());
      dic.setDate(new PdfDate(sap.getSignDate()));
      sap.setCryptoDictionary(dic);
      int contentEstimated = 15000;
      HashMap<Object, Object> exc = new HashMap<Object, Object>();
      exc.put(PdfName.CONTENTS, new Integer(contentEstimated * 2 + 2));
      sap.preClose(exc);

      Provider prov = entry.getProvider();
      PrivateKey key = entry.getPrivateKey();
      Certificate[] chain = entry.getCertificateChain();
      PdfPKCS7 sgn = new PdfPKCS7(key, chain, null, "SHA1", prov.getName(), false); // $NON-NLS-1$
      InputStream data = sap.getRangeStream();
      MessageDigest messageDigest = MessageDigest.getInstance("SHA1"); // $NON-NLS-1$
      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 (isUseOCSP() && chain.length >= 2) {
        String url = PdfPKCS7.getOCSPURL((X509Certificate) chain[0]);
        if (url != null && url.length() > 0)
          ocsp =
              new OcspClientBouncyCastle(
                      (X509Certificate) chain[0], (X509Certificate) chain[1], url)
                  .getEncoded();
      }
      byte sh[] = sgn.getAuthenticatedAttributeBytes(hash, cal, ocsp);
      sgn.update(sh, 0, sh.length);

      TSAClient tsc = null;
      if (isUseTSA() && tsaLocation != null) tsc = new TSAClientBouncyCastle(tsaLocation);
      byte[] encodedSig = sgn.getEncodedPKCS7(hash, cal, tsc, ocsp);

      if (contentEstimated + 2 < encodedSig.length)
        throw new Exception("Not enough space"); // $NON-NLS-1$

      byte[] paddedSig = new byte[contentEstimated];
      PdfDictionary dic2 = new PdfDictionary();
      System.arraycopy(encodedSig, 0, paddedSig, 0, encodedSig.length);
      dic2.put(PdfName.CONTENTS, new PdfString(paddedSig).setHexWriting(true));
      sap.close(dic2);

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  public String hashSignExternalTimestamp(String read, String write) throws Exception {
    Provider prov = entry.getProvider();
    PrivateKey key = entry.getPrivateKey();
    Certificate[] chain = entry.getCertificateChain();

    PdfReader reader = new PdfReader(read);
    int pageCount = reader.getNumberOfPages();

    File outputFile = new File(write);
    PdfStamper stp = PdfStamper.createSignature(reader, null, '\0', outputFile, true);

    PdfSignatureAppearance sap = stp.getSignatureAppearance();
    sap.setProvider(prov.getName());
    sap.setReason(getReason());
    sap.setLocation(getLocation());
    sap.setContact(getContact());

    sap.setCrypto(null, chain, null, PdfSignatureAppearance.SELF_SIGNED);

    int[] coord = LoadImageAction.getImageXY();

    if (!LoadImageAction.posMatriz) { // Se for por coordenadas do sample
      coord[0] = LoadImageAction.getAssX();
      coord[1] = LoadImageAction.getAssY();
    }

    // Adicionar imagem ao PDF se for para utilizar
    if (!isSignatureVisible()) {
      sap.setLayer2Text("");
    } else {
      if (LoadImageAction.getFlagPDF()) {
        sap.setAcro6Layers(true);
        Image img = LoadImageAction.getAssImagePDF();

        if (LoadImageAction.getPagToSign() == -1)
          sap.setVisibleSignature(
              new Rectangle(
                  coord[0], coord[1], coord[0] + img.getWidth(), coord[1] + img.getHeight()),
              pageCount,
              null);
        else
          sap.setVisibleSignature(
              new Rectangle(
                  coord[0], coord[1], coord[0] + img.getWidth(), coord[1] + img.getHeight()),
              LoadImageAction.getPagToSign(),
              null);

        sap.setLayer2Text("\n\n(Doc. assinado digitalmente)");
        sap.setImage(img);
      } else {
        if (LoadImageAction.getPagToSign() == -1)
          sap.setVisibleSignature(
              new Rectangle(coord[0], coord[1], coord[0] + 150, coord[1] + 40), pageCount, null);
        else
          sap.setVisibleSignature(
              new Rectangle(coord[0], coord[1], coord[0] + 150, coord[1] + 40),
              LoadImageAction.getPagToSign(),
              null);

        sap.setLayer2Text(getSignatureText((X509Certificate) chain[0], sap.getSignDate()));
      }
    }

    PdfSignature dic =
        new PdfSignature(PdfName.ADOBE_PPKLITE, new PdfName("adbe.pkcs7.detached")); // $NON-NLS-1$
    dic.setReason(sap.getReason());
    dic.setLocation(sap.getLocation());
    dic.setContact(sap.getContact());
    dic.setDate(new PdfDate(sap.getSignDate()));
    sap.setCryptoDictionary(dic);
    int contentEstimated = 15000;
    HashMap<Object, Object> exc = new HashMap<Object, Object>();
    exc.put(PdfName.CONTENTS, new Integer(contentEstimated * 2 + 2));
    sap.preClose(exc);

    PdfPKCS7 sgn = new PdfPKCS7(key, chain, null, "SHA1", prov.getName(), false);
    InputStream data = sap.getRangeStream();
    MessageDigest messageDigest = MessageDigest.getInstance("SHA1"); // $NON-NLS-1$
    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 (isUseOCSP() && chain.length >= 2) {
      String url = PdfPKCS7.getOCSPURL((X509Certificate) chain[0]);
      if (url != null && url.length() > 0)
        ocsp =
            new OcspClientBouncyCastle((X509Certificate) chain[0], (X509Certificate) chain[1], url)
                .getEncoded();
    }
    byte sh[] = sgn.getAuthenticatedAttributeBytes(hash, cal, ocsp);
    sgn.update(sh, 0, sh.length);
    TSAClient tsc = null;
    if (isUseTSA() && tsaLocation != null) tsc = new TSAClientBouncyCastle(tsaLocation);

    // o PIN/PASS dos certificados � pedido aqui
    byte[] encodedSig = sgn.getEncodedPKCS7(hash, cal, tsc, ocsp);

    if (contentEstimated + 2 < encodedSig.length)
      throw new Exception("Not enough space"); // $NON-NLS-1$

    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));
    sap.close(dic2);

    deleteFile(read);
    return write;
  }