I am trying to create a PDF with checkbox with the initial value set to "Off". But when I am setting initial value to "Off" the field border and the check mark doesn't show up and does show only when highlighted. It is also not shown up when printing. All the examples which have the initial value set as Yes when it behaves normally but with Off there are lot of misbehavior identified. Please let me know how to achieve border and check mark visibility with initial value as Off and form field not highlighted.
import com.itextpdf.forms.PdfAcroForm;
import com.itextpdf.forms.fields.PdfFormField;
import com.itextpdf.kernel.color.Color;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.border.Border;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.renderer.CellRenderer;
import com.itextpdf.layout.renderer.DrawContext;
import java.io.FileNotFoundException;
public class CheckBoxExample {
public static void main(String[] args) throws FileNotFoundException {
CheckBoxExample checkBoxTable = new CheckBoxExample();
checkBoxTable.generatePDF("Check Box _Non Editable.pdf", false);
checkBoxTable.generatePDF("Check Box Editable.pdf", true);
}
public void generatePDF(String fileName, boolean editable ) throws FileNotFoundException {
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(fileName));
Document document = new Document(pdfDocument);
document.setMargins(60, 10, 60, 10);
document.add(new Paragraph().add("Just to get this started"));
addCheckBoxToTable(document, editable);
if (!editable) {
PdfAcroForm.getAcroForm(pdfDocument, true).flattenFields();
}
document.close();
}
private void addCheckBoxToTable(Document document, boolean editable) {
float[] columnWidths = new float[]{5, 95};
Table table = new Table(columnWidths);
table.setWidthPercent(100);
table.setFontSize(8);
Cell cell;
for (int i = 0; i < 4; i++) {
cell = new Cell();
cell.setNextRenderer(new MyCheckBox(cell, "CheckBox" + i, editable));
cell.setBorder(Border.NO_BORDER);
table.addCell(cell);
cell = new Cell().add(new Paragraph("CheckBox: " + i));
cell.setBorder(Border.NO_BORDER);
table.addCell(cell);
}
document.add(table);
}
private class MyCheckBox extends CellRenderer {
protected String name;
private boolean editable;
public MyCheckBox(Cell modelElement, String name, boolean editable) {
super(modelElement);
this.name = name;
this.editable = editable;
}
@Override
public void draw(DrawContext drawContext) {
PdfDocument document = drawContext.getDocument();
float x = (getOccupiedAreaBBox().getLeft() + getOccupiedAreaBBox().getRight()) / 2;
float y = (getOccupiedAreaBBox().getTop() + getOccupiedAreaBBox().getBottom()) / 2;
Rectangle cellRect = new Rectangle(x - 8, y - 8, 10, 10);
PdfFormField checkBox = PdfFormField.createCheckBox(drawContext.getDocument(), cellRect, name, "Off");
if(!editable) {
checkBox.setBorderColor(Color.GRAY);
checkBox.setBorderWidth(0.5f);
checkBox.regenerateField();
}
PdfAcroForm form = PdfAcroForm.getAcroForm(document, true);
form.addField(checkBox);
}
}
}
Aucun commentaire:
Enregistrer un commentaire