void EncryptTest::testLoadEncrypedFilePdfMemDocument() { std::string sFilename = TestUtils::getTempFilename(); try { CreatedEncrypedPdf( sFilename.c_str() ); // Try loading with PdfParser PdfMemDocument document; try { document.Load( sFilename.c_str() ); // Must throw an exception CPPUNIT_FAIL("Encrypted file not recognized!"); } catch( const PdfError & e ) { if( e.GetError() != ePdfError_InvalidPassword ) { CPPUNIT_FAIL("Invalid encryption exception thrown!"); } } document.SetPassword( "user" ); } catch( PdfError & e ) { e.PrintErrorMsg(); printf("Removing temp file: %s\n", sFilename.c_str()); TestUtils::deleteFile(sFilename.c_str()); throw e; } printf("Removing temp file: %s\n", sFilename.c_str()); TestUtils::deleteFile(sFilename.c_str()); }
void PagesTreeTest::testDeleteAll( PdfMemDocument & doc ) { for(int i=0; i<PODOFO_TEST_NUM_PAGES; i++) { doc.GetPagesTree()->DeletePage(0); CPPUNIT_ASSERT_EQUAL( doc.GetPageCount(), PODOFO_TEST_NUM_PAGES - (i + 1) ); } CPPUNIT_ASSERT_EQUAL( doc.GetPageCount(), 0 ); }
int count_pages( const char* pszFilename, const bool & bShortFormat ) { PdfMemDocument document; document.Load( pszFilename ); int nPages = document.GetPageCount(); if( bShortFormat ) printf("%i\n", nPages ); else printf("%s:\t%i\n", pszFilename, nPages ); return nPages; }
void PagesTreeTest::testEmptyDoc() { // PdfPagesTree does not throw exceptions but PdfDocument does PdfMemDocument writer; // Empty document must have page count == 0 CPPUNIT_ASSERT_EQUAL( writer.GetPageCount(), 0 ); // Retrieving any page from an empty document must be NULL CPPUNIT_ASSERT_THROW( writer.GetPage( 0 ), PdfError ); CPPUNIT_ASSERT_THROW( writer.GetPage( -1 ), PdfError ); CPPUNIT_ASSERT_THROW( writer.GetPage( 1 ), PdfError ); }
void PagesTreeTest::testEmptyTree() { PdfMemDocument writer; // Empty document must have page count == 0 CPPUNIT_ASSERT_EQUAL( writer.GetPageCount(), 0 ); // Retrieving any page from an empty document must be NULL PdfPage* pPage = writer.GetPagesTree()->GetPage( 0 ); CPPUNIT_ASSERT_EQUAL( pPage, static_cast<PdfPage*>(NULL) ); pPage = writer.GetPagesTree()->GetPage( -1 ); CPPUNIT_ASSERT_EQUAL( pPage, static_cast<PdfPage*>(NULL) ); pPage = writer.GetPagesTree()->GetPage( 1 ); CPPUNIT_ASSERT_EQUAL( pPage, static_cast<PdfPage*>(NULL) ); }
void PagesTreeTest::testInsert( PdfMemDocument & doc ) { const int INSERTED_PAGE_FLAG= 1234; const int INSERTED_PAGE_FLAG1= 1234 + 1; const int INSERTED_PAGE_FLAG2= 1234 + 2; PdfPage* pPage = new PdfPage( PdfPage::CreateStandardPageSize( ePdfPageSize_A4 ), &(doc.GetObjects()) ); pPage->GetObject()->GetDictionary().AddKey( PODOFO_TEST_PAGE_KEY, static_cast<long long>(INSERTED_PAGE_FLAG) ); // Insert page at the beginning doc.GetPagesTree()->InsertPage( ePdfPageInsertionPoint_InsertBeforeFirstPage, pPage ); delete pPage; // Find inserted page (beginning) pPage = doc.GetPage( 0 ); CPPUNIT_ASSERT_EQUAL( IsPageNumber( pPage, INSERTED_PAGE_FLAG ), true ); // Find old first page pPage = doc.GetPage( 1 ); CPPUNIT_ASSERT_EQUAL( IsPageNumber( pPage, 0 ), true ); // Insert at end pPage = doc.CreatePage( PdfPage::CreateStandardPageSize( ePdfPageSize_A4 ) ); pPage->GetObject()->GetDictionary().AddKey( PODOFO_TEST_PAGE_KEY, static_cast<long long>(INSERTED_PAGE_FLAG1) ); pPage = doc.GetPage( doc.GetPageCount() - 1 ); CPPUNIT_ASSERT_EQUAL( IsPageNumber( pPage, INSERTED_PAGE_FLAG1 ), true ); // Insert in middle pPage = new PdfPage( PdfPage::CreateStandardPageSize( ePdfPageSize_A4 ), &(doc.GetObjects()) ); pPage->GetObject()->GetDictionary().AddKey( PODOFO_TEST_PAGE_KEY, static_cast<long long>(INSERTED_PAGE_FLAG2) ); const int INSERT_POINT = 50; doc.GetPagesTree()->InsertPage( INSERT_POINT, pPage ); delete pPage; pPage = doc.GetPage( INSERT_POINT + 1 ); CPPUNIT_ASSERT_EQUAL( IsPageNumber( pPage, INSERTED_PAGE_FLAG2 ), true ); }
void PagesTreeTest::testGetPages( PdfMemDocument & doc ) { for(int i=0; i<PODOFO_TEST_NUM_PAGES; i++) { PdfPage* pPage = doc.GetPage( i ); CPPUNIT_ASSERT_EQUAL( pPage != NULL, true ); CPPUNIT_ASSERT_EQUAL( IsPageNumber( pPage, i ), true ); } // Now delete first page doc.GetPagesTree()->DeletePage( 0 ); for(int i=0; i<PODOFO_TEST_NUM_PAGES - 1; i++) { PdfPage* pPage = doc.GetPage( i ); CPPUNIT_ASSERT_EQUAL( pPage != NULL, true ); CPPUNIT_ASSERT_EQUAL( IsPageNumber( pPage, i + 1 ), true ); } // Now delete any page const int DELETED_PAGE = 50; doc.GetPagesTree()->DeletePage( DELETED_PAGE ); for(int i=0; i<PODOFO_TEST_NUM_PAGES - 2; i++) { PdfPage* pPage = doc.GetPage( i ); CPPUNIT_ASSERT_EQUAL( pPage != NULL, true ); if( i < DELETED_PAGE ) { CPPUNIT_ASSERT_EQUAL( IsPageNumber( pPage, i + 1 ), true ); } else { CPPUNIT_ASSERT_EQUAL( IsPageNumber( pPage, i + 2 ), true ); } } }
void PagesTreeTest::testCreateDelete() { PdfMemDocument writer; PdfPage* pPage; PdfPainter painter; PdfFont * pFont; // create font pFont = writer.CreateFont( "Arial" ); if( !pFont ) { PODOFO_RAISE_ERROR( ePdfError_InvalidHandle ); } pFont->SetFontSize( 16.0 ); // write 1. page pPage = writer.CreatePage( PdfPage::CreateStandardPageSize( ePdfPageSize_A4 ) ); painter.SetPage( pPage ); painter.SetFont( pFont ); painter.DrawText( 200, 200, "Page 1" ); painter.FinishPage(); CPPUNIT_ASSERT_EQUAL( writer.GetPageCount(), 1 ); // write 2. page pPage = writer.CreatePage( PdfPage::CreateStandardPageSize( ePdfPageSize_A4 ) ); painter.SetPage( pPage ); painter.DrawText( 200, 200, "Page 2" ); painter.FinishPage(); CPPUNIT_ASSERT_EQUAL( writer.GetPageCount(), 2 ); // try to delete second page, index is 0 based writer.DeletePages( 1, 1 ); CPPUNIT_ASSERT_EQUAL( writer.GetPageCount(), 1 ); // write 3. page pPage = writer.CreatePage( PdfPage::CreateStandardPageSize( ePdfPageSize_A4 ) ); painter.SetPage( pPage ); painter.DrawText( 200, 200, "Page 3" ); painter.FinishPage(); CPPUNIT_ASSERT_EQUAL( writer.GetPageCount(), 2 ); }
void EncryptTest::CreatedEncrypedPdf( const char* pszFilename ) { PdfMemDocument writer; PdfPage* pPage = writer.CreatePage( PdfPage::CreateStandardPageSize( ePdfPageSize_A4 ) ); PdfPainter painter; painter.SetPage( pPage ); PdfFont* pFont = writer.CreateFont( "Arial", PdfEncodingFactory::GlobalWinAnsiEncodingInstance(), false ); if( !pFont ) { PODOFO_RAISE_ERROR( ePdfError_InvalidHandle ); } pFont->SetFontSize( 16.0 ); painter.SetFont( pFont ); painter.DrawText( 100, 100, "Hello World" ); painter.FinishPage(); writer.SetEncrypted( "user", "owner" ); writer.Write( pszFilename ); printf( "Wrote: %s\n", pszFilename ); }
void PagesTreeTest::testGetPagesReverse( PdfMemDocument & doc ) { for(int i=PODOFO_TEST_NUM_PAGES-1; i>=0; i--) { PdfPage* pPage = doc.GetPage( i ); CPPUNIT_ASSERT_EQUAL( pPage != NULL, true ); CPPUNIT_ASSERT_EQUAL( IsPageNumber( pPage, i ), true ); } // Now delete first page doc.GetPagesTree()->DeletePage( 0 ); for(int i=PODOFO_TEST_NUM_PAGES-2; i>=0; i--) { PdfPage* pPage = doc.GetPage( i ); CPPUNIT_ASSERT_EQUAL( pPage != NULL, true ); CPPUNIT_ASSERT_EQUAL( IsPageNumber( pPage, i + 1 ), true ); } }
void PdfDestination::Init( PdfObject* pObject, PdfDocument* pDocument ) { bool bValueExpected = false; PdfObject* pValue = NULL; if ( pObject->GetDataType() == ePdfDataType_Array ) { m_array = pObject->GetArray(); m_pObject = pObject; } else if( pObject->GetDataType() == ePdfDataType_String ) { PdfNamesTree* pNames = pDocument->GetNamesTree( ePdfDontCreateObject ); if( !pNames ) { PODOFO_RAISE_ERROR( ePdfError_NoObject ); } pValue = pNames->GetValue( "Dests", pObject->GetString() ); bValueExpected = true; } else if( pObject->GetDataType() == ePdfDataType_Name ) { PdfMemDocument* pMemDoc = dynamic_cast<PdfMemDocument*>(pDocument); if ( !pMemDoc ) { PODOFO_RAISE_ERROR_INFO( ePdfError_InvalidHandle, "For reading from a document, only use PdfMemDocument." ); } PdfObject* pCatalog = pMemDoc->GetCatalog(); if ( !pCatalog ) { PODOFO_RAISE_ERROR( ePdfError_NoObject ); } PdfObject* pDests = pCatalog->GetIndirectKey( PdfName( "Dests" ) ); if( !pDests ) { // The error code has been chosen for its distinguishability. PODOFO_RAISE_ERROR_INFO( ePdfError_InvalidKey, "No PDF-1.1-compatible destination dictionary found." ); } pValue = pDests->GetIndirectKey( pObject->GetName() ); bValueExpected = true; } else { PdfError::LogMessage( eLogSeverity_Error, "Unsupported object given to" " PdfDestination::Init of type %s", pObject->GetDataTypeString() ); m_array = PdfArray(); // needed to prevent crash on method calls // needed for GetObject() use w/o checking its return value for NULL m_pObject = pDocument->GetObjects()->CreateObject( m_array ); } if ( bValueExpected ) { if( !pValue ) { PODOFO_RAISE_ERROR( ePdfError_InvalidName ); } if( pValue->IsArray() ) m_array = pValue->GetArray(); else if( pValue->IsDictionary() ) m_array = pValue->GetDictionary().GetKey( "D" )->GetArray(); m_pObject = pValue; } }