Package com.lowagie.text.pdf

Examples of com.lowagie.text.pdf.PdfStamper


        System.out.println("Filling in a form");
        try {
            // we create a reader for a certain document
            PdfReader reader = new PdfReader("SimpleRegistrationForm.pdf");
            // filling in the form
            PdfStamper stamp1 = new PdfStamper(reader, new FileOutputStream("registered.pdf"));
            AcroFields form1 = stamp1.getAcroFields();
            form1.setField("name", "Bruno Lowagie");
            form1.setField("address", "Baeyensstraat 121, Sint-Amandsberg");
            form1.setField("postal_code", "BE-9040");
            form1.setField("email", "bruno@lowagie.com");
            stamp1.close();
            // filling in the form and flatten
            reader = new PdfReader("SimpleRegistrationForm.pdf");
            PdfStamper stamp2 = new PdfStamper(reader, new FileOutputStream("registered_flat.pdf"));
            AcroFields form2 = stamp2.getAcroFields();
            form2.setField("name", "Bruno Lowagie");
            form2.setField("address", "Baeyensstraat 121, Sint-Amandsberg");
            form2.setField("postal_code", "BE-9040");
            form2.setField("email", "bruno@lowagie.com");
            stamp2.setFormFlattening(true);
            stamp2.close();
        }
        catch (Exception de) {
            de.printStackTrace();
        }
    }
View Full Code Here


            Certificate[] chain =  ks.getCertificateChain(store.getKeyAlias());

            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

         reader = new PdfReader(new URL(getURL()));
      }
      buffer = new ByteArrayOutputStream();
      try
      {
         stamper = new PdfStamper(reader, buffer);
         Contexts.getEventContext().set(STAMPER_KEY, stamper);
      }
      catch (DocumentException e)
      {
         throw new FacesException("Could not create PDF stamper", e);
View Full Code Here

         Certificate[] chain = ks.getCertificateChain(store.getKeyAlias());

         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);
      } finally {
View Full Code Here

         }
         else
         {
            if (readOnly.booleanValue())
            {
               PdfStamper stamper = (PdfStamper) Contexts.getEventContext().get(STAMPER_KEY);
               stamper.partialFormFlattening(theName);
            }
         }
      }
      catch (DocumentException e)
      {
View Full Code Here

         }
         else
         {
            if (readOnly.booleanValue())
            {
               PdfStamper stamper = (PdfStamper) Contexts.getEventContext().get(STAMPER_KEY);
               stamper.partialFormFlattening(theName);
            }
         }
      }
      catch (DocumentException e)
      {
View Full Code Here

            reader = new PdfReader(new URL(getURL()));
        }
        log.debug("Loading template #0", getURL());
        buffer = new ByteArrayOutputStream();
        try {
            stamper = new PdfStamper(reader, buffer);
            Contexts.getEventContext().set(STAMPER_KEY, stamper);
        } catch (DocumentException e) {
            throw new FacesException("Could not create PDF stamper", e);
        }
        fields = stamper.getAcroFields();
