Skip to content

Commit 658e926

Browse files
Merge pull request #464 from telerik/new-kb-insert-form-xobject-elements-pdf-table-cell-02e030e87e20432fa8a0d3d36410e8ce
Added new kb article insert-form-xobject-elements-pdf-table-cell
2 parents 542da2b + 8d23633 commit 658e926

File tree

4 files changed

+138
-0
lines changed

4 files changed

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

libraries/radpdfprocessing/editing/tablecell.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,4 @@ The result from __Example 3__ is illustrated on __Figure 1__.
105105
* [TableRow]({%slug radpdfprocessing-editing-tablerow%})
106106
* [Block]({%slug radpdfprocessing-editing-block%})
107107
* [How to Generate a Table with Images with PdfProcessing]({%slug generate-table-with-images-pdf-processing%})
108+
* [Creating a PDF Table with Form Fields Inside the Cells]({%slug insert-form-xobject-elements-pdf-table-cell%})

libraries/radpdfprocessing/model/interactive-forms/form-fields/formfields.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,4 @@ When merging documents that contain FormFields you need to ensure that each fiel
145145
* [AcroForm]({%slug radpdfprocessing-model-interactive-forms-acroform %})
146146
* [Create Interactive Forms SDK example](https://github.com/telerik/document-processing-sdk/tree/master/PdfProcessing/CreateInteractiveForms)
147147
* [Modifying Forms SDK example](https://github.com/telerik/document-processing-sdk/tree/master/PdfProcessing/ModifyForms)
148+
* [Creating a PDF Table with Form Fields Inside the Cells]({%slug insert-form-xobject-elements-pdf-table-cell%})

0 commit comments

Comments
 (0)