Set Field Script in C++

Use FormEditor for this task. The examples below retain the original C++ code and API Reference steps.

Set field script

Add an alert script to Script_Demo_Button, then replace it with a second alert script.

  1. Create FormEditor.
  2. Bind the PDF input with Facade::BindPdf().
  3. Add a field script with FormEditor::AddFieldScript().
  4. Replace the field script with FormEditor::SetFieldScript().
  5. Save the resulting PDF with SaveableFacade::Save().
#include <Aspose.PDF.Cpp/Facades/FormEditor.h>
#include <system/string.h>

namespace Facades::Examples::PDF_FormEditor
{
using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Facades;

void SetFieldScript(String inputFile, String outputFile)
{
    auto editor = MakeObject<FormEditor>();

    editor->BindPdf(inputFile);
    editor->AddFieldScript(u"Script_Demo_Button", u"app.alert('Script 1 has been executed');");
    editor->SetFieldScript(u"Script_Demo_Button", u"app.alert('Script 2 has been executed');");
    editor->SaveableFacade::Save(outputFile);
}
} // namespace Facades::Examples::PDF_FormEditor