Skip to content
DemoFAQ

PDF Generation Template Editor

Open-source visual PDF generation engine with customizable templates and developer-friendly APIs.

View on GitHub

Convert PDF to TXT ​

Overview ​

When you need to extract the text content in the PDF file, in order for data analysis, text mining, information retrieval, etc. Using ComPDF Convert SDK, you can easily extract the text in the PDF into the TXT file.

Conversion Options ​

This format uses only the following options of ConvertOptions; the other options have no effect:

OptionDefaultMeaning
txt_table_formattrueOn: the cells of a table row are written on one line, separated by two spaces; off: each cell is written on its own line. Only matters when the document has tables.
enable_ai_layouttrueUses AI layout analysis for reading order, paragraphs, lists, headers and footers, and table detection; when off, rule-based paragraphs and ruling-line tables are used. Ignored if the license does not include AI layout analysis.
enable_ai_table_recognitiontrueRecognizes the cell structure of tables with the table model, which decides how table text is split and ordered. Only applies to tables found by AI layout analysis (enable_ai_layout on).
table_recognition_accuracyTableRecognitionAccuracy::e_BalancedTable model: TableRecognitionAccuracy::e_Balanced uses the base documentai.model, TableRecognitionAccuracy::e_HighAccuracy uses the table_legacy.model package. Only takes effect when AI table recognition runs with the built-in table model. If the model of the requested accuracy is not loaded, the conversion still succeeds and tables fall back to rule-based recognition.
enable_ocrfalseRecognizes text with OCR on the pages chosen by ocr_option. Required for image input; without it the conversion returns ErrorCode::e_ErrUnsupportedFeature. Ignored if the license does not include OCR.
ocr_optionOCROption::e_AllWhich content OCR handles: OCROption::e_All every page; OCROption::e_ScanPage only image (scanned) pages; OCROption::e_InvalidCharacter re-recognizes unreadable characters on the other pages; OCROption::e_InvalidCharacterAndScanPage both. Only takes effect when enable_ocr is on.
languages / language_countlanguage_count = 0OCR recognition languages; when no language is given, English is used. Only takes effect when enable_ocr is on.
enable_document_orientation_classificationfalseDetects the page rotation and turns the page image upright before OCR. Only takes effect when enable_ocr is on.
enable_document_dewarpfalseFlattens curved or warped page photos before OCR. Only takes effect when enable_ocr is on.
contain_imagetrueTXT never contains images. With enable_ocr and enable_ai_layout on, OCR text inside image regions found by AI layout analysis is dropped while this is on; turn it off to keep that text.
formula_to_imagetrueWhen on, formulas found by AI layout analysis are left out of the TXT; turn it off to keep the formula text. Only applies with enable_ai_layout on.
output_document_per_pagefalseWrites one file per page instead of one document. The files are named after the input file plus the page number and are written to the folder of the output path; the file name in the output path is not used.
page_rangesEmptyPages to convert, e.g. "1-3,5,7-9"; empty converts every page. For multi-frame image input it selects the frames.

Example:

cpp
ConvertOptions opt;
opt.txt_table_format = false;
opt.formula_to_image = false;
CPDFConversion::StartPDFToTxt("input.pdf", "password", "path/output.txt", opt);

Preserving Table Format ​

The SDK supports an option called txt_table_format that preserves the table format when writing the TXT file, meaning that the original table structure is maintained. It is generally recommended to enable this option, especially useful for data extraction scenarios.

Sample ​

This sample demonstrates how to convert from a PDF to TXT file.

c++
ConvertOptions opt;
// PDF to TXT.
CPDFConversion::StartPDFToTxt("txt.pdf", "password", "path/output.txt", opt);