Count PDF pagination artifacts in C++
Contents
[
Hide
]
Inspect the first page artifact collection and count Watermark, Background, Header, and Footer subtypes. Other artifact types and subtypes are skipped.
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.
Count pagination artifacts by subtype
Initialize four counters, skip artifacts whose type is not Pagination, and increment the counter for each recognized subtype. Print the counts without saving a modified PDF.
- Open the input PDF with Document.
- Iterate through the first page Artifacts collection.
- Check Artifact::get_Type() and skip non-Pagination artifacts.
- Switch on Artifact::get_Subtype() to count Watermark, Background, Header, and Footer.
- Print the four counters.
#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;
#include <iostream>
void CountPdfArtifacts(System::String inputFile)
{
auto document = MakeObject<Document>(inputFile);
int watermarks = 0, backgrounds = 0, headers = 0, footers = 0;
for (auto& artifact : document->get_Pages()->idx_get(1)->get_Artifacts()) {
if (artifact->get_Type() != Artifact::ArtifactType::Pagination) continue;
switch (artifact->get_Subtype()) {
case Artifact::ArtifactSubtype::Watermark: ++watermarks; break;
case Artifact::ArtifactSubtype::Background: ++backgrounds; break;
case Artifact::ArtifactSubtype::Header: ++headers; break;
case Artifact::ArtifactSubtype::Footer: ++footers; break;
default: break;
}
}
std::cout << "Watermarks: " << watermarks << "\nBackgrounds: " << backgrounds
<< "\nHeaders: " << headers << "\nFooters: " << footers << std::endl;
}