How to check a check box in PDF-form using Java PDFBOX api

Ganeshja picture Ganeshja · Jan 30, 2013 · Viewed 8.7k times · Source

How to check a check box in PDF-form using Java PDFBOX api

Initially I tried with the below piece of code but after the execution check box field is invisible in PDF , but it has been checked.. how to avoid such circumstances or they way i have implemented is wrong ? can any one help me out

public void check() throws Exception 
    {
        PDDocument fdeb = null;

         fdeb = PDDocument.load( "C:\\Users\\34\\Desktop\\complaintform.pdf" );
         PDAcroForm form = fdeb.getDocumentCatalog().getAcroForm();
         PDField feld3 = form.getField( "check" );
         feld3.setValue("check");
         fdeb.save("C:\\Users\\34\\Desktop\\complaintform.pdf");
         fdeb.close();

    } 

Thanks

Answer

Ganeshja picture Ganeshja · Jan 30, 2013

Finally got it work !!!! change made in setValue statment and replaced with ((PDCheckbox) feld3).check();

public void check() throws Exception 
    {
        PDDocument fdeb = null;

        fdeb = PDDocument.load( "C:\\Users\\34\\Desktop\\complaintform.pdf" );

         PDAcroForm form = fdeb.getDocumentCatalog().getAcroForm();
         PDField feld3 = form.getField("loan");
         ((PDCheckbox) feld3).check();


         fdeb.save("C:\\Users\\34\\Desktop\\complaintform.pdf");
         fdeb.close();

    }