class PdfInteractiveFormRadioButtonGroupFieldExample
{
/// <summary>
/// Creates a PDF document with radio button group fields.
/// </summary>
/// <param name="filename">The filename.</param>
public static void CreateDocumentWithRadioButtonGroupField(string filename)
{
// create PDF document
using (Vintasoft.Imaging.Pdf.PdfDocument document = new Vintasoft.Imaging.Pdf.PdfDocument())
{
// create interactive form in PDF document
document.InteractiveForm = new Vintasoft.Imaging.Pdf.Tree.InteractiveForms.PdfDocumentInteractiveForm(document);
// specify that the viewer application must construct appearance streams and
// appearance properties for all widget annotations
document.InteractiveForm.NeedAppearances = true;
// create an empty page
Vintasoft.Imaging.Pdf.Tree.PdfPage page = new Vintasoft.Imaging.Pdf.Tree.PdfPage(
document, Vintasoft.Imaging.PaperSizeKind.A4);
// add page to the document
document.Pages.Add(page);
// "ZapfDingbats" font
float fontSize = 15;
Vintasoft.Imaging.Pdf.Tree.Fonts.PdfFont font = document.FontManager.GetStandardFont(
Vintasoft.Imaging.Pdf.Tree.Fonts.PdfStandardFontType.ZapfDingbats);
float width = 100;
float height = 30;
// create a rectangle that defines the first radio button group position on PDF page
System.Drawing.RectangleF rect = new System.Drawing.RectangleF(
(page.Size.Width - width) / 2,
((page.Size.Height - height) / 3) * 2,
width, height);
// create the first radio button group
Vintasoft.Imaging.Pdf.Tree.InteractiveForms.PdfInteractiveFormRadioButtonGroupField radioGroup1 =
new Vintasoft.Imaging.Pdf.Tree.InteractiveForms.PdfInteractiveFormRadioButtonGroupField(document, "RadioGroup1");
// add radio buttons to the radio button group
AddSimpleRadioButtons(radioGroup1, font, fontSize, rect, System.Drawing.Color.Green, "1", "2", "3", "4", "5");
// change the state of radio button with value "2" to the checked state
radioGroup1.CheckedAppearanceStateName = "2";
// create a rectangle that defines the second radio button group position on PDF page
rect = new System.Drawing.RectangleF(rect.X, rect.Y - rect.Height, rect.Width, rect.Height);
// create the second radio button group
Vintasoft.Imaging.Pdf.Tree.InteractiveForms.PdfInteractiveFormRadioButtonGroupField radioGroup2 =
new Vintasoft.Imaging.Pdf.Tree.InteractiveForms.PdfInteractiveFormRadioButtonGroupField(document, "RadioGroup2");
// add radio buttons to the radio button group
AddSimpleRadioButtons(radioGroup2, font, fontSize, rect, System.Drawing.Color.Red, "One", "Two", "Three");
// change the state of radio button with value "Two" to the checked state
radioGroup2.CheckedAppearanceStateName = "Two";
// add radio button groups to the interactive form of document
document.InteractiveForm.Fields.Add(radioGroup1);
document.InteractiveForm.Fields.Add(radioGroup2);
// add annotations, associated with radio button group fields, to the page
page.Annotations = new Vintasoft.Imaging.Pdf.Tree.Annotations.PdfAnnotationList(document);
page.Annotations.AddRange(radioGroup1.GetAnnotations());
page.Annotations.AddRange(radioGroup2.GetAnnotations());
// save the document
document.Save(filename);
}
}
/// <summary>
/// Adds simple radio buttons to a radio button group.
/// </summary>
/// <param name="radioButtonGroup">The parent radio button group.</param>
/// <param name="font">The font.</param>
/// <param name="fontSize">The font size.</param>
/// <param name="rect">Rectangle that defines radio button group position on PDF page.</param>
/// <param name="onColor">Button color in "on" state.</param>
/// <param name="values">The values of radio buttons.</param>
private static void AddSimpleRadioButtons(
Vintasoft.Imaging.Pdf.Tree.InteractiveForms.PdfInteractiveFormRadioButtonGroupField radioButtonGroup,
Vintasoft.Imaging.Pdf.Tree.Fonts.PdfFont font,
float fontSize,
System.Drawing.RectangleF rect,
System.Drawing.Color onColor,
params string[] values)
{
Vintasoft.Imaging.Pdf.Drawing.PdfBrush offBrush =
new Vintasoft.Imaging.Pdf.Drawing.PdfBrush(System.Drawing.Color.Black);
Vintasoft.Imaging.Pdf.Drawing.PdfBrush onBrush =
new Vintasoft.Imaging.Pdf.Drawing.PdfBrush(onColor);
// for each value
for (int i = 0; i < values.Length; i++)
{
// create a rectangle that defines radio button position on PDF page
System.Drawing.RectangleF radioButtonRect = new System.Drawing.RectangleF(
rect.X + i * rect.Width / values.Length, rect.Y, rect.Width / values.Length, rect.Height);
// create a radio button
Vintasoft.Imaging.Pdf.Tree.InteractiveForms.PdfInteractiveFormRadioButtonField radioButton =
new Vintasoft.Imaging.Pdf.Tree.InteractiveForms.PdfInteractiveFormRadioButtonField(
radioButtonGroup.Document, radioButtonRect);
// create a rectangle that defines size of the radio button content
radioButtonRect = new System.Drawing.RectangleF(0, 0, radioButtonRect.Width, radioButtonRect.Height);
// create ON button appearance
using (Vintasoft.Imaging.Pdf.Drawing.PdfGraphics g = radioButton.CreateAppearanceGraphics(values[i]))
{
g.DrawString("m", font, fontSize, offBrush, radioButtonRect,
Vintasoft.Imaging.Pdf.Drawing.PdfContentAlignment.Center, false);
g.DrawString("l", font, fontSize, onBrush, radioButtonRect,
Vintasoft.Imaging.Pdf.Drawing.PdfContentAlignment.Center, false);
}
// create "Off" button appearance
using (Vintasoft.Imaging.Pdf.Drawing.PdfGraphics g = radioButton.CreateOffAppearanceGraphics())
{
g.DrawString("m", font, fontSize, offBrush, radioButtonRect,
Vintasoft.Imaging.Pdf.Drawing.PdfContentAlignment.Center, false);
}
// add new radio button to radio button group
radioButtonGroup.Kids.Add(radioButton);
}
}
}