Use DocumentBuilder to inserting a Form Field
within C# and .NET


   Use field codes to insert fields that can provide automatically updated information, such as the time, title, page numbers, and so on. You can switch between displaying field codes and results in your document in Microsoft Word with the keyboard shortcut Alt + F9. Field codes appear between curly braces { }. Use DocumentBuilderInsertField to create fields in the document.

   To create a Form in Microsoft Word that others can fill out, start with a template or document and add content controls. Content controls include things like check boxes, text boxes, date pickers, and drop-down lists. DocumentBuilder provides special methods to insert each type of form field into the document: DocumentBuilderInsertTextInput, DocumentBuilderInsertCheckBox, DocumentBuilderInsertComboBox.

Complete code

using System;
using SautinSoft.Document;
using System.Text;
using SautinSoft.Document.Drawing;

namespace Example
{
    class Program
    {
        static void Main(string[] args)
        {
            // Get your free 30-day key here:   
            // https://sautinsoft.com/start-for-free/
            InsertingField();
        }
        /// <summary>
        /// Generate document with forms and fields using DocumentBuilder.
        /// </summary>
        /// <remarks>
        /// Details: https://www.sautinsoft.com/products/document/help/net/developer-guide/documentbuilder-inserting-field.php
        /// </remarks>

        static void InsertingField()
        {
            DocumentCore dc = new DocumentCore();
            DocumentBuilder db = new DocumentBuilder(dc);

            string resultPath = @"Result.pdf";
            string[] items = { "One", "Two", "Three", "Four", "Five" };

            // Insert the formatted text into the document using DocumentBuilder.
            db.CharacterFormat.FontName = "Verdana";
            db.CharacterFormat.Size = 16;
            db.CharacterFormat.FontColor = Color.Orange;
            db.Writeln("Generate document with forms and fields using DocumentBuilder.");
            db.CharacterFormat.ClearFormatting();

            db.Write(@"{ TIME   \* MERGEFORMAT } - ");
            db.InsertField("TIME");
            db.InsertSpecialCharacter(SpecialCharacterType.LineBreak);

            db.InsertTextInput("TextInput", FormTextType.RegularText, "", "Insert Text Input", 0);
            db.InsertSpecialCharacter(SpecialCharacterType.LineBreak);
            
            db.InsertCheckBox("CheckBox", true, 0);
            db.InsertSpecialCharacter(SpecialCharacterType.LineBreak);

            db.InsertComboBox("DropDown", items, 3);

            // Save our document into PDF format.
            dc.Save(resultPath, new PdfSaveOptions());

            // Open the result for demonstration purposes.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(resultPath) { UseShellExecute = true });
        }
    }
}

Download

Imports System
Imports SautinSoft.Document
Imports System.Text
Imports SautinSoft.Document.Drawing

Namespace Example
	Friend Class Program
		Shared Sub Main(ByVal args() As String)
			InsertingField()
		End Sub
                ''' Get your free 30-day key here:   
                ''' https://sautinsoft.com/start-for-free/
		''' <summary>
		''' Generate document with forms and fields using DocumentBuilder.
		''' </summary>
		''' <remarks>
		''' Details: https://www.sautinsoft.com/products/document/help/net/developer-guide/documentbuilder-inserting-field.php
		''' </remarks>

		Private Shared Sub InsertingField()
			Dim dc As New DocumentCore()
			Dim db As New DocumentBuilder(dc)

			Dim resultPath As String = "result.pdf"
			Dim items() As String = {"One", "Two", "Three", "Four", "Five"}

			' Insert the formatted text into the document using DocumentBuilder.
			db.CharacterFormat.FontName = "Verdana"
			db.CharacterFormat.Size = 16
			db.CharacterFormat.FontColor = Color.Orange
			db.Writeln("Generate document with forms and fields using DocumentBuilder.")
			db.CharacterFormat.ClearFormatting()

			db.Write("{ TIME   \* MERGEFORMAT } - ")
			db.InsertField("TIME")
			db.InsertSpecialCharacter(SpecialCharacterType.LineBreak)

			db.InsertTextInput("TextInput", FormTextType.RegularText, "", "Insert Text Input", 0)
			db.InsertSpecialCharacter(SpecialCharacterType.LineBreak)

			db.InsertCheckBox("CheckBox", True, 0)
			db.InsertSpecialCharacter(SpecialCharacterType.LineBreak)

			db.InsertComboBox("DropDown", items, 3)

			' Save our document into PDF format.
			dc.Save(resultPath, New PdfSaveOptions())

			' Open the result for demonstration purposes.
			System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(resultPath) With {.UseShellExecute = True})
		End Sub
	End Class
End Namespace

Download


If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below:



Questions and suggestions from you are always welcome!

We are developing .Net components since 2002. We know PDF, DOCX, RTF, HTML, XLSX and Images formats. If you need any assistance with creating, modifying or converting documents in various formats, we can help you. We will write any code example for you absolutely free.