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

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

Introduction

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

Prototype

int FIELD_TYPE_SIGNATURE

To view the source code for com.itextpdf.text.pdf AcroFields FIELD_TYPE_SIGNATURE.

Click Source Link

Document

A field type.

Usage

From source file:my.charpdf.DandDcharPDFUI.java

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

    String inputFile;/*from w w w . ja  v  a 2  s  . c  o  m*/
    inputFile = jTextField1.getText().replace("\n", "").replace("\r", "");
    String pcName = "";
    String pcRace = "";
    String pcAlign = "";
    String pcBackG = "";
    String pcExp = "";
    String pcProfBonus = "";
    String pcStrScore = "";
    String pcStrMod = "";
    String pcDexScore = "";
    String pcDexMod = "";
    String pcConScore = "";
    String pcConMod = "";
    String pcIntScore = "";
    String pcIntMod = "";
    String pcWisScore = "";
    String pcWisMod = "";
    String pcChaScore = "";
    String pcChaMod = "";
    String pcClassLevel = "";
    String pcPerc = "";
    if (!inputFile.equals("")) {
        try {
            Builder parser = new Builder();
            Document doc = parser.build("file:///" + inputFile);

            Element root = doc.getRootElement();
            Elements character = root.getChildElements();

            pcName = character.get(0).getFirstChildElement("name").getValue();
            pcRace = character.get(0).getFirstChildElement("race").getValue();
            pcExp = character.get(0).getFirstChildElement("exp").getValue();
            pcAlign = character.get(0).getFirstChildElement("alignment").getValue();
            pcBackG = character.get(0).getFirstChildElement("background").getValue();
            pcProfBonus = character.get(0).getFirstChildElement("profbonus").getValue();
            pcPerc = character.get(0).getFirstChildElement("perception").getValue();

            //Integer numChildren = character.get(0).getChildCount();
            //System.out.println(numChildren);

            Elements pcAttrs = character.get(0).getChildElements("abilities").get(0).getChildElements();
            Elements pcClasses = character.get(0).getChildElements("classes").get(0).getChildElements();

            for (int i = 0; i < pcAttrs.size(); i++) {
                if (pcAttrs.get(i).getLocalName().equals("strength")) {
                    pcStrScore = pcAttrs.get(i).getChildElements("score").get(0).getValue();
                    pcStrMod = pcAttrs.get(i).getChildElements("bonus").get(0).getValue();
                } else if (pcAttrs.get(i).getLocalName().equals("dexterity")) {
                    pcDexScore = pcAttrs.get(i).getChildElements("score").get(0).getValue();
                    pcDexMod = pcAttrs.get(i).getChildElements("bonus").get(0).getValue();
                } else if (pcAttrs.get(i).getLocalName().equals("constitution")) {
                    pcConScore = pcAttrs.get(i).getChildElements("score").get(0).getValue();
                    pcConMod = pcAttrs.get(i).getChildElements("bonus").get(0).getValue();
                } else if (pcAttrs.get(i).getLocalName().equals("intelligence")) {
                    pcIntScore = pcAttrs.get(i).getChildElements("score").get(0).getValue();
                    pcIntMod = pcAttrs.get(i).getChildElements("bonus").get(0).getValue();
                } else if (pcAttrs.get(i).getLocalName().equals("wisdom")) {
                    pcWisScore = pcAttrs.get(i).getChildElements("score").get(0).getValue();
                    pcWisMod = pcAttrs.get(i).getChildElements("bonus").get(0).getValue();
                } else if (pcAttrs.get(i).getLocalName().equals("charisma")) {
                    pcChaScore = pcAttrs.get(i).getChildElements("score").get(0).getValue();
                    pcChaMod = pcAttrs.get(i).getChildElements("bonus").get(0).getValue();
                }
            }

            for (int i = 0; i < pcClasses.size(); i++) {
                // Gets the list of classes
                //System.out.println(pcClasses.get(i).getLocalName());
                String tempClass = pcClasses.get(i).getChildElements("name").get(0).getValue();
                String tempLevel = pcClasses.get(i).getChildElements("level").get(0).getValue();
                pcClassLevel += tempClass + " " + tempLevel + " / ";
            }
            pcClassLevel = pcClassLevel.substring(0, pcClassLevel.length() - 2);

            //for(i = 0; i < numClasses; i++) {
            //    System.out.println(charac);
            //}

            String inputTemplate = "resources/DandD5e-template.pdf";

            String outputPDF = "resources/" + pcName + ".pdf";

            PdfReader reader = new PdfReader(inputTemplate);
            PdfStamper stamper;
            stamper = new PdfStamper(reader, new FileOutputStream(outputPDF));
            AcroFields form = reader.getAcroFields();

            Set<String> fields = form.getFields().keySet();

            for (String key : fields) {
                //System.out.println(key);
                switch (form.getFieldType(key)) {
                case AcroFields.FIELD_TYPE_CHECKBOX:
                    //System.out.println(key + ": Checkbox");
                    break;
                case AcroFields.FIELD_TYPE_COMBO:
                    //System.out.println(key + ": Combo");
                    break;
                case AcroFields.FIELD_TYPE_LIST:
                    //System.out.println(key + ": List");
                    break;
                case AcroFields.FIELD_TYPE_NONE:
                    //System.out.println(key + ": None");
                    break;
                case AcroFields.FIELD_TYPE_PUSHBUTTON:
                    //System.out.println(key + ": Pushbutton");
                    break;
                case AcroFields.FIELD_TYPE_RADIOBUTTON:
                    //System.out.println(key + ": Radio");
                    break;
                case AcroFields.FIELD_TYPE_SIGNATURE:
                    //System.out.println(key + ": Signature");
                    break;
                case AcroFields.FIELD_TYPE_TEXT:
                    //System.out.println(key + ": Text");
                    break;
                default:
                    //System.out.println(key + ": ???");
                }
            }
            stamper.getAcroFields().setField("Race ", pcRace);
            stamper.getAcroFields().setField("CharacterName", pcName);
            stamper.getAcroFields().setField("XP", pcExp);
            stamper.getAcroFields().setField("Alignment", pcAlign);
            stamper.getAcroFields().setField("Background", pcBackG);
            int tempPB = Integer.parseInt(pcProfBonus);
            if (tempPB > 0) {
                pcProfBonus = "+" + pcProfBonus;
            }
            stamper.getAcroFields().setField("ProfBonus", pcProfBonus);

            //Attributes
            stamper.getAcroFields().setField("STR", pcStrScore);
            stamper.getAcroFields().setField("STRmod", pcStrMod);
            stamper.getAcroFields().setField("DEX", pcDexScore);
            stamper.getAcroFields().setField("DEXmod ", pcDexMod);
            stamper.getAcroFields().setField("CON", pcConScore);
            stamper.getAcroFields().setField("CONmod", pcConMod);
            stamper.getAcroFields().setField("INT", pcIntScore);
            stamper.getAcroFields().setField("INTmod", pcIntMod);
            stamper.getAcroFields().setField("WIS", pcWisScore);
            stamper.getAcroFields().setField("WISmod", pcWisMod);
            stamper.getAcroFields().setField("CHA", pcChaScore);
            stamper.getAcroFields().setField("CHamod", pcChaMod);

            stamper.getAcroFields().setField("ClassLevel", pcClassLevel);
            stamper.getAcroFields().setField("Passive", pcPerc);

            stamper.close();
            reader.close();
        } catch (java.io.IOException | DocumentException e) {
            System.err.println("1st Catch, that didn't go well: " + e.getMessage());
        } catch (ParsingException e) {
            System.err.println("2nd Catch, that didn't go well: " + e.getMessage());
        }
    }
}

