Friday, 02 June 2017 07:50

Assign an Icon to a PDF Button Field and Set Icon Layout in C#

Written by  support iceblue
Rate this item
(0 votes)

In PDF, buttons can be assigned icon appearances and each button can have as many as three appearances: Normal, Rollover, and Click. With Spire.PDF, not only can we assign icons to buttons, but also we can change the icons of buttons. In this article, we’re going to show you how to assign an icon to a button and set icon layout using Spire.PDF and C#. As for changing icons, please refer How to change the image on button field in C#.

Code Snippets:

Step 1: Create a PDF document and add a page to it.

PdfDocument doc = new PdfDocument();
PdfPageBase page = doc.Pages.Add();

Step 2: Create a button field.

PdfButtonField btn = new PdfButtonField(page, "button1");
btn.Bounds = new RectangleF(0, 50, 120, 100);

Step 3: Set button layout.

btn.HighlightMode = PdfHighlightMode.Push;
btn.LayoutMode = PdfButtonLayoutMode.CaptionOverlayIcon;
//Set text and icon for Normal appearance
btn.Text = "Normal Text";
btn.Icon = PdfImage.FromFile("Image.png");
//Set text and icon for Click appearance (Can only be set when highlight mode is Push)
btn.AlternateText = "Alternate Text";
btn.AlternateIcon = PdfImage.FromFile("Sunflower.jpg");
//Set text and icon for Rollover appearance (Can only be set when highlight mode is Push)
btn.RolloverText = "Rollover Text";
btn.RolloverIcon = PdfImage.FromFile("PinkRoses.jpg");

Step 4: Set icon layout.

btn.IconLayout.Spaces = new float[] { 0.5f, 0.5f };
btn.IconLayout.ScaleMode = PdfButtonIconScaleMode.Proportional;
btn.IconLayout.ScaleReason = PdfButtonIconScaleReason.Always;
btn.IconLayout.IsFitBounds = false;

Step 5: Add the button to the document.

doc.Form.Fields.Add(btn);

Step 6: Save the document.

doc.SaveToFile("AddIcon.pdf");

The resultant document looks as follows:

Assign an Icon to a PDF Button Field and Set Icon Layout in C#

Full code:

using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Fields;
using Spire.Pdf.Graphics;

namespace Add_Icon_to_PDF_Button_Field
{
    class Program
    {
        static void Main(string[] args)
        {   
            //Create a PDF document       
            PdfDocument doc = new PdfDocument();
            PdfPageBase page = doc.Pages.Add();

            //Create a Button
            PdfButtonField btn = new PdfButtonField(page, "button1");
            btn.Bounds = new RectangleF(0, 50, 120, 100);
            btn.HighlightMode = PdfHighlightMode.Push;
            btn.LayoutMode = PdfButtonLayoutMode.CaptionOverlayIcon;
            //Set text and icon for Normal appearance
            btn.Text = "Normal Text";
            btn.Icon = PdfImage.FromFile("Image.png");
            //Set text and icon for Click appearance (Can only be set when highlight mode is Push)
            btn.AlternateText = "Alternate Text";
            btn.AlternateIcon = PdfImage.FromFile("Sunflower.jpg");
            //Set text and icon for Rollover appearance (Can only be set when highlight mode is Push)
            btn.RolloverText = "Rollover Text";
            btn.RolloverIcon = PdfImage.FromFile("PinkRoses.jpg");

            //Set icon layout
            btn.IconLayout.Spaces = new float[] { 0.5f, 0.5f };
            btn.IconLayout.ScaleMode = PdfButtonIconScaleMode.Proportional;
            btn.IconLayout.ScaleReason = PdfButtonIconScaleReason.Always;
            btn.IconLayout.IsFitBounds = false;

            //Add the button to the document
            doc.Form.Fields.Add(btn);

            //Save the document
            doc.SaveToFile("AddIcon.pdf");            
        }
    }
}

Additional Info

  • tutorial_title:
Last modified on Friday, 02 June 2017 08:00