Import and export PDF form data in C++
Contents
[
Hide
]
Export form data to FDF
- Create a Aspose::Pdf::Facades::Form facade for the input PDF.
- Create a writable output stream.
- Call ExportFdf() on Aspose::Pdf::Facades::Form with the stream.
- 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
- Create a Aspose::Pdf::Facades::Form facade for the input PDF.
- Create a writable output stream.
- Call ExportXfdf() on Aspose::Pdf::Facades::Form with the stream.
- 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
- Create a Aspose::Pdf::Facades::Form facade for the input PDF.
- Create a writable output stream.
- Call ExportXml() on Aspose::Pdf::Facades::Form with the stream.
- 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.
- Create a Aspose::Pdf::Facades::Form facade for the input PDF.
- Open StudentInfoFormElectronic.fdf from the output PDF directory.
- Call ImportFdf() on Aspose::Pdf::Facades::Form with the stream.
- Save the modified PDF to outputFile.
- 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.
- Create a Aspose::Pdf::Facades::Form facade for the input PDF.
- Open StudentInfoFormElectronic.xfdf from the output PDF directory.
- Call ImportXfdf() on Aspose::Pdf::Facades::Form with the stream.
- Save the modified PDF to outputFile.
- 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.
- Create a Aspose::Pdf::Facades::Form facade for the input PDF.
- Open StudentInfoFormElectronic.xml from the output PDF directory.
- Call ImportXml() on Aspose::Pdf::Facades::Form with the stream.
- Save the modified PDF to outputFile.
- 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();
}