Example usage for com.itextpdf.text.pdf PushbuttonField setOptions

List of usage examples for com.itextpdf.text.pdf PushbuttonField setOptions

Introduction

In this page you can find the example usage for com.itextpdf.text.pdf PushbuttonField setOptions.

Prototype

public void setOptions(int options) 

Source Link

Document

Sets the option flags.

Usage

From source file:bd.gov.forms.web.FormBuilder.java

License:Open Source License

@RequestMapping(value = "/pdfExport", method = RequestMethod.GET)
public String pdfExport(@RequestParam(value = "formId", required = true) String formId,
        @RequestParam(value = "page", required = false) Integer page,
        @RequestParam(value = "colName", required = false) String colName,
        @RequestParam(value = "colVal", required = false) String colVal,
        @RequestParam(value = "sortCol", required = false) String sortCol,
        @RequestParam(value = "sortDir", required = false) String sortDir, ModelMap model,
        HttpServletResponse response, HttpServletRequest request) throws IOException {

    Document document = new Document();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    // step 2//from w  w w.  j  a v a 2s .  c  o  m
    try {
        response.reset();
        response.setContentType("application/pdf");
        response.setHeader("Content-disposition", "inline; filename=test.pdf");

        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setHeader("Pragma", "No-cache");

        PdfWriter writer = PdfWriter.getInstance(document, baos);
        // step 3
        document.open();

        Form form = null;
        //System.out.println("The form id is 1:"+formId);
        if (formId != null) {
            form = formDao.getFormWithFields(formId);
        }

        if (form != null) {
            if (form.getStatus() != 2) {//2-active, 3-deactive
                model.put("doneMessage", "msg.access.denied");
                model.put("doneMsgType", "failed");

                return "redirect:done.htm";
            }
            initForm(form);
        }

        List<Field> fieldList = form.getFields();

        if (fieldList.isEmpty()) {
            System.out.println("The list size is zero");
        }

        PdfPCell space;
        space = new PdfPCell();
        space.setBorder(Rectangle.NO_BORDER);
        space.setColspan(2);
        space.setFixedHeight(8);

        PdfPTable table = new PdfPTable(2);
        PdfPCell cell;

        //PdfPCell cell;                
        table.setWidths(new int[] { 1, 2 });

        int i = 0;
        for (Field f : fieldList) {
            if ("text".equals(f.getType())) {

                table.addCell(f.getLabel());
                cell = new PdfPCell();
                cell.setCellEvent(new TextFields(1, i));
                table.addCell(cell);

            } else if ("textarea".equals(f.getType())) {
                table.addCell(f.getLabel());
                cell = new PdfPCell();
                cell.setCellEvent(new TextFields(1, i));
                cell.setFixedHeight(60);
                table.addCell(cell);

            } else if ("select".equals(f.getType())) {
                table.addCell(f.getType());
                cell = new PdfPCell();
                cell.setCellEvent(new ChoiceFields(3, f.getList().toArray()));
                table.addCell(cell);
                //table.addCell(space);
                System.out.println("ajsdhd");
            }
            i++;
        }

        /*
          for(Field f : fieldList)
          {  
                
        if( "radio".equals(f.getType()) )
        {    
            System.out.println("List "+f.getList()+"  Oppt"+f.getOptions()+ "    df"+f.getColName());
                    
            writer = PdfWriter.getInstance(document, new FileOutputStream("TextFieldForm.pdf"));
                 
            //writer.addJavaScript(Utilities.readFileToString(""));
            // add the radio buttons
            PdfContentByte canvas = writer.getDirectContent();
            Font font = new Font(FontFamily.HELVETICA, 14);
            Rectangle rect;
            PdfFormField field;
            PdfFormField radiogroup = PdfFormField.createRadioButton(writer, true);
            radiogroup.setFieldName("language");
            RadioCheckField radio;
            for (int i = 0; i < 2; i++) 
            {
                rect = new Rectangle(40, 806 - i * 40, 60, 788 - i * 40);
                radio = new RadioCheckField(writer, rect, null, f.getLabel());
                radio.setBorderColor(GrayColor.GRAYBLACK);
                radio.setBackgroundColor(GrayColor.GRAYWHITE);
                radio.setCheckType(RadioCheckField.TYPE_CIRCLE);
                field = radio.getRadioField();
                radiogroup.addKid(field);
                        
                writer.addAnnotation(field);
                       
                ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT,
                    new Phrase(f.getLabel(), font), 70, 790 - i * 40, 0);
            }
            //table.addCell(f.getLabel());
            //cell = new PdfPCell();
                    
            //document.add(radiogroup);
            //writer.addAnnotation(radiogroup);
                    
                
        }
                
          }       */

        // Add submit button   
        PushbuttonField submitBtn = new PushbuttonField(writer, new Rectangle(400, 700, 370, 670),
                "submitPOST");
        //submitBtn.setBackgroundColor(Color.GRAY);
        submitBtn.setBorderStyle(PdfBorderDictionary.STYLE_BEVELED);
        submitBtn.setText("Submit");
        submitBtn.setOptions(PushbuttonField.VISIBLE_BUT_DOES_NOT_PRINT);
        PdfFormField submitField = submitBtn.getField();
        submitField.setAction(
                PdfAction.createSubmitForm("http://localhost:8084/GovForm-07-02/formBuilder/pdfresponse.htm",
                        null, PdfAction.SUBMIT_HTML_FORMAT));

        writer.addAnnotation(submitField);

        document.add(table);

        System.out.println("Pdf creation successful");

        document.close();

        ServletOutputStream out = response.getOutputStream();
        baos.writeTo(out);
        out.flush();

    } catch (Exception ex) {
        System.out.println("Could not print reasone::" + ex.toString());

    }

    //////////////////////////////////////// email part////////////////////////////  
    //email functionalities
    // Recipient's email ID needs to be mentioned.
    String to = "tanviranik@gmail.com";

    // Sender's email ID needs to be mentioned
    String from = "tanvir_cse@yahoo.com";

    // Assuming you are sending email from localhost
    String host = "localhost";

    // Get system properties
    Properties properties = System.getProperties();

    // Setup mail server
    properties.setProperty("mail.smtp.host", host);

    // Get the default Session object.
    Session session = Session.getDefaultInstance(properties);

    try {
        // Create a default MimeMessage object.
        MimeMessage message = new MimeMessage(session);

        // Set From: header field of the header.
        message.setFrom(new InternetAddress(from));

        // Set To: header field of the header.
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

        // Set Subject: header field
        message.setSubject("This is the Subject Line!");

        // Now set the actual message
        message.setText("This is actual message");

        // Send message
        Transport.send(message);
        System.out.println("Sent message successfully....");
    } catch (MessagingException mex) {
        mex.printStackTrace();
    }
    //////////////////////////////////////// email part////////////////////////////

    return null;

}