Example usage for com.itextpdf.text.pdf AcroFields getFieldPositions

List of usage examples for com.itextpdf.text.pdf AcroFields getFieldPositions

Introduction

In this page you can find the example usage for com.itextpdf.text.pdf AcroFields getFieldPositions.

Prototype

public List<FieldPosition> getFieldPositions(String name) 

Source Link

Document

Gets the field box positions in the document.

Usage

From source file:book.pdftemplates.FillTemplateHelper.java

public FillTemplateHelper(String stationery) throws IOException, DocumentException {
    reader = new PdfReader(stationery);
    AcroFields fields = reader.getAcroFields();
    pageSize = reader.getPageSize(1);/*from   w  w  w .  jav  a  2s  . c  om*/
    body = fields.getFieldPositions("body").get(0).position;
    mLeft = body.getLeft() - pageSize.getLeft();
    mRight = pageSize.getRight() - body.getRight();
    mTop = pageSize.getTop() - body.getTop();
    mBottom = body.getBottom() - pageSize.getBottom();
    to = fields.getFieldPositions("to").get(0).position;
    from = fields.getFieldPositions("from").get(0).position;
    date = fields.getFieldPositions("date").get(0).position;
    footer = fields.getFieldPositions("footer").get(0).position;
    basefont = BaseFont.createFont();
    font = new Font(basefont, 12);
}

From source file:com.education.freelancer.MainActivity.java

