I need to display a large number of servlet request parameters in a pdf format. So I decided to create a helper class which will have factory methods to create various pdf objects and can be used to create a pdf template, which can be filled in with the user entered values using the pdfstamper class. The problem I am having is
that the check boxes that I create doesn't print even though they are visible on the pdf report. The code is given below. Please tell what I am doing wrong? Also is there any other better ways to acheive my goal? (populating pdf template with request information)
 

import java.awt.Color;
import java.io.FileOutputStream;
import java.io.IOException;

import com.lowagie.text.*;
import com.lowagie.text.pdf.*;

public class PdfHelper
{
    public static void main(String[] args)
    {
        System.out.println("Textfield");

        // step 1: creation of a document-object
        Document document = new Document(PageSize.A4);

        try
        {
            String fileName = "Temp/checkbox.pdf";
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(fileName));
            document.open ();
            PdfContentByte cb = writer.getDirectContent();
            BaseFont font = BaseFont.createFont("Helvetica", "winansi", false);
           
            writer.addAnnotation(getPdfCheckBox(writer, cb, "fieldName1",  10, font, 12, 170, 780, 10, 10, true));
        }
        catch(DocumentException de) {
            System.err.println(de.getMessage());
        }
        catch(IOException ioe) {
            System.err.println(ioe.getMessage());
        }
        catch(Exception ioe) {
            System.err.println(ioe.getMessage());
        }

        document.close();
    }
   
    public static synchronized PdfFormField getPdfCheckBox( PdfWriter writer,
                                                            PdfContentByte cb, 
                                                            String fieldName, // Acro field name
                                                            int maxLength,    // Maximum characters you can fill in
                                                            BaseFont font,   
                                                            float fontSize,
                                                            int x,            // top left corner x-coordinate
                                                            int y,            // top left corner y-coordinate
                                                            int width,       
                                                            int height,      
                                                            boolean isChecked) throws Exception
    {
       cb.moveTo(0, 0);
        PdfAppearance tpOff = cb.createAppearance(width, height);
        tpOff.rectangle(1, 1, width-2, height-2);
        tpOff.stroke();

        PdfAppearance tpOn = cb.createAppearance(width, height);
        tpOn.rectangle(1, 1, width-2, height-2);
        tpOn.setRGBColorFill(255, 255, 255);       
        tpOn.fillStroke();       
        tpOn.moveTo(1, 1);
        tpOn.lineTo(width-1, height-1);
        tpOn.moveTo(1, width-1);
        tpOn.lineTo(height-1, 1);
        tpOn.stroke();

        Rectangle size = new Rectangle(x, y, x+width, y+height);
       
        PdfFormField formField = PdfFormField.createCheckBox(writer);
        formField.setWidget(size, PdfAnnotation.HIGHLIGHT_INVERT );
        formField.setFieldName(fieldName);
        formField.setValueAsName("Off");
        formField.setAppearanceState("Off");
        formField.setFieldFlags(PdfFormField.FLAGS_PRINT);
        if (isChecked)
            formField.setAppearanceState("On");
        formField.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, "Off", tpOff);
        formField.setAppearance(PdfAnnotation.APPEARANCE_NORMAL , "On", tpOn);

        return formField;
    }  
}

Reply via email to