Creating PDF forms

A PDF form is a PDF document in which the user can enter data via various controls and then submit the form to be either emailed to an address to sent to a server.

PDF controls

The available controls are PDFTextField, PDFTextArea, PDFCheckBox, PDFRadioButton, PDFPopupMenu, PDFComboBox and PDFListBox. PDF controls can only be added to PDF documents you create in Xojo via the PDFDocument class. A control is added to a PDFDocument object via it's AddControl method.

Note

PDF documents can be created via a PDF template which describes a PDF document (which can include controls) in JSON format. See PDFDocument for details.

Drawing controls

Unlike their desktop, web and mobile counterparts, PDFTextField, PDFTextArea, PDFCheckBox, and PDFRadioButton do not draw the entire control. For PDFTextField and PDFTextArea, no border is drawn. For PDFCheckBox and PDFRadioButton no captions are drawn. PDFButton draws the caption but not the border around the button itself. If you want to add these, you can do so by drawing rectangles for borders and text for captions.

Displaying the form to the user

To display it to the user so that they can add information and submit it, the PDFDocument must be saved to disk and then opened in a PDF viewer app that fully supports forms such as Acrobat Reader.

Sending a PDF form via email or to a server

After the user enters information into the various controls added to the PDF file, the data can be sent elsewhere by clicking on a button that has been set to either create an email with the PDF file attached, send the data from the form to a server or send the entire PDF file itself to a server. All are done by setting the Action property of the a PDFButton.

Warning

As of MacOS Big Sur, Apple's Preview app does not support actions for buttons in PDF files.

Example code

This example generates a PDFDocument that includes all the various PDF controls and then saves the document to the desktop. You can then open it with Acrobat Reader or any other PDF viewer that supports button actions. Fill out the form and then press the Send button. A draft email to hello@example.com will be created as a draft email will then be created with the filled out PDF form attached.

Var d As New PDFDocument
d.Compressed = False
Var f As FolderItem = SpecialFolder.Desktop.Child("AcroFormTest.pdf")
Var g As Graphics = d.Graphics

' PDFForm - TextField control

Var tf As New PDFTextField(1, 100, 50, 200, 22, "TextField", "Some sample text")
d.AddControl(tf)

' PDFForm - TextArea control
Var ta As New PDFTextArea(1, 100, 100, 200, 300, "CampoTexto", "Hi There!")
d.AddControl(ta)

' PDFForm - Button control with Action set to default = "Reset"
Var btn1 As New PDFButton(1, 320, 100, 50, 22, "Reset","Reset")
d.AddControl(btn1)

' PDFForm - CheckBox control
Var chB1 As New PDFCheckbox(1, 100, 430,15, 15,"Checkbox 1","checkbox")
d.AddControl(chB1)

Var chB2 As New PDFCheckbox(1, 200, 430, 15, 15,"Checkbox 2", "Checkbox 2")
chB2.Value = True
d.AddControl(chB2)

' PDFForm - RadioButton group control
Var rBtn1GroupA As New PDFRadioButton(1, 200, 450, 15,15,"GroupA","OptionA")
Var rBtn2GroupA As New PDFRadioButton(1, 200, 470, 15, 15,"GroupA", "OptionB")
rBtn2GroupA.Value = True
d.AddControl(rBtn1GroupA)
d.AddControl(rBtn2GroupA)

Var rBtn1GroupB As New PDFRadioButton(1, 200, 500, 15,15,"GroupB","OptionA")
rBtn1GroupB.Value = True
Var rBtn2GroupB As New PDFRadioButton(1, 200, 520, 15, 15,"GroupB", "OptionB")
d.AddControl(rBtn1GroupB)
d.AddControl(rBtn2GroupB)

' PDFForm - ChoiceList control, set to Editable ComboBox
Var cCombo As New PDFComboBox(1,100,540,100,15,"ListC","Option A", "Option B", "Option C", "Option D")
cCombo.SelectedRowValue = "Option C"
d.AddControl(cCombo)

' PDFForm - ChoiceList control, set to No-Editable ComboBox
Var cPopup As New PDFPopupMenu(1,210,540,100,15,"List","Option A", "Option B", "Option C", "Option D")
d.AddControl(cPopup)

' PDFForm - ChoiceList control, set to ListBox with Multiple Selections
Var cListBox As New PDFListBox(1,320,540,100,90,"ListB","Option A", "Option B", "Option C", "Option D")
cListBox.RowSelectionType = PDFListBox.RowSelectionTypes.Multiple
cListBox.SelectedRowValue = "Option D"
d.AddControl(cListBox)

' PDFForm - Button control with Action set to "Submit"
Var btn2 As New PDFButton(1, 320, 660, 50, 22, "Send","Send")
btn2.Action = PDFButton.Actions.SendPDFFile
btn2.URL = "mailto:hello@example.com"
btn2.Caption = "TestDocument"
d.AddControl(btn2)

' Drawing for the PDFForm Layout -> rectangles surrounding widget areas and labels
g.DrawRectangle(95,45,210,32)
g.DrawRectangle(95,95,210,310)
g.DrawRoundRectangle(320,95,50, 32, 10, 10)
g.DrawRectangle(100,430,15,15)
g.DrawRectangle(200,430,15,15)

Var y As Integer = 430 + (7.5 + ((g.TextHeight)/2))
g.DrawText("Xojo rocks!", 125,y)
g.DrawText("I Love Native Multiplatform", 225,y)

y = 450 + (7.5 + ((g.TextHeight)/2))
g.DrawRectangle(200,450,15,15)
g.DrawText("Option A", 225,y)

y = 470 + (7.5 + ((g.TextHeight)/2))
g.DrawRectangle(200,470,15,15)
g.DrawText("Option B", 225,y)

g.DrawRectangle(200,500,15,15)
y = 500 + (7.5 + ((g.TextHeight)/2))
g.DrawText("Option A-1", 225,y)
g.DrawRectangle(200,520,15,15)

y = 520 + (7.5 + ((g.TextHeight)/2))
g.DrawText("Option B-1", 225,y)
g.DrawRectangle(100,540,100,15)

g.DrawRectangle(320,540,100,90)
g.DrawRoundRectangle(320,655,50, 32, 10, 10)

g.DrawRectangle(210,540,100,15)

' Saving the PDF document instance and opening it in the by default viewer App
d.Save(f)