PDF 양식 필드 추가

기존 PDF 파일에 양식 필드 추가

기존 PDF 파일에 양식 필드를 추가하려면 FormEditor 클래스의 AddField 메서드를 사용해야 합니다. 이 메서드는 추가하려는 필드의 유형과 필드의 이름 및 위치를 지정해야 합니다. 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");
    }
}