Sometimes you need the Excel form containing form controls as an interactive PDF form (or AcroForms). Form controls are objects that can be added to a worksheet to enable interaction with a cell or a data range available in the worksheet. With setFormFields method of PdfSaveOptions class, DsExcel allows you to export the form controls as form fields to the PDF document, which will be interactive for the user.
Refer to the following example code to export Excel form controls as PDF form fields:
Java |
Copy Code |
---|---|
// Initialize Workbook. Workbook workbook = new Workbook(); // Create worksheet. IWorksheet ws = workbook.getWorksheets().get("Sheet1"); // Add three checkboxes. ICheckBox checkBox1 = ws.getControls().addCheckBox(62.4, 16.8, 69, 19.2); checkBox1.setValue(false); ICheckBox checkBox2 = ws.getControls().addCheckBox(62.4, 36.6, 69, 19.2); checkBox2.setValue(true); ICheckBox checkBox3 = ws.getControls().addCheckBox(62.4, 57, 69, 19.2); checkBox3.setValue(false); // Add dropdown. IDropDown dropDown = ws.getControls().addDropDown(28.8, 81.8, 103.8, 31.4); dropDown.setPrintObject(true); dropDown.getItems().add(new DropDownItem("Item 1")); dropDown.getItems().add(new DropDownItem("Item 2")); dropDown.getItems().add(new DropDownItem("Item 3")); dropDown.setSelectedIndex(0); // Add listbox. IListBox lstBox1 = ws.getControls().addListBox(51.6, 134.2, 135, 99.6); for (int i = 0; i < 6; i++) { lstBox1.getItems().add(new ListBoxItem("Item " + (i + 1))); } lstBox1.setSelectedIndex(2); // Add option button groups. ws.getControls().addGroupBox(234.2, 8.4, 222.6, 138.6); ws.getControls().addOptionButton(261.2, 29.4, 71.4, 16.8); ws.getControls().addOptionButton(267.8, 70.8, 71.4, 16.8); ws.getControls().addOptionButton(275.6, 111.6, 71.4, 16.8); ws.getControls().addGroupBox(244.4, 187.6, 176.4, 143.4); ws.getControls().addOptionButton(274.4, 216.6, 71.4, 16.8); ws.getControls().addOptionButton(279.8, 255, 71.4, 16.8); ws.getControls().addOptionButton(286.4, 295.2, 71.4, 16.8); // Set FormFields to true to export Excel form controls as PDF form fields. PdfSaveOptions options = new PdfSaveOptions(); options.setFormFields(true); // Save the PDF document. workbook.save("PdfFormFieldExample.pdf", options); |
If form controls are not present in the exported PDF document, then check and enable setPrintObject setting of the specific form control. By default, the value of this property is true for all form controls except the Button form control.
Refer to the following example code to print a Button form control while saving it as a PDF:
Java |
Copy Code |
---|---|
// Create a new workbook Workbook workbook = new Workbook(); IWorksheet worksheet = workbook.getWorksheets().get(0); // Add a button control to worksheet and set its PrintObject property IButton btn = worksheet.getControls().addButton(360, 91.8, 103.94, 19.79); btn.setText("Test settings"); btn.setPrintObject(true); // Save to a pdf file workbook.save("FormControlPdf.pdf"); |
Limitations
For more information on exporting form controls to PDF document, see Simulate UI with PDF form fields.