Click or drag to resize
Pdf Library for .NET

Html Forms to PDF Form Fields

By default the controls of a html form are printed as they look on the page: the generated pdf shows the text boxes, check boxes and lists, but nothing in them can be filled in. Set CreateFormFields to true and every form control becomes a fillable PDF form field placed over the control as it was printed, so the page keeps its own look and the reader can fill the form in a pdf viewer.

Which controls are converted

Html control

PDF form field

input of type text, email, number, search, tel, url, date and time types

Text box, with the control's value and maximum length

input type="password"

Password text box. The value is not carried into the pdf.

textarea

Multiline text box

input type="checkbox"

Check box, checked when the control is

input type="radio"

Radio button group, one per input name (within one form), with the checked button selected

select

Combo box with the options and the selected one

select multiple

List box allowing several selected items

Each field takes the name of the control (or its id when it has no name; a name used twice gets a suffix, since fields with the same name share a value), its read-only and required settings, and a description read from the control's label, so assistive technology can announce it. The values are the ones the controls hold after the page's own scripts and PostLoadingScript have run, so a script can fill the form before it is converted.

Hidden, file, image and button inputs are not converted, and neither are the form's action and the page's scripted validation. Reading the controls needs a script, so JavaScriptEnabled must stay on.

Converting a page with a form
HtmlToPdf converter = new HtmlToPdf();
converter.Options.CreateFormFields = true;

PdfDocument doc = converter.ConvertUrl("https://example.com/application-form");
doc.Save("form.pdf");
doc.Close();

The fields of the generated document can be read and filled later with PdfFormManager.

A form as a region of a page

CreateFormFields does the same for html drawn into a region of a page with PdfHtmlElement: the controls become fields of the document the element is added to, on the page each control was drawn on. It is not available in a header, footer or stamp, which is drawn on every page while a field belongs to one.

PdfDocument doc = new PdfDocument();
PdfPage page = doc.AddPage();

PdfHtmlElement form = new PdfHtmlElement(0, 50, "https://example.com/form");
form.CreateFormFields = true;
page.Add(form);

doc.Save("page-with-form.pdf");
doc.Close();
Accessible and archival documents

In a tagged document (Tagged, PDF/UA, PDF/A level A) every field joins the structure tree in the Form element of the control it replaces, in the reading order of the page. In a document that has to embed its fonts (PDF/A, PDF/UA, PDF/X) the fields are drawn with an embedded font, and check marks and radio buttons are drawn as shapes rather than with a font. A form converted this way validates against PDF/A-1b to PDF/A-4 and PDF/UA-1 and PDF/UA-2.

See Also