Search code examples
c++podofo

How to determine PdfPage object emptiness?


Let's have following snippet of code:

PoDoFo::PdfMemDocument pdfDoc;
pdfDoc.CreatePage( PoDoFo::PdfPage::CreateStandardPageSize( PoDoFo::ePdfPageSize_A4 ) );

the newly created page is clearly empty. How can one determine such a page is empty (with no xobjects, etc. inside). I've tried to lookup any kind of method like isEmpty() or implement my own solution as you can see below, but without any luck.

auto page = src.GetPage( nPage );

if( page->GetContents() == page->GetObject() )
{
   // Page is empty
}

// Also tried these checks:
// page->GetResources()->GetObjectLength() == 0
// page->GetResources()->GetStream()->GetLength() == 0
// page->GetResources()->HasStream() == false

As it has high prioroty, I've asked kind of dumb question meanwhile digging deep in the PoDoFo library to find solution. Does anyone have an idea how could we determine page emptiness?


Solution

  • After few painful hours I've managed to solve the issue (could be done smarter, but whatever):

    /// @param page Page object to be checked for emptiness.
    /// @return True in case page is empty, false otherwise.
    bool isDocumentPageEmpty( const PoDoFo::PdfPage* page )
    {
        PoDoFo::PdfContentsTokenizer tokenizer( const_cast<PoDoFo::PdfPage*>( page ) );
        PoDoFo::PdfVariant var;
        PoDoFo::EPdfContentsType type;
        const char* token = nullptr;
    
        while( tokenizer.ReadNext( type, token, var ) )
        {
           switch( type )
           {
              case PoDoFo::ePdfContentsType_Keyword:
              case PoDoFo::ePdfContentsType_Variant:
              {
                 return false;
              }
           }
        }
    
        return true;
    }