Created
April 21, 2020 09:46
-
-
Save aspose-com-gists/ce355c98742d47fd3d575d713c1b4f72 to your computer and use it in GitHub Desktop.
Extract Text from PDF in C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
auto extractor = MakeObject<Facades::PdfExtractor>(); | |
// Bind source PDF document | |
extractor->BindPdf(u"candy.pdf"); | |
// Extract text from PDF to PdfExtractor | |
extractor->ExtractText(); | |
auto unicode = System::Text::Encoding::get_Unicode(); | |
int pageNumber = 1; | |
while (extractor->HasNextPageText()) | |
{ | |
auto memStream = MakeObject<IO::MemoryStream>(); | |
extractor->GetNextPageText(memStream); | |
String text; | |
// Specify Unicode encoding type in StreamReader constructor | |
auto streamReader = MakeObject<StreamReader>(memStream, unicode); | |
streamReader->get_BaseStream()->Seek(0, SeekOrigin::Begin); | |
text = streamReader->ReadToEnd(); | |
streamReader->Dispose(); | |
// Print extracted text | |
std::cout << "Page: " << pageNumber << "\n"; | |
Console::Write(text); | |
std::cout << "\n------------------------\n"; | |
pageNumber++; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
auto extractor = MakeObject<Facades::PdfExtractor>(); | |
// Bind source PDF document | |
extractor->BindPdf(u"candy.pdf"); | |
// Set page range | |
extractor->set_StartPage(2); | |
extractor->set_EndPage(2); | |
// Extract text from PDF to PdfExtractor | |
extractor->ExtractText(); | |
auto memStream = MakeObject<IO::MemoryStream>(); | |
// Save text into memory stream | |
extractor->GetText(memStream); | |
auto unicode = System::Text::Encoding::get_Unicode(); | |
String allText = unicode->GetString(memStream->ToArray()); | |
// Print extracted text | |
Console::WriteLine(u"Extracted text:"); | |
Console::WriteLine(allText); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
auto extractor = MakeObject<Facades::PdfExtractor>(); | |
// Bind source PDF document | |
extractor->BindPdf(u"candy.pdf"); | |
// Extract text from PDF to PdfExtractor | |
extractor->ExtractText(); | |
auto memStream = MakeObject<IO::MemoryStream>(); | |
// Save text into memory stream | |
extractor->GetText(memStream); | |
auto unicode = System::Text::Encoding::get_Unicode(); | |
String allText = unicode->GetString(memStream->ToArray()); | |
// Print extracted text | |
Console::WriteLine(u"Extracted text:"); | |
Console::WriteLine(allText); |
Would you please make sure that you are using a valid license while using the API? In case you do not have one, you can apply for a free 30-days temporary license. Also, please make sure to install all windows fonts (msttcorefonts package) and libgdiplus package in the system before using the API. In case issue still persists, please create a post in Aspose.PDF support forum where we will further proceed to assist you accordingly.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This doesn't seem to be working. I am just getting a blank string. Running on Ubutnu