From source file:org.alfresco.extension.countersign.action.executer.PDFAddSignatureFieldActionExecuter.java

License:Open Source License

/**
 * @see org.alfresco.repo.action.executer.ActionExecuterAbstractBase#executeImpl(org.alfresco.service.cmr.repository.NodeRef,
 * org.alfresco.service.cmr.repository.NodeRef)
 *//*from   w  w w.  ja va 2 s  . co  m*/
protected void executeImpl(Action ruleAction, NodeRef actionedUponNodeRef) {

    NodeService ns = serviceRegistry.getNodeService();
    if (ns.exists(actionedUponNodeRef) == false) {
        // node doesn't exist - can't do anything
        return;
    }

    String fieldName = (String) ruleAction.getParameterValue(PARAM_FIELDNAME);
    String position = (String) ruleAction.getParameterValue(PARAM_POSITION);

    JSONObject box;
    int page = -1;

    // parse out the position JSON
    JSONObject positionObj = null;

    try {
        positionObj = (JSONObject) parser.parse(position);
    } catch (ParseException e) {
        logger.error("Could not parse position JSON from Share");
        throw new AlfrescoRuntimeException("Could not parse position JSON from Share");
    }

    // get the page
    page = Integer.parseInt(String.valueOf(positionObj.get("page")));

    // get the box
    box = (JSONObject) positionObj.get("box");

    try {
        // open original pdf
        ContentReader pdfReader = getReader(actionedUponNodeRef);
        PdfReader reader = new PdfReader(pdfReader.getContentInputStream());
        OutputStream cos = serviceRegistry.getContentService()
                .getWriter(actionedUponNodeRef, ContentModel.PROP_CONTENT, true).getContentOutputStream();

        PdfStamper stamp = new PdfStamper(reader, cos);

        // does a field with this name already exist?
        AcroFields allFields = stamp.getAcroFields();

        // if this doc is already signed, cannot add a new sig field without 
        if (allFields.getSignatureNames() != null && allFields.getSignatureNames().size() > 0) {
            throw new AlfrescoRuntimeException("This document has signatures applied, "
                    + "adding a new signature field would invalidate existing signatures");
        }

        // cant create duplicate field names
        if (allFields.getFieldType(fieldName) == AcroFields.FIELD_TYPE_SIGNATURE) {
            throw new AlfrescoRuntimeException(
                    "A signature field named " + fieldName + " already exists in this document");
        }

        // create the signature field
        Rectangle pageRect = reader.getPageSizeWithRotation(page);
        Rectangle sigRect = positionBlock(pageRect, box);
        PdfFormField sigField = stamp.addSignature(fieldName, page, sigRect.getLeft(), sigRect.getBottom(),
                sigRect.getRight(), sigRect.getTop());

        // style the field (no borders)
        sigField.setBorder(new PdfBorderArray(0, 0, 0));
        sigField.setBorderStyle(new PdfBorderDictionary(0, PdfBorderDictionary.STYLE_SOLID));
        allFields.regenerateField(fieldName);

        // apply the change and close streams
        stamp.close();
        reader.close();
        cos.close();

        // once the signature field has been added, apply the sig field aspect
        if (!ns.hasAspect(actionedUponNodeRef, CounterSignSignatureModel.ASPECT_SIGNABLE)) {
            ns.addAspect(actionedUponNodeRef, CounterSignSignatureModel.ASPECT_SIGNABLE, null);
        }

        // now update the signature fields metadata
        Serializable currentFields = ns.getProperty(actionedUponNodeRef,
                CounterSignSignatureModel.PROP_SIGNATUREFIELDS);
        ArrayList<String> fields = new ArrayList<String>();

        if (currentFields != null) {
            fields.addAll((List<String>) currentFields);
        }

        fields.add(fieldName);
        ns.setProperty(actionedUponNodeRef, CounterSignSignatureModel.PROP_SIGNATUREFIELDS, fields);
    } catch (IOException ioex) {
        throw new AlfrescoRuntimeException(ioex.getMessage());
    } catch (DocumentException dex) {
        throw new AlfrescoRuntimeException(dex.getMessage());
    }
}

