添加 PDF 表单字段

在现有 PDF 文件中添加表单字段

为了在现有 PDF 文件中添加表单字段,您需要使用 AddField 方法的 FormEditor 类。此方法要求您指定要添加的字段类型,以及字段的名称和位置。您需要创建一个 FormEditor 类的对象,使用 AddField 方法在 PDF 中添加新字段。此外,您可以使用 SetFieldLimit 方法指定字段中的字符数限制,最后使用 Save 方法保存更新后的 PDF 文件。以下代码片段演示了如何在现有 PDF 文件中添加表单字段。

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void AddField()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf();

    // Create an instance of FormEditor to manipulate form fields
    using (var editor = new Aspose.Pdf.Facades.FormEditor())
    {
        // Bind PDF document
        editor.BindPdf(dataDir + "Sample-Form-01.pdf");

        // Add a text field named "Country" to the first page of the PDF
        // Specify the coordinates of the field (left, bottom, right, top)
        editor.AddField(Aspose.Pdf.Facades.FieldType.Text, "Country", 1, 232.56f, 496.75f, 352.28f, 514.03f);

        // Set a character limit for the "Country" field to 20 characters
        editor.SetFieldLimit("Country", 20);

        // Save PDF document
        editor.Save(dataDir + "Sample-Form-01-mod.pdf");
    }
}

在现有 PDF 文件中添加提交按钮 URL

AddSubmitBtn 方法允许您在 PDF 文件中设置提交按钮的 URL。这是提交按钮被点击时数据发布的 URL。在我们的示例代码中,我们指定 URL、字段名称、要添加的页面编号以及按钮放置坐标。AddSubmitBtn 方法需要提交按钮字段的名称和 URL。此方法由 FormEditor 类提供。以下代码片段演示了如何设置提交按钮 URL。

 // For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
 private static void AddSubmitBtn()
 {
     // The path to the documents directory
     var dataDir = RunExamples.GetDataDir_AsposePdf();

     // Create an instance of FormEditor to manipulate form fields
     using (var editor = new Aspose.Pdf.Facades.FormEditor())
     {
         // Bind PDF document
         editor.BindPdf(dataDir + "Sample-Form-01.pdf");

         // Add a submit button named "Submit" to the first page of the PDF
         // Specify the button text ("Submit"), the URL to which the form data will be submitted,
         // and the coordinates of the button (left, bottom, right, top)
         editor.AddSubmitBtn("Submit", 1, "Submit", "http://localhost:3000", 232.56f, 466.75f, 352.28f, 484.03f);

         // Save PDF document
         editor.Save(dataDir + "Sample-Form-01-mod.pdf");
     }
 }

为按钮添加 JavaScript

AddFieldScript 方法允许您在 PDF 文件中的按钮上添加 JavaScript。此方法需要按钮的名称和 JavaScript。此方法由 FormEditor 类提供。以下代码片段演示了如何为按钮设置 JavaScript。

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void AddFieldScript()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf();

    // Create an instance of FormEditor to manipulate form fields
    using (var editor = new Aspose.Pdf.Facades.FormEditor())
    {
        // Bind PDF document
        editor.BindPdf(dataDir + "Sample-Form-01.pdf");

        // Add a JavaScript action to the field named "Last Name"
        // The script displays an alert box with the message "Only one last name"
        editor.AddFieldScript("Last Name", "app.alert(\"Only one last name\",3);");

        // Save PDF document
        editor.Save(dataDir + "Sample-Form-01-mod.pdf");
    }
}