Skip to content

Commit 8b1ec4e

Browse files
author
KB Bot
committed
Added new kb article insert-form-xobject-elements-pdf-table-cell
1 parent 943845b commit 8b1ec4e

File tree

1 file changed

+134
-0
lines changed

1 file changed

+134
-0
lines changed
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
---
2+
title: Creating a PDF Table with Form Fields Inside the Cells
3+
description: Learn how to insert interactive form fields such as radio buttons and textboxes into table cells in a PDF document using RadPdfProcessing.
4+
type: how-to
5+
page_title: How to Add Interactive Form Fields to Table Cells in PDF Documents with RadPdfProcessing
6+
slug: insert-form-xobject-elements-pdf-table-cell
7+
tags: pdfprocessing, document, processing, pdf, table, interactive, forms, radio, button, textbox, cell, acroform
8+
res_type: kb
9+
ticketid: 1663177
10+
---
11+
12+
## Environment
13+
14+
| Version | Product | Author |
15+
| --- | --- | ---- |
16+
| 2024.3.806| RadPdfProcessing |[Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova)|
17+
18+
## Description
19+
20+
Learn how to insert interactive [form fields]({%slug radpdfprocessing-model-interactive-forms-form-fields%}) such as [radio buttons]({%slug radpdfprocessing-model-interactive-forms-form-fields-radiobuttonfield%}) and [textboxes]({%slug radpdfprocessing-model-interactive-forms-form-fields-textboxfield%}) into [table cells]({%slug radpdfprocessing-editing-tablecell%}) in a PDF document using RadPdfProcessing.
21+
22+
## Solution
23+
24+
To insert interactive form fields like radio buttons and textboxes into table cells in a PDF document, follow the steps below:
25+
26+
1. Create a new [RadFixedDocument]({%slug radpdfprocessing-model-radfixeddocument%}) and add a page to it.
27+
2. Initialize a [FixedContentEditor]({%slug radpdfprocessing-editing-fixedcontenteditor%}) on the page for drawing content.
28+
3. Define a [Table]({%slug radpdfprocessing-editing-table%}) object and configure its properties, including borders and cell padding.
29+
4. For each row in the table:
30+
- Add a text cell with the caption using [InsertText](https://docs.telerik.com/devtools/document-processing/libraries/radpdfprocessing/editing/block#inserting-text).
31+
- Create a [RadioButtonField]({%slug radpdfprocessing-model-interactive-forms-form-fields-radiobuttonfield%}) , configure its options, and draw it in a table cell.
32+
- Create a [TextBoxField]({%slug radpdfprocessing-model-interactive-forms-form-fields-textboxfield%}), set its value, and draw it in another table cell.
33+
5. [Export](https://docs.telerik.com/devtools/document-processing/libraries/radpdfprocessing/formats-and-conversion/pdf/pdfformatprovider/pdfformatprovider#export) the `RadFixedDocument` to a PDF file.
34+
35+
Below is a simplified code snippet demonstrating these steps:
36+
37+
```csharp
38+
private readonly string[] _questions = new string[] { "Question 1", "Question 2", "Question 3" };
39+
40+
public static void Main(string[] args)
41+
{
42+
RadFixedDocument document = new RadFixedDocument();
43+
RadFixedPage page = document.Pages.AddPage();
44+
FixedContentEditor editor = new FixedContentEditor(page);
45+
double top = 5;
46+
double left = 5;
47+
string[] questions = new string[] { "Question 1", "Question 2", "Question 3" };
48+
Table questionnaireTable = new Table();
49+
RgbColor bordersColor = new RgbColor(255, 0, 0);
50+
int thickness = 1;
51+
Border border = new Border(thickness, Telerik.Windows.Documents.Fixed.Model.Editing.BorderStyle.Single, bordersColor);
52+
TableCellBorders tableCellsBorder = new TableCellBorders(border, border, border, border, null, null);
53+
questionnaireTable.Borders = new TableBorders(border);
54+
questionnaireTable.DefaultCellProperties.Borders = tableCellsBorder;
55+
questionnaireTable.DefaultCellProperties.Padding = new Thickness(thickness);
56+
for (int i = 0; i < questions.Length; i++)
57+
{
58+
string question = questions[i];
59+
TableRow questionRow = questionnaireTable.Rows.AddTableRow();
60+
61+
// 1. Caption text
62+
TableCell captionCell = questionRow.Cells.AddTableCell();
63+
captionCell.PreferredWidth = 100;
64+
captionCell.Blocks.AddBlock().InsertText(question);
65+
captionCell.Blocks.AddBlock().InsertText(string.Empty);
66+
captionCell.Blocks.AddBlock().InsertText(string.Empty);
67+
captionCell.Borders = tableCellsBorder;
68+
int rowHeight = 0;
69+
foreach (Block b in captionCell.Blocks)
70+
{
71+
rowHeight += (int)b.Measure().Height;
72+
}
73+
// 2. Radio button field
74+
TableCell radioButtonCell = questionRow.Cells.AddTableCell();
75+
radioButtonCell.PreferredWidth = 200;
76+
RadioButtonField answerRadioButtonField = new RadioButtonField("RadioButton_" + question);
77+
answerRadioButtonField.Options.Add(new RadioOption("Yes"));
78+
answerRadioButtonField.Options.Add(new RadioOption("No"));
79+
answerRadioButtonField.Value = answerRadioButtonField.Options[1];
80+
81+
document.AcroForm.FormFields.Add(answerRadioButtonField);
82+
editor.Position.Translate((int)captionCell.PreferredWidth + 10, rowHeight * i + rowHeight / 2);
83+
foreach (RadioOption option in answerRadioButtonField.Options)
84+
{
85+
DrawNextWidgetWithDescription(editor, option.Value, (e) => e.DrawWidget(answerRadioButtonField, option, new Size(20, 20)));
86+
}
87+
88+
// 3. Textbox field
89+
TableCell commentCell = questionRow.Cells.AddTableCell();
90+
commentCell.PreferredWidth = 200;
91+
TextBoxField textBox = new TextBoxField("textBox_" + question);
92+
document.AcroForm.FormFields.Add(textBox);
93+
textBox.Value = "Sample comment...";
94+
editor.Position.Translate((int)captionCell.PreferredWidth + (int)radioButtonCell.PreferredWidth + 10, editor.Position.Matrix.OffsetY);
95+
DrawNextWidgetWithDescription(editor, string.Empty, (e) => e.DrawWidget(textBox, new Size((int)commentCell.PreferredWidth, rowHeight / 2)));
96+
}
97+
98+
editor.Position.Translate(top, left);
99+
questionnaireTable.Draw(editor, new Rect(left, top, page.Size.Width, page.Size.Height));
100+
101+
string fileName = $"{Guid.NewGuid()}.pdf";
102+
File.Delete(fileName);
103+
PdfFormatProvider provider = new PdfFormatProvider();
104+
using Stream stream = File.OpenWrite(fileName);
105+
provider.Export(document, stream);
106+
107+
Process.Start(new ProcessStartInfo() { UseShellExecute = true, FileName = fileName });
108+
}
109+
110+
private static void DrawNextWidgetWithDescription(FixedContentEditor editor, string description, Action<FixedContentEditor> drawWidgetWithEditor)
111+
{
112+
double padding = 10;
113+
drawWidgetWithEditor(editor);
114+
Size annotationSize = editor.Root.Annotations[editor.Root.Annotations.Count - 1].Rect.Size;
115+
double x = editor.Position.Matrix.OffsetX;
116+
double y = editor.Position.Matrix.OffsetY;
117+
118+
Block block = new Block();
119+
block.TextProperties.FontSize = 18;
120+
block.VerticalAlignment = Telerik.Windows.Documents.Fixed.Model.Editing.Flow.VerticalAlignment.Center;
121+
block.InsertText(description);
122+
editor.Position.Translate(x + annotationSize.Width, y);
123+
editor.DrawBlock(block, new Size(editor.Root.Size.Width, annotationSize.Height));
124+
125+
editor.Position.Translate(x + block.Measure().Width + annotationSize.Width + padding * 2, y);
126+
}
127+
```
128+
129+
## See Also
130+
131+
- [Create Interactive Forms SDK Example](https://github.com/telerik/document-processing-sdk/tree/master/PdfProcessing/CreateInteractiveForms)
132+
- [Modify Form Values SDK Example](https://github.com/telerik/document-processing-sdk/tree/master/PdfProcessing/ModifyForms)
133+
134+
---

0 commit comments

Comments
 (0)