Import and export PDF form data in C++

Export form data to FDF

  1. Create a Aspose::Pdf::Facades::Form facade for the input PDF.
  2. Create a writable output stream.
  3. Call ExportFdf() on Aspose::Pdf::Facades::Form with the stream.
  4. Close the stream and the facade.
#include <Aspose.PDF.Cpp/Facades/Form.h>
#include <system/io/file.h>

using namespace System;

void FormExamples::ExportDataToFdf(String inputFile, String outputFile)
{
    auto form = MakeObject<Aspose::Pdf::Facades::Form>(inputFile);
    auto stream = System::IO::File::Create(outputFile);
    form->ExportFdf(stream);
    stream->Close();
    form->Close();
}

Export form data to XFDF

  1. Create a Aspose::Pdf::Facades::Form facade for the input PDF.
  2. Create a writable output stream.
  3. Call ExportXfdf() on Aspose::Pdf::Facades::Form with the stream.
  4. Close the stream and 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::ExportDataToXfdf(String inputFile, String outputFile)
{
    auto form = MakeObject<Aspose::Pdf::Facades::Form>(inputFile);
    auto stream = System::IO::File::Create(outputFile);
    form->ExportXfdf(stream);
    stream->Close();
    form->Close();
}

Export form data to XML

  1. Create a Aspose::Pdf::Facades::Form facade for the input PDF.
  2. Create a writable output stream.
  3. Call ExportXml() on Aspose::Pdf::Facades::Form with the stream.
  4. Close the stream and 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::ExportDataToXml(String inputFile, String outputFile)
{
    auto form = MakeObject<Aspose::Pdf::Facades::Form>(inputFile);
    auto stream = System::IO::File::Create(outputFile);
    form->ExportXml(stream);
    stream->Close();
    form->Close();
}

Import form data from FDF

The data file is resolved relative to outputFile, not inputFile.

  1. Create a Aspose::Pdf::Facades::Form facade for the input PDF.
  2. Open StudentInfoFormElectronic.fdf from the output PDF directory.
  3. Call ImportFdf() on Aspose::Pdf::Facades::Form with the stream.
  4. Save the modified PDF to outputFile.
  5. Close the input stream and 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::ImportDataFromFdf(String inputFile, String outputFile)
{
    auto form = MakeObject<Aspose::Pdf::Facades::Form>(inputFile);
    auto stream = System::IO::File::OpenRead(System::IO::Path::Combine(
        System::IO::Path::GetDirectoryName(outputFile), u"StudentInfoFormElectronic.fdf"));
    form->ImportFdf(stream);
    form->Save(outputFile);
    stream->Close();
    form->Close();
}

Import form data from XFDF

The data file is resolved relative to outputFile, not inputFile.

  1. Create a Aspose::Pdf::Facades::Form facade for the input PDF.
  2. Open StudentInfoFormElectronic.xfdf from the output PDF directory.
  3. Call ImportXfdf() on Aspose::Pdf::Facades::Form with the stream.
  4. Save the modified PDF to outputFile.
  5. Close the input stream and 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::ImportDataFromXfdf(String inputFile, String outputFile)
{
    auto form = MakeObject<Aspose::Pdf::Facades::Form>(inputFile);
    auto stream = System::IO::File::OpenRead(System::IO::Path::Combine(
        System::IO::Path::GetDirectoryName(outputFile), u"StudentInfoFormElectronic.xfdf"));
    form->ImportXfdf(stream);
    form->Save(outputFile);
    stream->Close();
    form->Close();
}

Import form data from XML

The XML example closes the stream and facade after success and when a System::Exception is caught, then rethrows the exception.

  1. Create a Aspose::Pdf::Facades::Form facade for the input PDF.
  2. Open StudentInfoFormElectronic.xml from the output PDF directory.
  3. Call ImportXml() on Aspose::Pdf::Facades::Form with the stream.
  4. Save the modified PDF to outputFile.
  5. Close the input stream and 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::ImportDataFromXml(String inputFile, String outputFile)
{
    auto form = MakeObject<Aspose::Pdf::Facades::Form>(inputFile);
    auto stream = System::IO::File::OpenRead(System::IO::Path::Combine(
        System::IO::Path::GetDirectoryName(outputFile), u"StudentInfoFormElectronic.xml"));

    const auto cleanup = [&]() {
        stream->Close();
        form->Close();
    };

    try {
        form->ImportXml(stream);
        form->Save(outputFile);
    }
    catch (const System::Exception&) {
        cleanup();
        throw;
    }

    cleanup();
}