Modify PDF form fields in C++
Contents
[
Hide
]
Read and set text-field limits, change field appearance, delete a named field, and clear text in selected Form XObjects using Aspose.PDF for C++.
Read the first field text limit
The form must contain at least one field. Only the first field is inspected; outputFile is unused.
- Open the input PDF as a Document.
- Select index 0 from the form Fields array.
- Cast the field to TextBoxField.
- Print MaxLen when the cast succeeds.
#include <Aspose.PDF.Cpp/InteractiveFeatures/Forms/TextBoxField.h>
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Page.h>
using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Forms;
using namespace Aspose::Pdf::Text;
void FormExamples::GetFieldLimit(String inputFile, String outputFile)
{
auto document = MakeObject<Document>(inputFile);
auto field = document->get_Form()->get_Fields()->idx_get(0);
if (auto textBoxField = field.dynamic_pointer_cast<TextBoxField>())
{
std::cout << (String::Format(u"Limit: {0}", textBoxField->get_MaxLen())) << std::endl;
}
}
Set a named field character limit
The input must contain the target field. This snippet retains the qualified base-class Save() call from the source.
- Create FormEditor and bind the input PDF.
- Call SetFieldLimit() for First Name with a limit of 15.
- Save through the SaveableFacade base implementation.
#include <Aspose.PDF.Cpp/Facades/FormEditor.h>
using namespace System;
void FormExamples::SetFieldLimit(String inputFile, String outputFile)
{
auto form = MakeObject<Aspose::Pdf::Facades::FormEditor>();
form->BindPdf(inputFile);
form->SetFieldLimit(String(u"First Name"), 15);
form->Aspose::Pdf::Facades::SaveableFacade::Save(outputFile);
}
Change the first text field font
The form must have at least one field and Calibri must be available.
- Open the input PDF as a Document.
- Select form field index 0 and cast it to TextBoxField.
- Create a DefaultAppearance using Calibri from FontRepository, size 10, and black text.
- Assign the appearance when the cast succeeds.
- Save the PDF with Save().
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Text/FontRepository.h>
#include <Aspose.PDF.Cpp/Color.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/DefaultAppearance.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/Forms/TextBoxField.h>
using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Forms;
using namespace Aspose::Pdf::Annotations;
using namespace Aspose::Pdf::Text;
void FormExamples::SetFormFieldFont(String inputFile, String outputFile)
{
auto document = MakeObject<Document>(inputFile);
auto field = document->get_Form()->get_Fields()->idx_get(0);
if (auto textBoxField = field.dynamic_pointer_cast<TextBoxField>())
{
textBoxField->set_DefaultAppearance(MakeObject<DefaultAppearance>(
FontRepository::FindFont(String(u"Calibri")), 10, Color::get_Black()->ToRgb()));
}
document->Save(outputFile);
}
Delete a named AcroForm field
- Open the input PDF as a Document.
- Call Delete() on Aspose::Pdf::Forms::Form for the field named First Name.
- Save the PDF with Save().
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/Forms/Form.h>
using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Forms;
void FormExamples::DeleteFormField(String inputFile, String outputFile)
{
auto document = MakeObject<Document>(inputFile);
document->get_Form()->Delete(String(u"First Name"));
document->Save(outputFile);
}
Clear text in matching Typewriter XObjects
This modifies selected Form XObjects, not the values of all AcroForm fields.
- Open the input PDF as a Document.
- Enumerate XForm resources on page 1.
- Select resources whose IT is Typewriter and Subtype is Form.
- Visit each selected resource with TextFragmentAbsorber.
- Set every detected fragment text to an empty string.
- Save the PDF with Save().
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
#include <Aspose.PDF.Cpp/Resources.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/Forms/Form.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/Forms/FormType.h>
#include <Aspose.PDF.Cpp/Text/TextFragment.h>
#include <Aspose.PDF.Cpp/Text/TextFragmentAbsorber.h>
#include <Aspose.PDF.Cpp/Text/TextFragmentCollection.h>
#include <Aspose.PDF.Cpp/XForm.h>
#include <Aspose.PDF.Cpp/XFormCollection.h>
using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Forms;
using namespace Aspose::Pdf::Text;
void FormExamples::ClearTextInForm(String inputFile, String outputFile)
{
auto document = MakeObject<Document>(inputFile);
for (auto& form : document->get_Pages()->idx_get(1)->get_Resources()->get_Forms())
{
if (String(u"Typewriter") == form->get_IT() && String(u"Form") == form->get_Subtype())
{
auto absorber = MakeObject<TextFragmentAbsorber>();
absorber->Visit(form);
for (auto& fragment : absorber->get_TextFragments())
{
fragment->set_Text(String::Empty);
}
}
}
document->Save(outputFile);
}