Extract PDF form data in C++

Read PDF form field values and export a name-to-value JSON object using Aspose.PDF for C++.

The outputFile parameter is unused; results are printed to the console.

  1. Create a Aspose::Pdf::Facades::Form facade for the input PDF.
  2. Iterate through FieldNames.
  3. Call GetField() on Aspose::Pdf::Facades::Form for each name and print the value.
  4. Print the field count and close the facade.
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Facades/Form.h>
#include <system/io/file.h>
#include <system/io/path.h>

using namespace System;
using namespace Aspose::Pdf;

void FormExamples::GetValuesFromAllFields(String inputFile, String outputFile)
{
    auto form = MakeObject<Aspose::Pdf::Facades::Form>(inputFile);
    for (const auto &name : form->get_FieldNames())
        std::cout << name << ": " << form->GetField(name) << std::endl;

    std::cout << "Fields: " << form->get_FieldNames()->get_Length() << std::endl;
    form->Close();
}

Export field values as JSON

This exports names and values, not complete field definitions, widget geometry, or appearance settings.

  1. Create a Aspose::Pdf::Facades::Form facade for the input PDF.
  2. Define the helper that escapes quotation marks, backslashes, and control characters.
  3. Iterate through FieldNames and retrieve each value with GetField() on Aspose::Pdf::Facades::Form.
  4. Build a JSON object whose properties map field names to string values.
  5. Write the JSON text to outputFile and close the facade.
#include <Aspose.PDF.Cpp/Facades/Form.h>
#include <system/io/file.h>
#include <system/io/path.h>

using namespace System;
using namespace Aspose::Pdf;

void FormExamples::ExtractFormFieldsToJson(String inputFile, String outputFile)
{
    auto form = MakeObject<Aspose::Pdf::Facades::Form>(inputFile);
    auto escape = [](const String &value) {
        String result;
        for (int i = 0; i < value.get_Length(); ++i)
        {
            char16_t c = value[i];
            if (c == u'"' || c == u'\\')
            {
                result += u'\\';
                result += c;
            }
            else if (c < 0x20)
                result += String::Format(u"\\u{0:x4}", static_cast<int>(c));
            else
                result += c;
        }
        return result;
    };
    String json = u"{\n";
    auto names = form->get_FieldNames();
    for (int i = 0; i < names->get_Length(); ++i)
    {
        auto name = names->idx_get(i);
        json += u"    \"" + escape(name) + u"\": \"" + escape(form->GetField(name)) + u"\"";
        if (i + 1 < names->get_Length())
            json += u",";
        json += u"\n";
    }
    json += u"}\n";
    System::IO::File::WriteAllText(outputFile, json);
    form->Close();
}

Use the delegating JSON entry point

Despite the Doc suffix, this wrapper does not use a separate Document-based JSON serialization API.

  1. Keep the declaration and implementation of ExtractFormFieldsToJson() available.
  2. Call ExtractFormFieldsToJsonDoc() with the input PDF and output JSON paths.
  3. Let it delegate to the preceding JSON exporter.
#include <Aspose.PDF.Cpp/Facades/Form.h>
#include <system/io/file.h>
#include <system/io/path.h>

using namespace System;
using namespace Aspose::Pdf;

void FormExamples::ExtractFormFieldsToJsonDoc(String inputFile, String outputFile)
{
    ExtractFormFieldsToJson(inputFile, outputFile);
}