Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/ce355c98742d47fd3d575d713c1b4f72 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/ce355c98742d47fd3d575d713c1b4f72 to your computer and use it in GitHub Desktop.
Extract Text from PDF in C++
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++;
}
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);
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);
@ajpieri
Copy link

ajpieri commented Oct 13, 2020

This doesn't seem to be working. I am just getting a blank string. Running on Ubutnu

@asadalikhan90
Copy link

@ajpieri

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