Remove PDF Form XObjects in C++

Clear the first page Form XObjects

This removes all Form XObject resources on page 1, including any that do not represent interactive form fields.

  1. Open the input PDF as a Document.
  2. Retrieve XFormCollection from page 1 resources.
  3. Call Clear() on the collection.
  4. 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/XFormCollection.h>


using namespace System;
using namespace Aspose::Pdf;

void FormExamples::RemoveAllForms(String inputFile, String outputFile)
{
    auto document = MakeObject<Document>(inputFile);
    auto forms = document->get_Pages()->idx_get(1)->get_Resources()->get_Forms();
    forms->Clear();
    document->Save(outputFile);
}

Remove matching Typewriter XObjects

Collecting names before deletion avoids changing the collection while it is being enumerated. To delete an AcroForm field by name, use the Modify AcroForm guide.

  1. Open the input PDF as a Document.
  2. Enumerate XForm resources on page 1.
  3. Collect resource names when IT is Typewriter and Subtype is Form.
  4. Delete the collected names from XFormCollection.
  5. 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/XForm.h>
#include <Aspose.PDF.Cpp/XFormCollection.h>

using namespace System;
using namespace Aspose::Pdf;

void FormExamples::RemoveSpecifiedForm(String inputFile, String outputFile)
{
    auto document = MakeObject<Document>(inputFile);
    auto forms = document->get_Pages()->idx_get(1)->get_Resources()->get_Forms();
    std::vector<String> formNames;
    for (auto& form : forms)
    {
        if (String(u"Typewriter") == form->get_IT() && String(u"Form") == form->get_Subtype())
        {
            formNames.push_back(forms->GetFormName(form));
        }
    }
    for (auto& formName : formNames)
    {
        forms->Delete(formName);
    }
    document->Save(outputFile);
}