main.cpp
// open up the given PDF, and fill out the DocumentText structure with its contents
open up the given PDF, and fill out the DocumentText structure with its contents
open up the given PDF and fill out the DocumentText structure with its contents
bool ExtractPDFText(const std::string &filename, DocumentText &doctext)
{
	try
	{
		poppler::document * doc = poppler::document::load_from_file(filename, "", "");
		if(!doc)
			throw "Failed to read PDF file";
		for (std::string& key : doc->info_keys())
			doctext.metadata[key] = doc->info_key(key).to_latin1();
		doctext.pageCount = doc->pages();
		doctext.text.resize(doctext.pageCount);
		for(unsigned int pageindex = 0; pageindex < doctext.pageCount; ++pageindex)
		{
			poppler::page *pg(doc->create_page(pageindex));
			if(!pg)
				throw "Failed to open page";
			std::string pagetext = pg->text().to_latin1();
			unsigned int lineindex = 0; 
			char *linetok;
			char *linetext = strtok_r((char *)pagetext.c_str(), "\n", &linetok);
			while(linetext != NULL)
			{
				doctext.text[pageindex].resize(lineindex + 1);
				unsigned int wordindex = 0;
				char *wordtok;
				char *wordtext = strtok_r((char *)linetext, " ", &wordtok);
				while(wordtext != NULL)
				{
					doctext.text[pageindex][lineindex].push_back(wordtext);
					wordtext = strtok_r(NULL, " ", &wordtok);
					++wordindex;
				} 
				linetext = strtok_r(NULL, "\n", &linetok); 
				++lineindex;
			} 
		}
	}
	catch(const char *e)
	{
		printf("Caught exception:  %s\n", e);
		return false;
	}
	doctext.filename = filename;
	return true;
}
bool ExtractPDFText ( const std :: string & filename , DocumentText & doctext ) { try { poppler :: document * doc = poppler :: document :: load_from_file ( filename , " " , " " ) ; if ( ! doc ) throw " " ; for ( std :: string & key : doc -> info_keys ( ) ) doctext . metadata [ key ] = doc -> info_key ( key ) . to_latin1 ( ) ; doctext . pageCount = doc -> pages ( ) ; doctext . text . resize ( doctext . pageCount ) ; for ( unsigned int pageindex = 0 ; pageindex < doctext . pageCount ; ++ pageindex ) { poppler :: page * pg ( doc -> create_page ( pageindex ) ) ; if ( ! pg ) throw " " ; std :: string pagetext = pg -> text ( ) . to_latin1 ( ) ; unsigned int lineindex = 0 ; char * linetok ; char * linetext = strtok_r ( ( char * ) pagetext . c_str ( ) , " \n " , & linetok ) ; while ( linetext != NULL ) { doctext . text [ pageindex ] . resize ( lineindex + 1 ) ; unsigned int wordindex = 0 ; char * wordtok ; char * wordtext = strtok_r ( ( char * ) linetext , " " , & wordtok ) ; while ( wordtext != NULL ) { doctext . text [ pageindex ] [ lineindex ] . push_back ( wordtext ) ; wordtext = strtok_r ( NULL , " " , & wordtok ) ; ++ wordindex ; } linetext = strtok_r ( NULL , " \n " , & linetok ) ; ++ lineindex ; } } } catch ( const char * e ) { printf ( " \n " , e ) ; return false ; } doctext . filename = filename ; return true ; }
open up the given PDF, and fill out the DocumentText structure with its contents
open up the given PDF and fill out the DocumentText structure with its contents
// get document metadata by key // get page count // iterate through pages, pulling text from each // split text into lines and words