首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何判断PdfPage对象是否为空?

如何判断PdfPage对象是否为空?
EN

Stack Overflow用户
提问于 2020-05-14 22:45:11
回答 1查看 55关注 0票数 1

让我们来看下面的代码片段:

代码语言:javascript
复制
PoDoFo::PdfMemDocument pdfDoc;
pdfDoc.CreatePage( PoDoFo::PdfPage::CreateStandardPageSize( PoDoFo::ePdfPageSize_A4 ) );

新创建的页面显然是空的。如何确定这样的页面是空的(内部没有xobject等)。我试图查找任何类型的方法,如isEmpty()或实现我自己的解决方案,如下所示,但没有任何运气。

代码语言:javascript
复制
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

由于它有很高的优先级,我问了一个愚蠢的问题,同时深入PoDoFo库中寻找解决方案。有没有人知道我们如何确定页面空白?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-15 05:24:46

经过几个小时的痛苦,我终于解决了这个问题(可以做得更聪明,但不管怎样):

代码语言:javascript
复制
/// @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;
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61800289

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档