AcroForm 추출 - C#에서 PDF의 양식 데이터 추출

다음 코드 스니펫은 Aspose.PDF.Drawing 라이브러리와 함께 작동합니다.

양식에서 데이터 추출

PDF 문서의 모든 필드에서 값 가져오기

PDF 문서의 모든 필드에서 값을 가져오려면 모든 양식 필드를 탐색한 다음 Value 속성을 사용하여 값을 가져와야 합니다. Form 컬렉션에서 각 필드를 가져오고, 기본 필드 유형인 Field에 접근하여 그 Value 속성에 접근합니다.

다음 C# 코드 스니펫은 PDF 문서의 모든 필드에서 값을 가져오는 방법을 보여줍니다.

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

    // Open PDF document
    using (var document = new Aspose.Pdf.Document(dataDir + "GetValuesFromAllFields.pdf"))
    {
        // Get values from all fields
        foreach (Aspose.Pdf.Forms.Field formField in document.Form)
        {
            Console.WriteLine("Field Name : {0} ", formField.PartialName);
            Console.WriteLine("Value : {0} ", formField.Value);
        }
    }
}

PDF 문서의 개별 필드에서 값 가져오기

양식 필드의 Value 속성을 사용하면 특정 필드의 값을 가져올 수 있습니다. 값을 가져오려면 Document 객체의 Form 컬렉션에서 양식 필드를 가져옵니다. 이 C# 예제는 TextBoxField를 선택하고 Value 속성을 사용하여 값을 검색합니다.

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

    // Open PDF document
    using (var document = new Aspose.Pdf.Document(dataDir + "GetValueFromField.pdf"))
    {
        // Get a field
        if (document.Form["textbox1"] is Aspose.Pdf.Forms.TextBoxField textBoxField)
        {
            // Get field value
            Console.WriteLine("PartialName : {0} ", textBoxField.PartialName);
            Console.WriteLine("Value : {0} ", textBoxField.Value);
        }
    }
}

제출 버튼의 URL을 가져오려면 다음 코드 줄을 사용하십시오.

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

    // Open PDF document
    using (var document = new Aspose.Pdf.Document(dataDir + "GetValueFromField.pdf"))
    {
        // Get the SubmitFormAction from the form field
        if (document.Form[1].OnActivated is Aspose.Pdf.Annotations.SubmitFormAction act)
        {
            // Output the URL of the SubmitFormAction
            Console.WriteLine(act.Url.Name);
        }
    }
}

PDF 파일의 특정 영역에서 양식 필드 가져오기

때때로 문서에서 양식 필드가 어디에 있는지 알 수 있지만 이름은 모를 수 있습니다. 예를 들어, 인쇄된 양식의 도면만 가지고 있는 경우입니다. Aspose.PDF for .NET를 사용하면 문제가 되지 않습니다. PDF 파일의 특정 영역에 어떤 필드가 있는지 확인할 수 있습니다. PDF 파일의 특정 영역에서 양식 필드를 가져오려면:

  1. Document 객체를 사용하여 PDF 파일을 엽니다.
  2. 문서의 Forms 컬렉션에서 양식을 가져옵니다.
  3. 직사각형 영역을 지정하고 Form 객체의 GetFieldsInRect 메서드에 전달합니다. Fields 컬렉션이 반환됩니다.
  4. 이를 사용하여 필드를 조작합니다.

다음 C# 코드 스니펫은 PDF 파일의 특정 직사각형 영역에서 양식 필드를 가져오는 방법을 보여줍니다.

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

    // Open PDF document
    using (var document = new Aspose.Pdf.Document(dataDir + "GetFieldsFromRegion.pdf"))
    {
        // Create rectangle object to get fields in that area
        var rectangle = new Aspose.Pdf.Rectangle(35, 30, 500, 500);

        // Get the PDF form
        var form = document.Form;

        // Get fields in the rectangular area
        var fields = form.GetFieldsInRect(rectangle);

        // Display Field names and values
        foreach (var field in fields)
        {
            // Display image placement properties for all placements
            Console.Out.WriteLine("Field Name: " + field.FullName + "-" + "Field Value: " + field.Value);
        }
    }
}