[]
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 FormFields property of PdfSaveOptions class, GcExcel 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:
// Initialize Workbook.
var workbook = new Workbook();
// Create worksheet.
IWorksheet ws = workbook.Worksheets["Sheet1"];
// Add three checkboxes.
var checkBox1 = ws.Controls.AddCheckBox(62.4, 16.8, 69, 19.2);
checkBox1.Value = false;
var checkBox2 = ws.Controls.AddCheckBox(62.4, 36.6, 69, 19.2);
checkBox2.Value = true;
var checkBox3 = ws.Controls.AddCheckBox(62.4, 57, 69, 19.2);
checkBox3.Value = false;
// Add dropdown.
var dropDown = ws.Controls.AddDropDown(28.8, 81.8, 103.8, 31.4);
dropDown.PrintObject = true;
dropDown.Items.Add(new DropDownItem("Item 1"));
dropDown.Items.Add(new DropDownItem("Item 2"));
dropDown.Items.Add(new DropDownItem("Item 3"));
dropDown.SelectedIndex = 0;
// Add listbox.
var lstBox1 = ws.Controls.AddListBox(51.6, 134.2, 135, 99.6);
for (int i = 0; i < 6; i++)
{
lstBox1.Items.Add(new ListBoxItem("Item " + (i + 1)));
}
lstBox1.SelectedIndex = 2;
// Add option button groups.
ws.Controls.AddGroupBox(234.2, 8.4, 222.6, 138.6);
ws.Controls.AddOptionButton(261.2, 29.4, 71.4, 16.8);
ws.Controls.AddOptionButton(267.8, 70.8, 71.4, 16.8);
ws.Controls.AddOptionButton(275.6, 111.6, 71.4, 16.8);
ws.Controls.AddGroupBox(244.4, 187.6, 176.4, 143.4);
ws.Controls.AddOptionButton(274.4, 216.6, 71.4, 16.8);
ws.Controls.AddOptionButton(279.8, 255, 71.4, 16.8);
ws.Controls.AddOptionButton(286.4, 295.2, 71.4, 16.8);
// Set FormFields to true to export Excel form controls as PDF form fields.
var options = new PdfSaveOptions { FormFields = true };
// Save the PDF document.
workbook.Save("PdfFormFieldExample.pdf", options);
type=warning
Note: The ZIndex of mapped PDF form fields is always higher than other shapes.
If form controls are not present in the exported PDF document, then check and enable PrintObject 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:
// Initialize Workbook.
Workbook workbook = new Workbook();
IWorksheet worksheet = workbook.Worksheets[0];
// Add a button control to worksheet and set its PrintObject property
var btn = worksheet.Controls.AddButton(360 * ColScale, 91.8, 103.94 * WidthScale, 19.79);
btn.Text = "Test settings";
btn.PrintObject = true;
// Save to a pdf file
workbook.Save("FormControlPdf.pdf");
Limitations
GcExcel does not support the export of the following controls and properties because they are not available in PDF form fields:
IButton
IGroupBox
ILabel
IScrollBar
ISpinner
Mixed option for GrapeCity.Documents.Excel.Forms.ICheckBox.IsChecked
GrapeCity.Documents.Excel.Forms.SelectionMode.Extended