Creating a PDF Table with Form Fields Inside the Cells
Environment
Version | Product | Author |
---|---|---|
2024.3.806 | RadPdfProcessing | Desislava Yordanova |
Description
Learn how to insert interactive form fields such as radio buttons and textboxes into table cells in a PDF document using RadPdfProcessing.
Solution
To insert interactive form fields like radio buttons and textboxes into table cells in a PDF document, follow the steps below:
- Create a new RadFixedDocument and add a page to it.
- Initialize a FixedContentEditor on the page for drawing content.
- Define a Table object and configure its properties, including borders and cell padding.
- For each row in the table:
- Add a text cell with the caption using InsertText.
- Create a RadioButtonField , configure its options, and draw it in a table cell.
- Create a TextBoxField, set its value, and draw it in another table cell.
-
Export the
RadFixedDocument
to a PDF file.
Below is a simplified code snippet demonstrating these steps:
private readonly string[] _questions = new string[] { "Question 1", "Question 2", "Question 3" };
public static void Main(string[] args)
{
RadFixedDocument document = new RadFixedDocument();
RadFixedPage page = document.Pages.AddPage();
FixedContentEditor editor = new FixedContentEditor(page);
double top = 5;
double left = 5;
string[] questions = new string[] { "Question 1", "Question 2", "Question 3" };
Table questionnaireTable = new Table();
RgbColor bordersColor = new RgbColor(255, 0, 0);
int thickness = 1;
Border border = new Border(thickness, Telerik.Windows.Documents.Fixed.Model.Editing.BorderStyle.Single, bordersColor);
TableCellBorders tableCellsBorder = new TableCellBorders(border, border, border, border, null, null);
questionnaireTable.Borders = new TableBorders(border);
questionnaireTable.DefaultCellProperties.Borders = tableCellsBorder;
questionnaireTable.DefaultCellProperties.Padding = new Thickness(thickness);
for (int i = 0; i < questions.Length; i++)
{
string question = questions[i];
TableRow questionRow = questionnaireTable.Rows.AddTableRow();
// 1. Caption text
TableCell captionCell = questionRow.Cells.AddTableCell();
captionCell.PreferredWidth = 100;
captionCell.Blocks.AddBlock().InsertText(question);
captionCell.Blocks.AddBlock().InsertText(string.Empty);
captionCell.Blocks.AddBlock().InsertText(string.Empty);
captionCell.Borders = tableCellsBorder;
int rowHeight = 0;
foreach (Block b in captionCell.Blocks)
{
rowHeight += (int)b.Measure().Height;
}
// 2. Radio button field
TableCell radioButtonCell = questionRow.Cells.AddTableCell();
radioButtonCell.PreferredWidth = 200;
RadioButtonField answerRadioButtonField = new RadioButtonField("RadioButton_" + question);
answerRadioButtonField.Options.Add(new RadioOption("Yes"));
answerRadioButtonField.Options.Add(new RadioOption("No"));
answerRadioButtonField.Value = answerRadioButtonField.Options[1];
document.AcroForm.FormFields.Add(answerRadioButtonField);
editor.Position.Translate((int)captionCell.PreferredWidth + 10, rowHeight * i + rowHeight / 2);
foreach (RadioOption option in answerRadioButtonField.Options)
{
DrawNextWidgetWithDescription(editor, option.Value, (e) => e.DrawWidget(answerRadioButtonField, option, new Size(20, 20)));
}
// 3. Textbox field
TableCell commentCell = questionRow.Cells.AddTableCell();
commentCell.PreferredWidth = 200;
TextBoxField textBox = new TextBoxField("textBox_" + question);
document.AcroForm.FormFields.Add(textBox);
textBox.Value = "Sample comment...";
editor.Position.Translate((int)captionCell.PreferredWidth + (int)radioButtonCell.PreferredWidth + 10, editor.Position.Matrix.OffsetY);
DrawNextWidgetWithDescription(editor, string.Empty, (e) => e.DrawWidget(textBox, new Size((int)commentCell.PreferredWidth, rowHeight / 2)));
}
editor.Position.Translate(top, left);
questionnaireTable.Draw(editor, new Rect(left, top, page.Size.Width, page.Size.Height));
string fileName = $"{Guid.NewGuid()}.pdf";
File.Delete(fileName);
PdfFormatProvider provider = new PdfFormatProvider();
using Stream stream = File.OpenWrite(fileName);
provider.Export(document, stream);
Process.Start(new ProcessStartInfo() { UseShellExecute = true, FileName = fileName });
}
private static void DrawNextWidgetWithDescription(FixedContentEditor editor, string description, Action<FixedContentEditor> drawWidgetWithEditor)
{
double padding = 10;
drawWidgetWithEditor(editor);
Size annotationSize = editor.Root.Annotations[editor.Root.Annotations.Count - 1].Rect.Size;
double x = editor.Position.Matrix.OffsetX;
double y = editor.Position.Matrix.OffsetY;
Block block = new Block();
block.TextProperties.FontSize = 18;
block.VerticalAlignment = Telerik.Windows.Documents.Fixed.Model.Editing.Flow.VerticalAlignment.Center;
block.InsertText(description);
editor.Position.Translate(x + annotationSize.Width, y);
editor.DrawBlock(block, new Size(editor.Root.Size.Width, annotationSize.Height));
editor.Position.Translate(x + block.Measure().Width + annotationSize.Width + padding * 2, y);
}