양식 필드 이름 식별

Aspose.PDF for .NET은 이미 생성된 PDF 양식을 만들고, 편집하고, 채울 수 있는 기능을 제공합니다. Aspose.Pdf.Facades 네임스페이스에는 Form 클래스가 포함되어 있으며, 이 클래스에는 FillField라는 함수가 있어 두 개의 인수, 즉 필드 이름과 필드 값을 받습니다. 따라서 양식 필드를 채우기 위해서는 정확한 양식 필드 이름을 알고 있어야 합니다.

구현 세부사항

우리는 종종 Adobe Designer와 같은 도구에서 생성된 양식을 채워야 하는 상황에 직면하게 되며, 이때 양식 필드 이름이 무엇인지 확실하지 않을 수 있습니다. 따라서 양식 필드를 채우기 위해 먼저 모든 PDF 양식 필드의 이름을 읽어야 합니다. Form 클래스는 모든 필드의 이름을 반환하는 FieldNames라는 속성을 제공하며, PDF에 필드가 없으면 null을 반환합니다. 그러나 이 속성을 사용할 때 PDF 양식의 모든 필드 이름을 얻지만, 어떤 이름이 어떤 필드에 해당하는지 확실하지 않을 수 있습니다.

이 문제에 대한 해결책으로 각 필드의 외관 속성을 사용할 것입니다. Form 클래스에는 GetFieldFacade라는 함수가 있어 위치, 색상, 테두리 스타일, 글꼴, 목록 항목 등을 포함한 속성을 반환합니다. 이러한 값을 저장하기 위해 FormFieldFacade 클래스를 사용해야 하며, 이 클래스는 필드의 시각적 속성을 기록하는 데 사용됩니다. 이러한 속성을 얻으면 각 필드 아래에 필드 이름을 표시하는 텍스트 필드를 추가할 수 있습니다.

Aspose.Pdf.Facades 네임스페이스에는 PDF 양식을 조작할 수 있는 FormEditor라는 클래스가 있습니다. PDF 양식을 열고, 기존 양식 필드 아래에 텍스트 필드를 추가하고, 새 이름으로 PDF 양식을 저장합니다.

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void IdentifyFormFieldsNames()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdfFacades_TechnicalArticles();
    // First a input pdf file should be assigned
    var form = new Aspose.Pdf.Facades.Form(dataDir + "FilledForm.pdf");
    // Get all field names
    var allfields = form.FieldNames;
    // Create an array which will hold the location coordinates of Form fields
    var box = new System.Drawing.Rectangle[allfields.Length];
    for (int i = 0; i < allfields.Length; i++)
    {
        // Get the appearance attributes of each field, consequtively
        var facade = form.GetFieldFacade(allfields[i]);
        // Box in FormFieldFacade class holds field's location
        box[i] = facade.Box;
    }
    // Save PDF document
    form.Save(dataDir + "IdentifyFormFields_1_out.pdf");

    // Create PDF document
    using (var document = new Aspose.Pdf.Document(dataDir + "FilledForm - 2.pdf"))
    {
        // Now we need to add a textfield just upon the original one
        using (var editor = new Aspose.Pdf.Facades.FormEditor(document))
        {
            for (int i = 0; i < allfields.Length; i++)
            {
                // Add text field beneath every existing form field
                editor.AddField(Aspose.Pdf.Facades.FieldType.Text, 
                "TextField" + i, allfields[i], 1, 
                box[i].Left, box[i].Top, box[i].Left + 50, box[i].Top + 10);
            }
            // Save PDF document
            editor.Save(dataDir + "IdentifyFormFields_out.pdf");
        }
    }
}