View Full Code Here

        try {
            String surveyName = (String) context.get("surveyName");
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            ByteWrapper byteWrapper = getInputByteWrapper(context, delegator);
            PdfReader pdfReader = new PdfReader(byteWrapper.getBytes());
            PdfStamper pdfStamper = new PdfStamper(pdfReader, os);
            AcroFields acroFields = pdfStamper.getAcroFields();
            HashMap acroFieldMap = acroFields.getFields();
           
            String contentId = (String) context.get("contentId");
            GenericValue survey = null;
            surveyId = (String) context.get("surveyId");
            if (UtilValidate.isEmpty(surveyId)) {
                surveyId = delegator.getNextSeqId("Survey");
                survey = delegator.makeValue("Survey", UtilMisc.toMap("surveyName", surveyName));
                survey.set("surveyId", surveyId);
                survey.set("allowMultiple", "Y");
                survey.set("allowUpdate", "Y");
                survey.create();
            }
           
            // create a SurveyQuestionCategory to put the questions in
            Map createCategoryResultMap = dispatcher.runSync("createSurveyQuestionCategory",
                    UtilMisc.toMap("description", "From AcroForm in Content [" + contentId + "] for Survey [" + surveyId + "]", "userLogin", userLogin));
            String surveyQuestionCategoryId = (String) createCategoryResultMap.get("surveyQuestionCategoryId");
           
            pdfStamper.setFormFlattening(true);
            Iterator i = acroFieldMap.keySet().iterator();
            while (i.hasNext()) {
                String fieldName = (String) i.next();
                AcroFields.Item item = acroFields.getFieldItem(fieldName);
                int type = acroFields.getFieldType(fieldName);
                String value = acroFields.getField(fieldName);
                Debug.logInfo("fieldName:" + fieldName + "; item: " + item + "; value: " + value, module);

                GenericValue surveyQuestion = delegator.makeValue("SurveyQuestion", UtilMisc.toMap("question", fieldName));
                String surveyQuestionId = delegator.getNextSeqId("SurveyQuestion");
                surveyQuestion.set("surveyQuestionId", surveyQuestionId);
                surveyQuestion.set("surveyQuestionCategoryId", surveyQuestionCategoryId);

                if (type == AcroFields.FIELD_TYPE_TEXT) {
                    surveyQuestion.set("surveyQuestionTypeId", "TEXT_SHORT");
                } else if (type == AcroFields.FIELD_TYPE_RADIOBUTTON) {
                    surveyQuestion.set("surveyQuestionTypeId", "OPTION");
                } else if (type == AcroFields.FIELD_TYPE_LIST || type == AcroFields.FIELD_TYPE_COMBO) {
                    surveyQuestion.set("surveyQuestionTypeId", "OPTION");
                    // TODO: handle these specially with the acroFields.getListOptionDisplay (and getListOptionExport?)
                    String[] listOptionDisplayArray = acroFields.getListOptionDisplay(fieldName);
                    String[] listOptionExportArray = acroFields.getListOptionExport(fieldName);
                    Debug.logInfo("listOptionDisplayArray: " + listOptionDisplayArray + "; listOptionExportArray: " + listOptionExportArray, module);
                } else {
                    surveyQuestion.set("surveyQuestionTypeId", "TEXT_SHORT");
                    Debug.logWarning("Building Survey from PDF, fieldName=[" + fieldName + "]: don't know how to handle field type: " + type + "; defaulting to short text", module);
                }
               
                // ==== create a good sequenceNum based on tab order or if no tab order then the page location
               
                Integer tabPage = (Integer) item.page.get(0);
                Integer tabOrder = (Integer) item.tabOrder.get(0);
                Debug.logInfo("tabPage=" + tabPage + ", tabOrder=" + tabOrder, module);
               
                //array of float  multiple of 5. For each of this groups the values are: [page, llx, lly, urx, ury]
                float[] fieldPositions = acroFields.getFieldPositions(fieldName);
                float fieldPage = fieldPositions[0];
                float fieldLlx = fieldPositions[1];
                float fieldLly = fieldPositions[2];
                float fieldUrx = fieldPositions[3];
                float fieldUry = fieldPositions[4];
                Debug.logInfo("fieldPage=" + fieldPage + ", fieldLlx=" + fieldLlx + ", fieldLly=" + fieldLly + ", fieldUrx=" + fieldUrx + ", fieldUry=" + fieldUry, module);

                Long sequenceNum = null;
                if (tabPage != null && tabOrder != null) {
                    sequenceNum = new Long(tabPage.intValue() * 1000 + tabOrder.intValue());
                    Debug.logInfo("tabPage=" + tabPage + ", tabOrder=" + tabOrder + ", sequenceNum=" + sequenceNum, module);
                } else if (fieldPositions.length > 0) {
                    sequenceNum = new Long((long) fieldPage * 10000 + (long) fieldLly * 1000 + (long) fieldLlx);
                    Debug.logInfo("fieldPage=" + fieldPage + ", fieldLlx=" + fieldLlx + ", fieldLly=" + fieldLly + ", fieldUrx=" + fieldUrx + ", fieldUry=" + fieldUry + ", sequenceNum=" + sequenceNum, module);
                }
               
                // TODO: need to find something better to put into these fields...
                String annotation = null;
                Iterator widgetIter = item.widgets.iterator();
                while (widgetIter.hasNext()) {
                    PdfDictionary dict = (PdfDictionary) widgetIter.next();
                   
                    // if the "/Type" value is "/Annot", then get the value of "/TU" for the annotation
                   
                    /* Interesting... this doesn't work, I guess we have to iterate to find the stuff...
                    PdfObject typeValue = dict.get(new PdfName("/Type"));
                    if (typeValue != null && "/Annot".equals(typeValue.toString())) {
                        PdfObject tuValue = dict.get(new PdfName("/TU"));
                        annotation = tuValue.toString();
                    }
                    */
                   
                    PdfObject typeValue = null;
                    PdfObject tuValue = null;
                   
                    Set dictKeys = dict.getKeys();
                    Iterator dictKeyIter = dictKeys.iterator();
                    while (dictKeyIter.hasNext()) {
                        PdfName dictKeyName = (PdfName) dictKeyIter.next();
                        PdfObject dictObject = dict.get(dictKeyName);
                       
                        if ("/Type".equals(dictKeyName.toString())) {
                            typeValue = dictObject;
                        } else if ("/TU".equals(dictKeyName.toString())) {
                            tuValue = dictObject;
                        }
                        //Debug.logInfo("AcroForm widget fieldName[" + fieldName + "] dictKey[" + dictKeyName.toString() + "] dictValue[" + dictObject.toString() + "]", module);
                    }
                    if (tuValue != null && typeValue != null && "/Annot".equals(typeValue.toString())) {
                        annotation = tuValue.toString();
                    }
                }
               
                surveyQuestion.set("description", fieldName);
                if (UtilValidate.isNotEmpty(annotation)) {
                    surveyQuestion.set("question", annotation);
                } else {
                    surveyQuestion.set("question", fieldName);
                }

                GenericValue surveyQuestionAppl = delegator.makeValue("SurveyQuestionAppl", UtilMisc.toMap("surveyId", surveyId, "surveyQuestionId", surveyQuestionId));
                surveyQuestionAppl.set("fromDate", nowTimestamp);
                surveyQuestionAppl.set("externalFieldRef", fieldName);

                if (sequenceNum != null) {
                    surveyQuestionAppl.set("sequenceNum", sequenceNum);
                }

                surveyQuestion.create();
                surveyQuestionAppl.create();
            }           
            pdfStamper.close();
            if (UtilValidate.isNotEmpty(contentId)) {
                survey = delegator.findByPrimaryKey("Survey", UtilMisc.toMap("surveyId", surveyId));
                survey.set("acroFormContentId", contentId);
                survey.store();
            }
View Full Code Here

            }
           
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            ByteWrapper byteWrapper = getInputByteWrapper(context, delegator);
            PdfReader r = new PdfReader(byteWrapper.getBytes());
            PdfStamper s = new PdfStamper(r,os);
            AcroFields fs = s.getAcroFields();
            HashMap hm = fs.getFields();
           
           
            s.setFormFlattening(true);
            Iterator i = hm.keySet().iterator();
            while (i.hasNext()) {
                String fieldName = (String)i.next();
                //AcroFields.Item item = fs.getFieldItem(fieldName);
                //int type = fs.getFieldType(fieldName);
                String value = fs.getField(fieldName);
               
                List questions = delegator.findByAnd("SurveyQuestionAndAppl", UtilMisc.toMap("surveyId", surveyId, "externalFieldRef", fieldName));
                if (questions.size() == 0 ) {
                    Debug.logInfo("No question found for surveyId:" + surveyId + " and externalFieldRef:" + fieldName, module);
                    continue;
                }
               
                GenericValue surveyQuestionAndAppl = (GenericValue)questions.get(0);
                String surveyQuestionId = (String)surveyQuestionAndAppl.get("surveyQuestionId");
                String surveyQuestionTypeId = (String)surveyQuestionAndAppl.get("surveyQuestionTypeId");
                GenericValue surveyResponseAnswer = delegator.makeValue("SurveyResponseAnswer", UtilMisc.toMap("surveyResponseId", surveyResponseId, "surveyQuestionId", surveyQuestionId));
                if (surveyQuestionTypeId ==null || surveyQuestionTypeId.equals("TEXT_SHORT")) {
                    surveyResponseAnswer.set("textResponse", value);
                }

                delegator.create(surveyResponseAnswer);
            }           
            s.close();
        } catch (GenericEntityException e) {
            String errMsg = "Error generating PDF: " + e.toString();
            Debug.logError(e, errMsg, module);
            return ServiceUtil.returnError(errMsg);
        } catch(GeneralException e) {
View Full Code Here

        try {
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            GenericDelegator delegator = dctx.getDelegator();
            ByteWrapper byteWrapper = getInputByteWrapper(context, delegator);
            PdfReader r = new PdfReader(byteWrapper.getBytes());
            PdfStamper s = new PdfStamper(r,os);
            AcroFields fs = s.getAcroFields();
            HashMap map = fs.getFields();
           
            s.setFormFlattening(true);
           
            // Debug code to get the values for setting TDP
    //        String[] sa = fs.getAppearanceStates("TDP");
    //        for (int i=0;i<sa.length;i++)
    //            Debug.log("Appearance="+sa[i]);
View Full Code Here

TOP

Related Classes of com.lowagie.text.pdf.PdfStamper

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.