VintaSoft Imaging .NET SDK 12.1: Documentation for .NET developer
In This Topic
    PDF: Add a button with "Import" action to a PDF interactive form
    In This Topic
    Here is an example that demonstrates how to create button that imports data of PDF interactive form from the specified file:
    /// <summary>
    /// Adds a form import button to a PDF document.
    /// </summary>
    /// <param name="pdfFilename">The filename of PDF document.</param>
    /// <param name="loadFilename">The filename of import data file.</param>
    public static void AddFormImportButton(string pdfFilename, string loadFilename)
    {
        // open PDF document
        using (Vintasoft.Imaging.Pdf.PdfDocument document =
            new Vintasoft.Imaging.Pdf.PdfDocument(pdfFilename))
        {
            // create PDF page
            Vintasoft.Imaging.Pdf.Tree.PdfPage page = new Vintasoft.Imaging.Pdf.Tree.PdfPage(
                document, Vintasoft.Imaging.PaperSizeKind.A6);
            // add page to the PDF document
            document.Pages.Add(page);
    
            // create a button and add button to PDF page
            Vintasoft.Imaging.Pdf.Tree.InteractiveForms.PdfInteractiveFormPushButtonField button =
                CreateButtonAndAddToPdfPage(page, "Load form");
    
            // create reference to a file with data that should be imported
            Vintasoft.Imaging.Pdf.Tree.PdfFileReferenceSpecification fileReference =
                new Vintasoft.Imaging.Pdf.Tree.PdfFileReferenceSpecification(document, loadFilename);
    
            // create the form import action
            Vintasoft.Imaging.Pdf.Tree.InteractiveForms.PdfInteractiveFormImportDataAction action =
                new Vintasoft.Imaging.Pdf.Tree.InteractiveForms.PdfInteractiveFormImportDataAction(document, fileReference);
            // specify that all fields of document must be imported
            action.Fields = document.InteractiveForm.Fields;
    
            // set the button action
            button.Annotation.ActivateAction = action;
    
            // save changes to a file
            document.SaveChanges();
        }
    }
    
    /// <summary>
    /// Creates button and adds it to PDF page.
    /// </summary>
    /// <param name="page">PDF page.</param>
    /// <param name="caption">Caption of button.</param>
    public static Vintasoft.Imaging.Pdf.Tree.InteractiveForms.PdfInteractiveFormPushButtonField CreateButtonAndAddToPdfPage(
        Vintasoft.Imaging.Pdf.Tree.PdfPage page, string caption)
    {
        // PDF document
        Vintasoft.Imaging.Pdf.PdfDocument document = page.Document;
    
        // button position on PDF page
        System.Drawing.RectangleF rect = new System.Drawing.RectangleF(50, page.Size.Height - 150, 200, 100);
    
        // create button
        Vintasoft.Imaging.Pdf.Tree.InteractiveForms.PdfInteractiveFormPushButtonField button =
            new Vintasoft.Imaging.Pdf.Tree.InteractiveForms.PdfInteractiveFormPushButtonField(document, "button", rect);
        // set the border style
        button.Annotation.BorderStyle = new Vintasoft.Imaging.Pdf.Tree.Annotations.PdfAnnotationBorderStyle(document);
        button.Annotation.BorderStyle.Style = Vintasoft.Imaging.Pdf.Tree.Annotations.PdfAnnotationBorderStyleType.Beveled;
        button.Annotation.BorderStyle.Width = 2;
        // set the appearance characteristics
        button.Annotation.AppearanceCharacteristics =
            new Vintasoft.Imaging.Pdf.Tree.Annotations.PdfAnnotationAppearanceCharacteristics(document);
        button.Annotation.AppearanceCharacteristics.BackgroundColor = System.Drawing.Color.LightGray;
        button.Annotation.AppearanceCharacteristics.ButtonNormalCaption = caption;
        // set the text default appearance
        Vintasoft.Imaging.Pdf.Tree.Fonts.PdfFont font = document.FontManager.GetStandardFont(
            Vintasoft.Imaging.Pdf.Tree.Fonts.PdfStandardFontType.TimesRoman);
        button.SetTextDefaultAppearance(font, 15f, System.Drawing.Color.Black);
    
        // if PDF document does NOT have interactive form
        if (document.InteractiveForm == null)
            // create interactive form in PDF document
            document.InteractiveForm = new Vintasoft.Imaging.Pdf.Tree.InteractiveForms.PdfDocumentInteractiveForm(document);
        // add button to the interactive form of PDF document
        document.InteractiveForm.Fields.Add(button);
    
        // if PDF page does NOT have annotation list
        if (page.Annotations == null)
            // create annotation list for PDF page
            page.Annotations = new Vintasoft.Imaging.Pdf.Tree.Annotations.PdfAnnotationList(document);
        // add the button annotation to PDF page
        page.Annotations.Add(button.Annotation);
    
        // update field appearance
        button.UpdateAppearance();
    
        // return the button
        return button;
    }
    
    ''' <summary>
    ''' Adds a form import button to a PDF document.
    ''' </summary>
    ''' <param name="pdfFilename">The filename of PDF document.</param>
    ''' <param name="loadFilename">The filename of import data file.</param>
    Public Shared Sub AddFormImportButton(pdfFilename As String, loadFilename As String)
        ' open PDF document
        Using document As New Vintasoft.Imaging.Pdf.PdfDocument(pdfFilename)
            ' create PDF page
            Dim page As New Vintasoft.Imaging.Pdf.Tree.PdfPage(document, Vintasoft.Imaging.PaperSizeKind.A6)
            ' add page to the PDF document
            document.Pages.Add(page)
    
            ' create a button and add button to PDF page
            Dim button As Vintasoft.Imaging.Pdf.Tree.InteractiveForms.PdfInteractiveFormPushButtonField = CreateButtonAndAddToPdfPage(page, "Load form")
    
            ' create reference to a file with data that should be imported
            Dim fileReference As New Vintasoft.Imaging.Pdf.Tree.PdfFileReferenceSpecification(document, loadFilename)
    
            ' create the form import action
            Dim action As New Vintasoft.Imaging.Pdf.Tree.InteractiveForms.PdfInteractiveFormImportDataAction(document, fileReference)
            ' specify that all fields of document must be imported
            action.Fields = document.InteractiveForm.Fields
    
            ' set the button action
            button.Annotation.ActivateAction = action
    
            ' save changes to a file
            document.SaveChanges()
        End Using
    End Sub
    
    ''' <summary>
    ''' Creates button and adds it to PDF page.
    ''' </summary>
    ''' <param name="page">PDF page.</param>
    ''' <param name="caption">Caption of button.</param>
    Public Shared Function CreateButtonAndAddToPdfPage(page As Vintasoft.Imaging.Pdf.Tree.PdfPage, caption As String) As Vintasoft.Imaging.Pdf.Tree.InteractiveForms.PdfInteractiveFormPushButtonField
        ' PDF document
        Dim document As Vintasoft.Imaging.Pdf.PdfDocument = page.Document
    
        ' button position on PDF page
        Dim rect As New System.Drawing.RectangleF(50, page.Size.Height - 150, 200, 100)
    
        ' create button
        Dim button As New Vintasoft.Imaging.Pdf.Tree.InteractiveForms.PdfInteractiveFormPushButtonField(document, "button", rect)
        ' set the border style
        button.Annotation.BorderStyle = New Vintasoft.Imaging.Pdf.Tree.Annotations.PdfAnnotationBorderStyle(document)
        button.Annotation.BorderStyle.Style = Vintasoft.Imaging.Pdf.Tree.Annotations.PdfAnnotationBorderStyleType.Beveled
        button.Annotation.BorderStyle.Width = 2
        ' set the appearance characteristics
        button.Annotation.AppearanceCharacteristics = New Vintasoft.Imaging.Pdf.Tree.Annotations.PdfAnnotationAppearanceCharacteristics(document)
        button.Annotation.AppearanceCharacteristics.BackgroundColor = System.Drawing.Color.LightGray
        button.Annotation.AppearanceCharacteristics.ButtonNormalCaption = caption
        ' set the text default appearance
        Dim font As Vintasoft.Imaging.Pdf.Tree.Fonts.PdfFont = document.FontManager.GetStandardFont(Vintasoft.Imaging.Pdf.Tree.Fonts.PdfStandardFontType.TimesRoman)
        button.SetTextDefaultAppearance(font, 15F, System.Drawing.Color.Black)
    
        ' if PDF document does NOT have interactive form
        If document.InteractiveForm Is Nothing Then
            ' create interactive form in PDF document
            document.InteractiveForm = New Vintasoft.Imaging.Pdf.Tree.InteractiveForms.PdfDocumentInteractiveForm(document)
        End If
        ' add button to the interactive form of PDF document
        document.InteractiveForm.Fields.Add(button)
    
        ' if PDF page does NOT have annotation list
        If page.Annotations Is Nothing Then
            ' create annotation list for PDF page
            page.Annotations = New Vintasoft.Imaging.Pdf.Tree.Annotations.PdfAnnotationList(document)
        End If
        ' add the button annotation to PDF page
        page.Annotations.Add(button.Annotation)
    
        ' update field appearance
        button.UpdateAppearance()
    
        ' return the button
        Return button
    End Function