Remove PDF Form XObjects in C++
Contents
[
Hide
]
Clear the first page Form XObjects
This removes all Form XObject resources on page 1, including any that do not represent interactive form fields.
- Open the input PDF as a Document.
- Retrieve XFormCollection from page 1 resources.
- Call Clear() on the collection.
- 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.
- Open the input PDF as a Document.
- Enumerate XForm resources on page 1.
- Collect resource names when IT is Typewriter and Subtype is Form.
- Delete the collected names from XFormCollection.
- 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);
}