From source file:org.alfresco.extension.countersign.behavior.SignableDocumentBehavior.java

License:Open Source License

/**
 * When the "signable" aspect is applied, extract the signature fields and add them
 * to the multivalue property//w  ww . j  av a  2 s . c o  m
 */
public void onAddAspect(NodeRef nodeRef, QName aspectTypeQName) {
    try {
        // when the aspect is added, extract the signature fields from the PDF
        ArrayList<String> signatureFields = new ArrayList<String>();
        ContentReader pdfReader = serviceRegistry.getContentService().getReader(nodeRef,
                ContentModel.PROP_CONTENT);
        PdfReader reader = new PdfReader(pdfReader.getContentInputStream());
        AcroFields form = reader.getAcroFields();
        Map<String, Item> fields = form.getFields();
        Iterator<String> it = fields.keySet().iterator();
        while (it.hasNext()) {
            String fieldName = it.next();
            if (form.getFieldType(fieldName) == AcroFields.FIELD_TYPE_SIGNATURE) {
                // add this signature field to the list of available fields
                signatureFields.add(fieldName);
            }
        }
        serviceRegistry.getNodeService().setProperty(nodeRef, CounterSignSignatureModel.PROP_SIGNATUREFIELDS,
                signatureFields);

    } catch (IOException ex) {
        logger.error("Error extracting PDF signature fields from document: " + ex);
    }
}