Lipika
Lipika

Reputation: 375

Adding button in a pdf file using iTextSharp

I want to add a button in an existing pdf using itextsharp. I found examples to add text field but I am not able to get any example to add a button at a particular location in a pdf file.

Upvotes: 1

Views: 4522

Answers (1)

Mathew Leger
Mathew Leger

Reputation: 1633

Use the PdfStamper to add a PushbuttonField to an existing PDF. Specify a position on a page, a name for the field, and the page number.

static void AddPushbuttonField(string inputFile, iTextSharp.text.Rectangle buttonPosition, string buttonName, string outputFile)
{
    using (PdfStamper stamper = new PdfStamper(new PdfReader(inputFile), File.Create(outputFile)))
    {
        PushbuttonField buttonField = new PushbuttonField(stamper.Writer, buttonPosition, buttonName);

        stamper.AddAnnotation(buttonField.Field, 1);
        stamper.Close();
    }
}

You should also take a look at iText in Action. Chapter 8 has examples of creating a PushbuttonField and many of the different properties you can set.

Upvotes: 3

Related Questions