Create AcroForm - Create Fillable PDF in C#
The following code snippet also work with Aspose.PDF.Drawing library.
Create form from scratch
Add Form Field in a PDF Document
The Document class provides a collection named Form which helps you manage form fields in a PDF document.
To add a form field:
- Create the form field you want to add.
- Call the Form collection’s Add method.
Adding TextBoxField
Below example shows how to add a TextBoxField.
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_Forms();
// Open document
Document document = new Document(dataDir + "TextField.pdf");
// Create a field
TextBoxField textBoxField = new TextBoxField(document.Pages[1], new Aspose.Pdf.Rectangle(100, 200, 300, 300));
textBoxField.PartialName = "textbox1";
textBoxField.Value = "Text Box";
// TextBoxField.Border = new Border(
Border border = new Border(textBoxField);
border.Width = 5;
border.Dash = new Dash(1, 1);
textBoxField.Border = border;
textBoxField.Color = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Green);
// Add field to the document
document.Form.Add(textBoxField, 1);
// Save modified PDF
document.Save(dataDir + "TextBox_out.pdf");
Adding RadioButtonField
The following code snippets show how to add RadioButtonField in a PDF document.
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_Forms();
// Instantiate Document object
Document document = new Document();
// Add a page to PDF file
document.Pages.Add();
// Instatiate RadioButtonField object with page number as argument
RadioButtonField radio = new RadioButtonField(document.Pages[1]);
// Add first radio button option and also specify its origin using Rectangle object
radio.AddOption("Test", new Rectangle(0, 0, 20, 20));
// Add second radio button option
radio.AddOption("Test1", new Rectangle(20, 20, 40, 40));
// Add radio button to form object of Document object
document.Form.Add(radio);
// Save the PDF file
document.Save(dataDir + "RadioButton_out.pdf");
The following code snippet shows the steps to add RadioButtonField with three options and place them inside Table cells.
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_Forms();
Document document = new Document();
Page page = document.Pages.Add();
Table table = new Table();
table.ColumnWidths = "120 120 120";
page.Paragraphs.Add(table);
Row r1 = table.Rows.Add();
Cell c1 = r1.Cells.Add();
Cell c2 = r1.Cells.Add();
Cell c3 = r1.Cells.Add();
RadioButtonField rf = new RadioButtonField(page);
rf.PartialName = "radio";
document.Form.Add(rf, 1);
RadioButtonOptionField opt1 = new RadioButtonOptionField();
RadioButtonOptionField opt2 = new RadioButtonOptionField();
RadioButtonOptionField opt3 = new RadioButtonOptionField();
opt1.OptionName = "Item1";
opt2.OptionName = "Item2";
opt3.OptionName = "Item3";
opt1.Width = 15;
opt1.Height = 15;
opt2.Width = 15;
opt2.Height = 15;
opt3.Width = 15;
opt3.Height = 15;
rf.Add(opt1);
rf.Add(opt2);
rf.Add(opt3);
opt1.Border = new Border(opt1);
opt1.Border.Width = 1;
opt1.Border.Style = BorderStyle.Solid;
opt1.Characteristics.Border = System.Drawing.Color.Black;
opt1.DefaultAppearance.TextColor = System.Drawing.Color.Red;
opt1.Caption = new TextFragment("Item1");
opt2.Border = new Border(opt1);
opt2.Border.Width = 1;
opt2.Border.Style = BorderStyle.Solid;
opt2.Characteristics.Border = System.Drawing.Color.Black;
opt2.DefaultAppearance.TextColor = System.Drawing.Color.Red;
opt2.Caption = new TextFragment("Item2");
opt3.Border = new Border(opt1);
opt3.Border.Width = 1;
opt3.Border.Style = BorderStyle.Solid;
opt3.Characteristics.Border = System.Drawing.Color.Black;
opt3.DefaultAppearance.TextColor = System.Drawing.Color.Red;
opt3.Caption = new TextFragment("Item3");
c1.Paragraphs.Add(opt1);
c2.Paragraphs.Add(opt2);
c3.Paragraphs.Add(opt3);
// Save the PDF file
document.Save(dataDir + "RadioButtonWithOptions_out.pdf");
Adding Caption to RadioButtonField
Following code snippet shows how to add caption which will be associated with RadioButtonField:
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_Forms();
// Load source PDF form
Form form1 = new Form(dataDir + "RadioButtonField.pdf");
Document PDF_Template_PDF_HTML = new Document(dataDir + "RadioButtonField.pdf");
foreach (var item in form1.FieldNames)
{
Console.WriteLine(item.ToString());
Dictionary<string, string> radioOptions = form1.GetButtonOptionValues(item);
if (item.Contains("radio1"))
{
RadioButtonField field0 = PDF_Template_PDF_HTML.Form[item] as RadioButtonField;
RadioButtonOptionField fieldoption = new RadioButtonOptionField();
fieldoption.OptionName = "Yes";
fieldoption.PartialName = "Yesname";
var updatedFragment = new TextFragment("test123");
updatedFragment.TextState.Font = FontRepository.FindFont("Arial");
updatedFragment.TextState.FontSize = 10;
updatedFragment.TextState.LineSpacing = 6.32f;
// Create TextParagraph object
TextParagraph par = new TextParagraph();
// Set paragraph position
par.Position = new Position(field0.Rect.LLX, field0.Rect.LLY + updatedFragment.TextState.FontSize);
// Specify word wraping mode
par.FormattingOptions.WrapMode = TextFormattingOptions.WordWrapMode.ByWords;
// Add new TextFragment to paragraph
par.AppendLine(updatedFragment);
// Add the TextParagraph using TextBuilder
TextBuilder textBuilder = new TextBuilder(PDF_Template_PDF_HTML.Pages[1]);
textBuilder.AppendParagraph(par);
field0.DeleteOption("item1");
}
}
PDF_Template_PDF_HTML.Save(dataDir + "RadioButtonField_out.pdf");
Adding ComboBox field
The following code snippets show how to add ComboBox field in a PDF document.
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_Forms();
// Create Document object
Document document = new Document();
// Add page to document object
document.Pages.Add();
// Instantiate ComboBox Field object
ComboBoxField combo = new ComboBoxField(document.Pages[1], new Aspose.Pdf.Rectangle(100, 600, 150, 616));
// Add option to ComboBox
combo.AddOption("Red");
combo.AddOption("Yellow");
combo.AddOption("Green");
combo.AddOption("Blue");
// Add combo box object to form fields collection of document object
document.Form.Add(combo);
// Save the PDF document
document.Save(dataDir + "ComboBox_out.pdf");
Add Tooltip to Form Field
The Document class provides a collection named Form which manages form fields in a PDF document. To add a tooltip to a form field, use the Field class AlternateName. Adobe Acrobat uses the ‘alternate name’ as a field tooltip.
The code snippets that follow show how to add a tooltip to a form field, first using C# and then Visual Basic.
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_Forms();
// Load source PDF form
Document document = new Document(dataDir + "AddTooltipToField.pdf");
// Set the tooltip for textfield
(document.Form["textbox1"] as Field).AlternateName = "Text box tool tip";
// Save the updated document
document.Save(dataDir + "AddTooltipToField_out.pdf");