استخراج AcroForm - استخراج بيانات النموذج من PDF في C#

تعمل مقتطفات الشيفرة التالية أيضًا مع مكتبة 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 لحقل النموذج الحصول على قيمة حقل معين. للحصول على القيمة، احصل على حقل النموذج من مجموعة Form لكائن Document. يختار هذا المثال 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. افتح ملف PDF باستخدام كائن Document .
  2. احصل على النموذج من مجموعة Forms الخاصة بالمستند.
  3. حدد المنطقة المستطيلة ومررها إلى طريقة GetFieldsInRect لكائن Form. يتم إرجاع مجموعة 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);
        }
    }
}