PdfAnnotation* PdfPage::GetAnnotation( int index ) { PdfAnnotation* pAnnot; PdfReference ref; PdfObject* pObj = this->GetAnnotationsArray( false ); if( !(pObj && pObj->IsArray()) ) { PODOFO_RAISE_ERROR( ePdfError_InvalidDataType ); } if( index < 0 && static_cast<unsigned int>(index) >= pObj->GetArray().size() ) { PODOFO_RAISE_ERROR( ePdfError_ValueOutOfRange ); } ref = pObj->GetArray()[index].GetReference(); pAnnot = m_mapAnnotations[ref]; if( !pAnnot ) { pObj = m_pObject->GetOwner()->GetObject( ref ); if( !pObj ) { PdfError::DebugMessage( "Error looking up object %i %i R\n", ref.ObjectNumber(), ref.GenerationNumber() ); PODOFO_RAISE_ERROR( ePdfError_NoObject ); } pAnnot = new PdfAnnotation( pObj, this ); m_mapAnnotations[ref] = pAnnot; } return pAnnot; }
void PdfXRef::AddObject( const PdfReference & rRef, pdf_uint64 offset, bool bUsed ) { TIVecXRefBlock it = m_vecBlocks.begin(); PdfXRef::TXRefItem item( rRef, offset ); bool bInsertDone = false; while( it != m_vecBlocks.end() ) { if( (*it).InsertItem( item, bUsed ) ) { bInsertDone = true; break; } ++it; } if( !bInsertDone ) { PdfXRefBlock block; block.m_nFirst = rRef.ObjectNumber(); block.m_nCount = 1; if( bUsed ) block.items.push_back( item ); else block.freeItems.push_back( rRef ); m_vecBlocks.push_back( block ); std::sort( m_vecBlocks.begin(), m_vecBlocks.end() ); } }
void PdfVecObjects::AddFreeObject( const PdfReference & rReference ) { std::pair<TIPdfReferenceList,TIPdfReferenceList> it = std::equal_range( m_lstFreeObjects.begin(), m_lstFreeObjects.end(), rReference, ReferenceComparatorPredicate() ); if( it.first != it.second && !m_lstFreeObjects.empty() ) { // Be sure that no reference is added twice to free list PdfError::DebugMessage( "Adding %d to freelist, is already contained !!", rReference.ObjectNumber() ); return; } else { // When append free objects from external doc we need plus one number objects SetObjectCount( rReference ); // Insert so that list stays sorted m_lstFreeObjects.insert( it.first, rReference ); } }
PdfObject* PdfPagesTree::GetPageNode( int nPageNum, PdfObject* pParent, PdfObjectList & rLstParents ) { if( !pParent ) { PODOFO_RAISE_ERROR( ePdfError_InvalidHandle ); } if( !pParent->GetDictionary().HasKey( PdfName("Kids") ) ) { PODOFO_RAISE_ERROR( ePdfError_InvalidKey ); } const PdfObject* pObj = pParent->GetIndirectKey( "Kids" ); if( pObj == NULL || !pObj->IsArray() ) { PODOFO_RAISE_ERROR( ePdfError_InvalidDataType ); } const PdfArray & rKidsArray = pObj->GetArray(); PdfArray::const_iterator it = rKidsArray.begin(); const size_t numDirectKids = rKidsArray.size(); const size_t numKids = GetChildCount(pParent); // use <= since nPageNum is 0-based if( static_cast<int>(numKids) <= nPageNum ) { PdfError::LogMessage( eLogSeverity_Critical, "Cannot retrieve page %i from a document with only %i pages.", nPageNum, static_cast<int>(numKids) ); return NULL; } //printf("Fetching: %i %i %i\n", numDirectKids, numKids, nPageNum ); if( numDirectKids == numKids && static_cast<size_t>(nPageNum) < numDirectKids ) { // This node has only page nodes as kids, // so we can access the array directly rLstParents.push_back( pParent ); return GetPageNodeFromArray( nPageNum, rKidsArray, rLstParents ); } else { // We have to traverse the tree while( it != rKidsArray.end() ) { if( (*it).IsArray() ) { // Fixes PDFs broken by having trees with arrays nested once rLstParents.push_back( pParent ); // the following code is to find the reference to log this with const PdfReference & rIterArrayRef = (*it).Reference(); PdfReference refToLog; bool isDirectObject // don't worry about 0-num. indirect ones = ( !(rIterArrayRef.ObjectNumber() ) ); if ( isDirectObject ) { if ( !(pObj->Reference().ObjectNumber() ) ) // rKidsArray's { refToLog = pParent->Reference(); } else { refToLog = pObj->Reference(); } } else { refToLog = rIterArrayRef; } PdfError::LogMessage( eLogSeverity_Error, "Entry in Kids array is itself an array" "%s reference: %s\n", isDirectObject ? " (direct object)" ", in object with" : ",", refToLog.ToString().c_str() ); const PdfArray & rIterArray = (*it).GetArray(); // is the array large enough to potentially have the page? if( static_cast<size_t>(nPageNum) < rIterArray.GetSize() ) { PdfObject* pPageNode = GetPageNodeFromArray( nPageNum, rIterArray, rLstParents ); if ( pPageNode ) // and if not, search further return pPageNode; } } else if( (*it).IsReference() ) { PdfObject* pChild = GetRoot()->GetOwner()->GetObject( (*it).GetReference() ); if (!pChild) { PdfError::LogMessage( eLogSeverity_Critical, "Requesting page index %i. Child not found: %s\n", nPageNum, (*it).GetReference().ToString().c_str()); return NULL; } if( this->IsTypePages(pChild) ) { int childCount = GetChildCount( pChild ); if( childCount < nPageNum + 1 ) // Pages are 0 based, but count is not { // skip this page node // and go to the next one nPageNum -= childCount; } else { rLstParents.push_back( pParent ); return this->GetPageNode( nPageNum, pChild, rLstParents ); } } else if( this->IsTypePage(pChild) ) { if( 0 == nPageNum ) { rLstParents.push_back( pParent ); return pChild; } // Skip a normal page if(nPageNum > 0 ) nPageNum--; } else { const PdfReference & rLogRef = pChild->Reference(); pdf_objnum nLogObjNum = rLogRef.ObjectNumber(); pdf_gennum nLogGenNum = rLogRef.GenerationNumber(); PdfError::LogMessage( eLogSeverity_Critical, "Requesting page index %i. " "Invalid datatype referenced in kids array: %s\n" "Reference to invalid object: %i %i R\n", nPageNum, pChild->GetDataTypeString(), nLogObjNum, nLogGenNum); } } else { PdfError::LogMessage( eLogSeverity_Critical, "Requesting page index %i. Invalid datatype in kids array: %s\n", nPageNum, (*it).GetDataTypeString()); return NULL; } ++it; } } return NULL; }