Hi All,
I am generating PDF using PDFBox, where I need to add a checkbox, which needs
to be preset to checked and readonly. But some how it does not work.Please find
below code, which adds the checkbox on PDF:import java.io.File;
import java.io.IOException;
import org.apache.pdfbox.cos.COSArray;
import org.apache.pdfbox.cos.COSDictionary;
import org.apache.pdfbox.cos.COSFloat;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.cos.COSString;
import org.apache.pdfbox.exceptions.COSVisitorException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
import org.apache.pdfbox.pdmodel.interactive.form.PDCheckbox;
import org.apache.pdfbox.pdmodel.interactive.form.PDField;
public class MyTest {
public static void main(String arg[]) throws IOException,
COSVisitorException
{
PDDocument document = new PDDocument();
PDPage page = new PDPage(PDPage.PAGE_SIZE_LETTER);
document.addPage(page);
COSDictionary acroFormDict = new COSDictionary();
acroFormDict.setItem(COSName.getPDFName("Fields"), new COSArray()); //
Added this line for Tilman's comment
PDAcroForm acroForm = new PDAcroForm(document, acroFormDict);
document.getDocumentCatalog().setAcroForm(acroForm);
float x = 10f;
float y = page.getMediaBox().getHeight();
float yOffset = 15f;
float yCurrent = y - yOffset;
COSDictionary cosDict = new COSDictionary();
COSArray rect = new COSArray();
rect.add(new COSFloat(x));
rect.add(new COSFloat(yCurrent));
rect.add(new COSFloat(x+20));
rect.add(new COSFloat(yCurrent-20));
cosDict.setItem(COSName.RECT, rect);
cosDict.setItem(COSName.FT, COSName.getPDFName("Btn")); // Field Type
cosDict.setItem(COSName.TYPE, COSName.ANNOT);
cosDict.setItem(COSName.SUBTYPE, COSName.getPDFName("Widget"));
cosDict.setItem(COSName.T, new COSString("testChk"));
// cosDict.setItem(COSName.DA, new COSString("/Helv 7 Tf 0 g"));
PDCheckbox checkbox = new PDCheckbox(acroForm, cosDict);
// checkbox.setReadonly(true);
// checkbox.setFieldFlags(PDField.FLAG_READ_ONLY);
checkbox.setValue("Yes");
// checkbox.check();
page.getAnnotations().add(checkbox.getWidget());
acroForm.getFields().add(checkbox); // Added this line for Tilman's
comment
yCurrent = yCurrent - 20 - yOffset;
File file = new File("C:\\pdf\\CheckBox\\CheckBoxSample1.pdf");
System.out.println("Sample file saved at : " + file.getAbsolutePath());
document.save(file);
document.close();
}
}
Now if you uncomment the
line:checkbox.setReadonly(true);Orcheckbox.setFieldFlags(PDField.FLAG_READ_ONLY);The
checkbox will no more displayed (or may be there but value is unchecked). I am
using PDFBox 1.8.10Behavior is similar in Adobe Reader 11 and Foxit.Also if I
generate the PDF without setting the box readonly, in Adobe, I see the checkbox
displayed with value set, but when I bring focus on it using tab, it gets
disappear. And again on focus out (I click somewhere else other than checkbox),
it appears again.Note: I also tried with
acroFormDict.setBoolean(COSName.getPDFName("NeedAppearances"), true);
But it did not help.To check the generated file, please go to
https://github.com/PatelVishalJ/PDFBoxSampleFileClick on "Download ZIP" button,
unzip the downloaded file, that will contain the generated PDF file.
It seems like I am missing a very small thing, but could not find out. Any
help? Thanks in advance.
Vishal