A form document has exactly the same structure as a text document. The only difference is a new forms entity placed with other paragraph elements – text runs, inline text content controls and hyperlinks. There are seven form types: text field, combo box, dropdown list, checkbox, radio button, picture form, and complex field.
Thus any form document structure with Office JavaScript API used to create it can be outlined like this:
Office JavaScript API
Document and document elements creation:
Api
Document
The main document properties, global color and fill/stroke settings, styles used throughout the document:
ApiDocumentContent, ApiDocument, ApiStyle, ApiFill, ApiStroke, ApiGradientStop, ApiUniColor, ApiPresetColor, ApiRGBColor, ApiSchemeColor
Section
Document section properties:
ApiSection
Paragraph
Common paragraph properties, common text properties, current paragraph properties, paragraph numbering:
ApiParaPr, ApiTextPr, ApiParagraph, ApiNumbering, ApiNumberingLevel
Text run
Common text properties, current text run properties:
ApiTextPr, ApiRun
Image
Common object properties, current image properties:
ApiDrawing, ApiImage
Chart
Common object properties, current chart properties:
ApiDrawing, ApiChart
Shape
Common object properties, current shape properties:
ApiDrawing, ApiShape.
Inline text content control
Common text properties, current inline text content control properties:
ApiTextPr, ApiInlineLvlSdt
Text run
Inline text content control
Hyperlink
Text field
Common object properties, common text properties, current text field properties:
ApiFormBase, ApiTextPr, ApiTextForm
Combo box / dropdown list
Common object properties, common text properties, current combo box / dropdown list properties:
ApiFormBase, ApiTextPr, ApiComboBoxForm
Checkbox / radio button
Common object properties, current checkbox / radio button properties:
ApiFormBase, ApiCheckBoxForm
Picture form
Common object properties, current picture form properties:
ApiFormBase, ApiPictureForm
Complex field
Common object properties, common text properties, current complex field properties:
ApiFormBase, ApiTextPr, ApiComplexForm
Hyperlink
Current hyperlink properties:
ApiHyperlink
Table
Common table styles, common table properties, current table properties:
ApiTableStylePr, ApiTablePr, ApiTable
Table row
Common table row properties, current table row properties:
ApiTableRowPr, ApiTableRow
Table cell
Common table cell properties, current table row properties:
ApiTableCellPr, ApiTableCell
Paragraph
Table
Block content control
Table cell
Common table cell properties, current table row properties:
ApiTableCellPr, ApiTableCell
Paragraph
Table
Block content control
Block content control
Common text properties, current block content control properties:
ApiTextPr, ApiBlockLvlSdt
Paragraph
Table
Block content control
To create a form document, the CreateFile method with the "pdf" extension must be used.
To save the ready document as a form, you need to use the SaveFile method with the "pdf" extension.
The simplest example form document with a single text form containing the "John Smith" text can be built with the help of ONLYOFFICE Document Builder using the following code:
builder.CreateFile("pdf"); // create a form document file in the .pdf format with ONLYOFFICE Document Builder var oDocument = Api.GetDocument(); // create a new 'oDocument' variable and get the created text document contents var oTextForm = Api.CreateTextForm(); // create an empty text form var oParagraph = oDocument.GetElement(0); // get the first empty paragraph from the created document oParagraph.AddElement(oTextForm); // add the created text form to the first paragraph oTextForm.SetText("John Smith"); // add the "John Smith" text to the text form builder.SaveFile("pdf", "example.pdf"); // save the resulting form document as a file in the .pdf format with the 'example.pdf' name builder.CloseFile(); // close the form document file and finish work with ONLYOFFICE Document Builder
If you want to edit an already existing form document, you can open it using ONLYOFFICE Document Builder, get its elements and change them however you need. The only difference from a document editor in this case will be that you will not need this document editor. The document is opened the following way:
builder.OpenFile("https://example.com/myformdocument.pdf"); // use a path to an existing 'myformdocument.pdf' form document file to open it with ONLYOFFICE Document Builder var oDocument = Api.GetDocument(); // create a new 'oDocument' variable and get the created text document contents var oTextForm = Api.CreateTextForm(); // create an empty text form var oParagraph = oDocument.GetElement(0); // get the first empty paragraph from the created document oParagraph.AddElement(oTextForm); // add the created text form to the first paragraph oTextForm.SetText("John Smith"); // add the "John Smith" text to the text form builder.SaveFile("pdf", "example.pdf"); // save the resulting form document as a file in the .pdf format with the 'example.pdf' name builder.CloseFile(); // close the form document file and finish work with ONLYOFFICE Document Builder
As you can see you just need to use the builder.OpenFile(); method of the CDocBuilder class with the path to the necessary form document as an argument to open it. In the above example we open myformdocument.pdf document, get its first paragraph and add the text form with the "John Smith" text to it. The same way any other form document element can be changed.
Use the appropriate API documentation sections to find out which methods allow you to change certain document and spreadsheet element formatting properties.