Hi,
thank you for integrating the patch.

I've tested solution with another set of PDF files and found another places where number of pages is requested using:
...
static_cast<int>(this->GetObject()->GetDictionary().GetKeyAsLong( "Count", 0LL ))
....

I've prepared new version of the patch to replace all such calls with one common function ( "PdfPagesTree::GetChildCount" ). This private function already exists in the PdfPagesTree but is used only at one place. I've also changed signature of the function:

-    int GetChildCount( const PdfObject* pNode ) const;
+    static int GetChildCount( const PdfObject* pNode );

Reasons to change signature of the method:
1. GetChildCount does not need any member variables from PdfPagesTree
2. Method is called from the constructor of PdfPagesTree
3. Method is private in PdfPagesTree and was used only at one place. Changing signature cannot break any APIs or applications.

Any comments are welcomed...

Petr

Dne 10.6.2014 18:46, zyx napsal(a):
On Tue, 2014-06-10 at 10:16 +0200, Petr Pytelka wrote:
There is small patch in the attachment which solves the issue.
         Hi,
the patch looks fine, I committed it as r1604:
http://sourceforge.net/p/podofo/code/1604
         Bye,
         zyx




--
LightComp v.o.s.
Document Management Systems&    Software Development
tel.: +420 271 741 664 (přímý) / +420 271 741 478
mob.: +420 603 277 068
http://www.lightcomp.cz


Index: src/doc/PdfPage.cpp
===================================================================
--- src/doc/PdfPage.cpp (revision 1604)
+++ src/doc/PdfPage.cpp (working copy)
@@ -526,7 +526,10 @@
                 if( pNode->GetDictionary().GetKey( PdfName::KeyType ) != NULL 
                     && pNode->GetDictionary().GetKey( PdfName::KeyType 
)->GetName() == PdfName( "Pages" ) )
                 {
-                    nPageNumber += 
static_cast<int>(pNode->GetDictionary().GetKey( "Count" )->GetNumber());
+                                       PdfObject* pCount = 
pNode->GetIndirectKey( "Count" );
+                                       if( pCount != NULL ) {
+                                               nPageNumber += 
static_cast<int>(pCount->GetNumber());
+                                       }
                 } else {
                     // if we do not have a page tree node, 
                     // we most likely have a page object:
Index: src/doc/PdfPagesTree.cpp
===================================================================
--- src/doc/PdfPagesTree.cpp    (revision 1604)
+++ src/doc/PdfPagesTree.cpp    (working copy)
@@ -56,7 +56,7 @@
 
 PdfPagesTree::PdfPagesTree( PdfObject* pPagesRoot )
     : PdfElement( "Pages", pPagesRoot ),
-      m_cache( static_cast<int>(pPagesRoot->GetDictionary().GetKeyAsLong( 
"Count", static_cast<pdf_int64>(0LL) )) )
+      m_cache( GetChildCount( pPagesRoot ) )
 {
     if( !this->GetObject() ) 
     {
@@ -71,13 +71,7 @@
 
 int PdfPagesTree::GetTotalNumberOfPages() const
 {
-    const PdfObject *pObject = GetObject()->GetIndirectKey( "Count" );
-    if ( pObject != NULL ) {
-        return (pObject->GetDataType() == ePdfDataType_Number) ?
-                static_cast<int>( pObject->GetNumber() ) : 0;
-    } else {
-        return 0;
-    }
+       return GetChildCount( GetObject() );
 }
 
 PdfPage* PdfPagesTree::GetPage( int nIndex )
@@ -317,7 +311,7 @@
     PdfArray::const_iterator it = rKidsArray.begin();
 
     const size_t numDirectKids = rKidsArray.size();
-    const size_t numKids = 
static_cast<size_t>(pParent->GetDictionary().GetKeyAsLong( "Count", 0LL ));
+       const size_t numKids = GetChildCount(pParent);
 
     if( static_cast<int>(numKids) < nPageNum ) 
     {
@@ -365,7 +359,7 @@
 
                 if( this->IsTypePages(pChild) ) 
                 {
-                    int childCount = this->GetChildCount( pChild );
+                    int childCount = GetChildCount( pChild );
                     if( childCount < nPageNum + 1 ) // Pages are 0 based, but 
count is not
                     {
                         // skip this page node
@@ -479,12 +473,18 @@
     return false;
 }
 
-int PdfPagesTree::GetChildCount( const PdfObject* pNode ) const
+int PdfPagesTree::GetChildCount( const PdfObject* pNode )
 {
     if( !pNode ) 
         return 0;
 
-    return static_cast<int>(pNode->GetDictionary().GetKeyAsLong("Count", 0L));
+       const PdfObject *pCount = pNode->GetIndirectKey( "Count" );
+       if( pCount != 0 ) {
+               return (pCount->GetDataType() == PoDoFo::ePdfDataType_Number) ? 
 
+                       static_cast<int>( pCount->GetNumber() ):0;
+       } else {
+               return 0;
+       }
 }
 
 int PdfPagesTree::GetPosInKids( PdfObject* pPageObj, PdfObject* pPageParent )
@@ -688,7 +688,7 @@
 {
     // Increment or decrement inPagesDict's Count by inDelta, and return the 
new count.
     // Simply return the current count if inDelta is 0.
-    int        cnt = static_cast<int>(pPageObj->GetDictionary().GetKey( 
"Count" )->GetNumber());
+    int        cnt = GetChildCount( pPageObj );
     if( 0 != nDelta ) 
     {
         cnt += nDelta ;
@@ -700,7 +700,7 @@
 
 bool PdfPagesTree::IsEmptyPageNode( PdfObject* pPageNode ) 
 {
-    long lCount = static_cast<long>(pPageNode->GetDictionary().GetKeyAsLong( 
PdfName("Count"), static_cast<pdf_int64>(0LL) ));
+       long lCount = GetChildCount( pPageNode );
     bool bKidsEmpty = true;
 
     if( pPageNode->GetDictionary().HasKey( PdfName("Kids") ) )
@@ -727,7 +727,7 @@
 
     PdfArray&  kidsArray = pObj->GetArray();
     size_t     numKids   = kidsArray.size();
-    size_t      kidsCount = pPagesObject->GetDictionary().GetKeyAsLong( 
"Count", 0 );
+    size_t      kidsCount = GetChildCount( pPagesObject );
 
     // All parents of the page node will be added to this lists,
     // so that the PdfPage can later access inherited attributes
Index: src/doc/PdfPagesTree.h
===================================================================
--- src/doc/PdfPagesTree.h      (revision 1604)
+++ src/doc/PdfPagesTree.h      (working copy)
@@ -182,7 +182,7 @@
     PdfObject* GetPageNode( int nPageNum, PdfObject* pParent, PdfObjectList & 
rLstParents );
     PdfObject* GetPageNodeFromArray( int nPageNum, const PdfArray & 
rKidsArray, PdfObjectList & rLstParents );
 
-    int GetChildCount( const PdfObject* pNode ) const;
+    static int GetChildCount( const PdfObject* pNode );
 
     /**
      * Test if a PdfObject is a page node
------------------------------------------------------------------------------
HPCC Systems Open Source Big Data Platform from LexisNexis Risk Solutions
Find What Matters Most in Your Big Data with HPCC Systems
Open Source. Fast. Scalable. Simple. Ideal for Dirty Data.
Leverages Graph Analysis for Fast Processing & Easy Data Exploration
http://p.sf.net/sfu/hpccsystems
_______________________________________________
Podofo-users mailing list
Podofo-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/podofo-users

Reply via email to