使用iTextSharp在pdf文件中添加按钮

本文关键字:添加 按钮 文件 pdf iTextSharp 使用 | 更新日期: 2023-09-27 18:23:40

我想使用itextsharp在现有的pdf中添加一个按钮。我找到了添加文本字段的示例,但我找不到任何在pdf文件中的特定位置添加按钮的示例。

使用iTextSharp在pdf文件中添加按钮

使用PdfStamper将PushbuttonField添加到现有PDF中。指定页面上的位置、字段名称和页码。

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();
    }
}

您还应该看看iText的实际操作。第8章提供了创建PushbuttonField的示例以及您可以设置的许多不同属性。