VintaSoft Imaging .NET SDK 15.1: Документация для .NET разработчика
В этом разделе
PDF: Добавление поля с флажком в интерактивную форму PDF
В этом разделе
Вот C#/VB.NET код, который демонстрирует, как создать поле с флажком и добавить его в интерактивную форму PDF документа:
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);
        }
    }
}
Class PdfInteractiveFormCheckBoxFieldExample
    ''' <summary>
    ''' Creates a PDF document with the check box fields.
    ''' </summary>
    ''' <param name="filename">The filename.</param>
    Public Shared Sub CreateDocumentWithCheckBoxField(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)

            Dim fontSize As Single = 20
            Dim width As Single = fontSize * 3F / 2F
            Dim height As Single = width
            ' create a rectangle that defines check box 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 check box
            Dim checkBox1 As New Vintasoft.Imaging.Pdf.Tree.InteractiveForms.PdfInteractiveFormCheckBoxField(document, "CheckBox1", rect)
            ' create a rectangle that defines size of the check box content
            Dim contentRect As New System.Drawing.RectangleF(0, 0, rect.Width, rect.Height)
            ' get a reference to "ZapfDingbats" font
            Dim font As Vintasoft.Imaging.Pdf.Tree.Fonts.PdfFont = document.FontManager.GetStandardFont(Vintasoft.Imaging.Pdf.Tree.Fonts.PdfStandardFontType.ZapfDingbats)
            ' create "Yes" appearance of checkbox1
            Using g As Vintasoft.Imaging.Pdf.Drawing.PdfGraphics = 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)
            End Using
            ' create "Off" appearance of checkbox1
            Using g As Vintasoft.Imaging.Pdf.Drawing.PdfGraphics = 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)
            End Using
            ' 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
            Dim checkBox2 As 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)
        End Using
    End Sub
End Class