class PdfInteractiveFormCheckBoxFieldExample
{
/// <summary>
/// Creates a PDF document with the check box fields.
/// </summary>
/// <param name="filename">The filename.</param>
public static void CreateDocumentWithCheckBoxField(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);
float fontSize = 20;
float width = fontSize * 3f / 2f;
float height = width;
// create a rectangle that defines check box 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 check box
Vintasoft.Imaging.Pdf.Tree.InteractiveForms.PdfInteractiveFormCheckBoxField checkBox1 =
new Vintasoft.Imaging.Pdf.Tree.InteractiveForms.PdfInteractiveFormCheckBoxField(document, "CheckBox1", rect);
// create a rectangle that defines size of the check box content
System.Drawing.RectangleF contentRect = new System.Drawing.RectangleF(0, 0, rect.Width, rect.Height);
// get a reference to "ZapfDingbats" font
Vintasoft.Imaging.Pdf.Tree.Fonts.PdfFont font = document.FontManager.GetStandardFont(
Vintasoft.Imaging.Pdf.Tree.Fonts.PdfStandardFontType.ZapfDingbats);
// create "Yes" appearance of checkbox1
using (Vintasoft.Imaging.Pdf.Drawing.PdfGraphics g = checkBox1.CreateYesAppearanceGraphics())
{
g.DrawString("o", font, fontSize, new Vintasoft.Imaging.Pdf.Drawing.PdfBrush(System.Drawing.Color.Black),
contentRect, Vintasoft.Imaging.Pdf.Drawing.PdfContentAlignment.Center, false);
g.DrawString("4", font, fontSize, new Vintasoft.Imaging.Pdf.Drawing.PdfBrush(System.Drawing.Color.Green),
contentRect, Vintasoft.Imaging.Pdf.Drawing.PdfContentAlignment.Center, false);
}
// create "Off" appearance of checkbox1
using (Vintasoft.Imaging.Pdf.Drawing.PdfGraphics g = checkBox1.CreateOffAppearanceGraphics())
{
g.DrawString("o", font, fontSize, new Vintasoft.Imaging.Pdf.Drawing.PdfBrush(System.Drawing.Color.Black),
contentRect, Vintasoft.Imaging.Pdf.Drawing.PdfContentAlignment.Center, false);
}
// set value of checkbox1
checkBox1.Value = "Off";
// change the vertical position of rectangle that defines check box position on PDF page
rect.Y -= rect.Height;
// create the second check box
Vintasoft.Imaging.Pdf.Tree.InteractiveForms.PdfInteractiveFormCheckBoxField checkBox2 =
new Vintasoft.Imaging.Pdf.Tree.InteractiveForms.PdfInteractiveFormCheckBoxField(document, "CheckBox2", rect);
// use checkbox1 appearance as checkbox2 appearance
checkBox2.Annotation.Appearances = checkBox1.Annotation.Appearances;
// set value of checkBox2
checkBox2.Value = "Yes";
// add the check box fields to the interactive form of document
document.InteractiveForm.Fields.Add(checkBox1);
document.InteractiveForm.Fields.Add(checkBox2);
// add annotations, associated with check box fields, to the page
page.Annotations = new Vintasoft.Imaging.Pdf.Tree.Annotations.PdfAnnotationList(document);
page.Annotations.Add(checkBox1.Annotation);
page.Annotations.Add(checkBox2.Annotation);
// save the document
document.Save(filename);
}
}
}