Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/8a76650883b4ab68490c1d7dca25ccf4 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/8a76650883b4ab68490c1d7dca25ccf4 to your computer and use it in GitHub Desktop.
Create, Fill, or Edit Fillable PDF Forms with C++
Create, Fill, or Edit Fillable PDF Forms with C++
// Create an instance of the Document class
auto pdfDocument = MakeObject<Document>();
// Add a blank page to the document
System::SharedPtr<Page> page = pdfDocument->get_Pages()->Add();
System::SharedPtr<Aspose::Pdf::Rectangle> rectangle1 = MakeObject<Aspose::Pdf::Rectangle>(275, 740, 440, 770);
// Create a TextBoxField
System::SharedPtr<TextBoxField> nameBox = MakeObject<TextBoxField>(pdfDocument, rectangle1);
nameBox->set_PartialName(u"nameBox1");
nameBox->get_DefaultAppearance()->set_FontSize(10);
nameBox->set_Multiline(true);
System::SharedPtr<Border> nameBorder = MakeObject<Border>(nameBox);
nameBorder->set_Width(1);
nameBox->set_Border(nameBorder);
nameBox->get_Characteristics()->set_Border(System::Drawing::Color::get_Black());
nameBox->set_Color(Aspose::Pdf::Color::FromRgb(System::Drawing::Color::get_Red()));
System::SharedPtr<Aspose::Pdf::Rectangle> rectangle2 = MakeObject<Aspose::Pdf::Rectangle>(275, 718, 440, 738);
// Create a TextBoxField
System::SharedPtr<TextBoxField> mrnBox = MakeObject<TextBoxField>(pdfDocument, rectangle2);
mrnBox->set_PartialName(u"Box1");
mrnBox->get_DefaultAppearance()->set_FontSize(10);
System::SharedPtr<Border> mrnBorder = MakeObject<Border>(mrnBox);
mrnBox->set_Width(165);
mrnBox->set_Border(mrnBorder);
mrnBox->get_Characteristics()->set_Border(System::Drawing::Color::get_Black());
mrnBox->set_Color(Aspose::Pdf::Color::FromRgb(System::Drawing::Color::get_Red()));
// Add TextBoxField to the form
pdfDocument->get_Form()->Add(nameBox, 1);
pdfDocument->get_Form()->Add(mrnBox, 1);
// Create a table
System::SharedPtr<Table> table = MakeObject<Table>();
table->set_Left(200);
table->set_Top(300);
table->set_ColumnWidths(u"120");
// Add the table to the page
page->get_Paragraphs()->Add(table);
// Create Rows and Columns
System::SharedPtr<Row> r1 = table->get_Rows()->Add();
System::SharedPtr<Row> r2 = table->get_Rows()->Add();
System::SharedPtr<Cell> c1 = r1->get_Cells()->Add();
System::SharedPtr<Cell> c2 = r2->get_Cells()->Add();
// Create a RadioButtonField
System::SharedPtr<RadioButtonField> rf = MakeObject<RadioButtonField>(page);
rf->set_PartialName(u"radio");
// Add the RadioButtonField to the form
pdfDocument->get_Form()->Add(rf, 1);
// Create radio button options
System::SharedPtr<RadioButtonOptionField> opt1 = MakeObject<RadioButtonOptionField>();
System::SharedPtr<RadioButtonOptionField> opt2 = MakeObject<RadioButtonOptionField>();
opt1->set_OptionName(u"Yes");
opt2->set_OptionName(u"No");
opt1->set_Width(15);
opt1->set_Height(15);
opt2->set_Width(15);
opt2->set_Height(15);
// Add the options to the RadioButtonField
rf->Add(opt1);
rf->Add(opt2);
System::SharedPtr<Border> opt1Border = MakeObject<Border>(opt1);
opt1->set_Border(opt1Border);
opt1->get_Border()->set_Width(1);
opt1->get_Border()->set_Style(BorderStyle::Solid);
opt1->get_Characteristics()->set_Border(System::Drawing::Color::get_Black());
opt1->get_DefaultAppearance()->set_TextColor(System::Drawing::Color::get_Red());
System::SharedPtr<TextFragment> opt1Fragment = MakeObject<TextFragment>(u"Yes");
opt1->set_Caption(opt1Fragment);
System::SharedPtr<Border> opt2Border = MakeObject<Border>(opt2);
opt2->set_Border(opt2Border);
opt2->get_Border()->set_Width(1);
opt2->get_Border()->set_Style(BorderStyle::Solid);
opt2->get_Characteristics()->set_Border(System::Drawing::Color::get_Black());
opt2->get_DefaultAppearance()->set_TextColor(System::Drawing::Color::get_Red());
System::SharedPtr<TextFragment> opt2Fragment = MakeObject<TextFragment>(u"No");
opt2->set_Caption(opt2Fragment);
// Add the options to the cells of the table
c1->get_Paragraphs()->Add(opt1);
c2->get_Paragraphs()->Add(opt2);
// Save the output file
pdfDocument->Save(u"OutputDirectory\\Fillable_PDF_Form_Out.pdf");
// Load the PDF file
auto pdfDocument = MakeObject<Document>(u"SourceDirectory\\Fill_PDF_Form_Field.pdf");
// Delete the field
pdfDocument->get_Form()->Delete(u"nameBox1");
// Save the output file
pdfDocument->Save(u"OutputDirectory\\Delete_Form_Field_out.pdf");
// Load the PDF file
auto pdfDocument = MakeObject<Document>(u"SourceDirectory\\Fillable_PDF_Form.pdf");
// Retrieve the text box fields
System::SharedPtr<TextBoxField> textBoxField1 = System::DynamicCast<TextBoxField>(pdfDocument->get_Form()->idx_get(u"nameBox1"));
System::SharedPtr<TextBoxField> textBoxField2 = System::DynamicCast<TextBoxField>(pdfDocument->get_Form()->idx_get(u"Box1"));
// Set the value of the text box fields
textBoxField1->set_Value(u"A quick brown fox jumped over the lazy dog.");
textBoxField2->set_Value(u"A quick brown fox jumped over the lazy dog.");
// Retrieve the radio button field
System::SharedPtr<RadioButtonField> radioField = System::DynamicCast<RadioButtonField>(pdfDocument->get_Form()->idx_get(u"radio"));
// Set the value of the radio button field
radioField->set_Selected(1);
// Save the output file
pdfDocument->Save(u"OutputDirectory\\Fill_PDF_Form_Field_Out.pdf");
// Load the PDF file
auto pdfDocument = MakeObject<Document>(u"SourceDirectory\\Fill_PDF_Form_Field.pdf");
// Retrieve the TextBoxField
System::SharedPtr<TextBoxField> textBoxField = System::DynamicCast<TextBoxField>(pdfDocument->get_Form()->idx_get(u"nameBox1"));
// Update the value of the TextBoxField
textBoxField->set_Value(u"Changed Value");
// Mark the TextBoxField as readonly
textBoxField->set_ReadOnly(true);
// Save the output file
pdfDocument->Save(u"OutputDirectory\\Modify_Form_Field_out.pdf");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment