VintaSoft Imaging .NET SDK 15.1: Документация для .NET разработчика
В этом разделе
PDF: Добавление группы радиокнопок в интерактивную форму PDF
В этом разделе
Вот C#/VB.NET код, который демонстрирует, как создать группу радиокнопок и добавить ее в интерактивную форму PDF документа:
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);
        }
    }
}
Class PdfInteractiveFormRadioButtonGroupFieldExample
    ''' <summary>
    ''' Creates a PDF document with radio button group fields.
    ''' </summary>
    ''' <param name="filename">The filename.</param>
    Public Shared Sub CreateDocumentWithRadioButtonGroupField(filename As String)
        ' create PDF document
        Using document As 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
            Dim page As New Vintasoft.Imaging.Pdf.Tree.PdfPage(document, Vintasoft.Imaging.PaperSizeKind.A4)
            ' add page to the document
            document.Pages.Add(page)

            ' "ZapfDingbats" font
            Dim fontSize As Single = 15
            Dim font As Vintasoft.Imaging.Pdf.Tree.Fonts.PdfFont = document.FontManager.GetStandardFont(Vintasoft.Imaging.Pdf.Tree.Fonts.PdfStandardFontType.ZapfDingbats)

            Dim width As Single = 100
            Dim height As Single = 30
            ' create a rectangle that defines the first radio button group position on PDF page
            Dim rect As New System.Drawing.RectangleF((page.Size.Width - width) / 2, ((page.Size.Height - height) / 3) * 2, width, height)

            ' create the first radio button group
            Dim radioGroup1 As 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
            Dim radioGroup2 As 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)
        End Using
    End Sub

    ''' <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 Shared Sub AddSimpleRadioButtons(radioButtonGroup As Vintasoft.Imaging.Pdf.Tree.InteractiveForms.PdfInteractiveFormRadioButtonGroupField, font As Vintasoft.Imaging.Pdf.Tree.Fonts.PdfFont, fontSize As Single, rect As System.Drawing.RectangleF, onColor As System.Drawing.Color, ParamArray values As String())
        Dim offBrush As New Vintasoft.Imaging.Pdf.Drawing.PdfBrush(System.Drawing.Color.Black)
        Dim onBrush As New Vintasoft.Imaging.Pdf.Drawing.PdfBrush(onColor)

        ' for each value
        For i As Integer = 0 To values.Length - 1
            ' create a rectangle that defines radio button position on PDF page
            Dim radioButtonRect As New System.Drawing.RectangleF(rect.X + i * rect.Width / values.Length, rect.Y, rect.Width / values.Length, rect.Height)

            ' create a radio button
            Dim radioButton As 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 g As Vintasoft.Imaging.Pdf.Drawing.PdfGraphics = 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)
            End Using
            ' create "Off" button appearance
            Using g As Vintasoft.Imaging.Pdf.Drawing.PdfGraphics = radioButton.CreateOffAppearanceGraphics()
                g.DrawString("m", font, fontSize, offBrush, radioButtonRect, Vintasoft.Imaging.Pdf.Drawing.PdfContentAlignment.Center, _
                    False)
            End Using

            ' add new radio button to radio button group
            radioButtonGroup.Kids.Add(radioButton)
        Next
    End Sub
End Class