Remove PDF background artifacts in C++
Contents
[
Hide
]
The example processes only the first page and deletes artifacts whose Type is Pagination and Subtype is Background. Background-like content stored as ordinary page text, images, or annotations is outside this filter.
The input PDF must contain at least one page. Configure the Aspose.PDF for C++ SDK and use a writable output path for examples that save a PDF.
Remove background artifacts
Delete Pagination artifacts with the Background subtype from page 1. Reverse iteration avoids skipping items when the collection shrinks.
- Open the input PDF with Document.
- Get the first page Artifacts collection.
- Iterate backward from ArtifactCollection::get_Count() to index 1.
- Check Artifact::get_Type() for Pagination and get_Subtype() for Background.
- Delete matches with ArtifactCollection::Delete().
- Save the result with Document::Save().
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
#include <Aspose.PDF.Cpp/Artifacts/Artifact.h>
#include <Aspose.PDF.Cpp/Artifacts/ArtifactCollection.h>
using namespace System;
using namespace Aspose::Pdf;
void RemoveBackground(System::String inputFile, System::String outputFile)
{
auto document = MakeObject<Document>(inputFile);
auto artifacts = document->get_Pages()->idx_get(1)->get_Artifacts();
for (int i = artifacts->get_Count(); i >= 1; --i) {
auto artifact = artifacts->idx_get(i);
if (artifact->get_Type() == Artifact::ArtifactType::Pagination &&
artifact->get_Subtype() == Artifact::ArtifactSubtype::Background) {
artifacts->Delete(artifact);
}
}
document->Save(outputFile);
}