private void setSignatureStamp(PdfStamper stamper, AcroFields acroFields, String signId) {
    String DIRECTORY = Environment.getExternalStorageDirectory().getPath() + "/DigitSign/";
    String StoredPath = DIRECTORY + signId + ".png";
    AcroFields.FieldPosition f = acroFields.getFieldPositions(signId).get(0);
    int page = f.page;
    Rectangle rect = f.position;//from  w w w .j a v a2  s  .c o  m
    File imgFile = new File(StoredPath);
    Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
    if (myBitmap == null) {
        return;
    }
    // myBitmap=getResizedBitmap(myBitmap,160);
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    myBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
    Image img = null;
    try {
        img = Image.getInstance(stream.toByteArray());
        img.scaleToFit(rect.getWidth(), rect.getHeight());
        img.setAbsolutePosition(rect.getLeft(), rect.getBottom());
        stamper.getOverContent(page).addImage(img);
        imgFile.delete();
    } catch (DocumentException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:controller.CCInstance.java

License:Open Source License

public final ArrayList<SignatureValidation> validatePDF(final String file, final ValidationListener vl)
        throws IOException, DocumentException, GeneralSecurityException {
    this.validating = true;

    final PdfReader reader = new PdfReader(file);
    final AcroFields af = reader.getAcroFields();
    final ArrayList names = af.getSignatureNames();
    final ArrayList<SignatureValidation> validateList = new ArrayList<>();
    X509Certificate x509c = null;

    Security.setProperty("ocsp.enable", "true");
    System.setProperty("com.sun.security.enableCRLDP", "true");

    boolean nextValid = true;

    for (Object o : names) {
        if (!validating) {
            return null;
        }//w  ww .  j ava 2s. c  om

        final String name = (String) o;
        final PdfPKCS7 pk = af.verifySignature(name, "BC");
        final Certificate pkc[] = pk.getCertificates();
        x509c = (X509Certificate) pkc[pkc.length - 1];

        final Certificate[] aL = pkc;//getCompleteCertificateChain(x509c);

        if (null == aL || 0 == aL.length) {
            return null;
        }

        CertificateStatus ocspCertificateStatus = CertificateStatus.UNCHECKED;

        BasicOCSPResp ocspResp = pk.getOcsp();
        if (null != ocspResp && pk.isRevocationValid()) {
            for (SingleResp singleResp : ocspResp.getResponses()) {
                if (null == singleResp.getCertStatus()) {
                    ocspCertificateStatus = CertificateStatus.OK;
                } else if (singleResp.getCertStatus() instanceof RevokedStatus) {
                    if (ocspResp.getProducedAt()
                            .before(((RevokedStatus) singleResp.getCertStatus()).getRevocationTime())) {
                        ocspCertificateStatus = CertificateStatus.OK;
                    } else {
                        ocspCertificateStatus = CertificateStatus.REVOKED;
                    }
                } else if (singleResp.getCertStatus() instanceof UnknownStatus) {
                    ocspCertificateStatus = CertificateStatus.UNKNOWN;
                }
            }
        }

        CertificateStatus crlCertificateStatus = CertificateStatus.UNCHECKED;
        Collection<CRL> crlResp = pk.getCRLs();
        if (null != crlResp) {
            boolean revoked = false;
            for (CRL crl : crlResp) {
                if (crl.isRevoked(x509c)) {
                    revoked = true;
                }
            }
            crlCertificateStatus = revoked ? CertificateStatus.REVOKED : CertificateStatus.OK;
        }

        if (ocspCertificateStatus.equals(CertificateStatus.UNCHECKED)
                && crlCertificateStatus.equals(CertificateStatus.UNCHECKED)) {
            if (pkc.length == 1) {
                Certificate[] completeChain = getCompleteTrustedCertificateChain(x509c);
                if (completeChain.length == 1) {
                    ocspCertificateStatus = CertificateStatus.UNCHAINED;
                } else {
                    ocspCertificateStatus = CertificateStatus.CHAINED_LOCALLY;
                }
            }
        }

        final TimeStampToken tst = pk.getTimeStampToken();
        boolean validTimestamp = false;
        if (null != tst) {
            final boolean hasTimestamp = pk.verifyTimestampImprint();
            validTimestamp = hasTimestamp && CertificateVerification.verifyTimestampCertificates(tst, ks, null);
        }

        PdfDictionary pdfDic = reader.getAcroFields().getSignatureDictionary(name);
        SignaturePermissions sp = new SignaturePermissions(pdfDic, null);

        boolean isValid;
        if (nextValid) {
            isValid = pk.verify();
        } else {
            isValid = false;
        }

        List<AcroFields.FieldPosition> posList = af.getFieldPositions(name);
        final SignatureValidation signature = new SignatureValidation(file, name, pk, !pk.verify(),
                af.signatureCoversWholeDocument(name), af.getRevision(name), af.getTotalRevisions(),
                reader.getCertificationLevel(), ocspCertificateStatus, crlCertificateStatus, validTimestamp,
                posList, sp, isValid);
        validateList.add(signature);

        if (null != vl) {
            vl.onValidationComplete(signature);
        }
        if (!sp.isFillInAllowed()) {
            nextValid = false;
        }
    }
    return validateList;
}

From source file:org.obiba.mica.PdfUtils.java

License:Open Source License

public static void addImage(byte[] input, OutputStream output, Image image, String placeholder)
        throws IOException, DocumentException {
    try (PdfReaderAutoclosable pdfReader = new PdfReaderAutoclosable(input);
            PdfStamperAutoclosable pdfStamper = new PdfStamperAutoclosable(pdfReader, output)) {
        AcroFields form = pdfStamper.getAcroFields();
        List<AcroFields.FieldPosition> positions = form.getFieldPositions(placeholder);

        positions.forEach(p -> {/* w w w .  j  ava 2 s.c  om*/
            image.scaleToFit(p.position.getWidth(), p.position.getHeight());
            image.setAbsolutePosition(
                    p.position.getLeft() + (p.position.getWidth() - image.getScaledWidth()) / 2,
                    p.position.getBottom() + (p.position.getHeight() - image.getScaledHeight()) / 2);
            PdfContentByte cb = pdfStamper.getOverContent(p.page);

            try {
                cb.addImage(image);
            } catch (DocumentException e) {
                throw Throwables.propagate(e);
            }
        });
    }
}