Sample code for 30+ languages & platforms
Unicode C++ Requires Chilkat v11.7.0+

Extract PDF Text to a StringBuilder and Save to a File

See more PDF Text Extraction Examples

Demonstrates ExtractTextSb, which writes the extracted text to a StringBuilder instead of returning it as a JSON string. Use it for large documents. The JSON output still receives the quality report.

Chilkat Unicode C++ Downloads

Unicode C++
#include <CkPdfW.h>
#include <CkJsonObjectW.h>
#include <CkStringBuilderW.h>

void ChilkatSample(void)
    {
    bool success = false;

    //  This example requires the Chilkat API to have been previously unlocked.
    //  See Global Unlock Sample for sample code.

    //  The PDF used in this example is available at long_report.pdf

    CkPdfW pdf;
    success = pdf.LoadFile(L"qa_data/pdf/long_report.pdf");
    if (success == false) {
        wprintf(L"%s\n",pdf.lastErrorText());
        return;
    }

    wprintf(L"Number of pages: %d\n",pdf.get_NumPages());

    //  ExtractTextSb takes the same operation and options as ExtractText, but the text
    //  goes to a StringBuilder.  This avoids building a very large JSON string for
    //  documents with many pages.
    CkJsonObjectW json;
    CkStringBuilderW sbText;
    CkJsonObjectW jsonOut;

    success = pdf.ExtractTextSb(L"text",json,sbText,jsonOut);
    if (success == false) {
        wprintf(L"%s\n",pdf.lastErrorText());
        return;
    }

    wprintf(L"Extracted %d characters.\n",sbText.get_Length());

    //  Save the text to a file.
    success = sbText.WriteFile(L"qa_output/long_report.txt",L"utf-8",false);
    if (success == false) {
        wprintf(L"%s\n",sbText.lastErrorText());
        return;
    }

    //  The JSON output does not contain the text, but it still has the quality report.
    wprintf(L"Quality: %s\n",jsonOut.stringOf(L"quality.rating"));
    wprintf(L"Pages with no text (need OCR): [%s]\n",jsonOut.stringOf(L"quality.pagesNeedingOcr"));

    //  Sample output:

    //  Number of pages: 3
    //  Extracted 4522 characters.
    //  Quality: good
    //  Pages with no text (need OCR): []
    }