PDF Generation Template Editor
Open-source visual PDF generation engine with customizable templates and developer-friendly APIs.
Open-source visual PDF generation engine with customizable templates and developer-friendly APIs.
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.
This format uses only the following options of ConvertOptions; the other options have no effect:
| Option | Default | Meaning |
|---|---|---|
txt_table_format | true | On: 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_layout | true | Uses 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_recognition | true | Recognizes 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_accuracy | TableRecognitionAccuracy::e_Balanced | Table 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_ocr | false | Recognizes 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_option | OCROption::e_All | Which 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_count | language_count = 0 | OCR recognition languages; when no language is given, English is used. Only takes effect when enable_ocr is on. |
enable_document_orientation_classification | false | Detects the page rotation and turns the page image upright before OCR. Only takes effect when enable_ocr is on. |
enable_document_dewarp | false | Flattens curved or warped page photos before OCR. Only takes effect when enable_ocr is on. |
contain_image | true | TXT 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_image | true | When 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_page | false | Writes 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_ranges | Empty | Pages to convert, e.g. "1-3,5,7-9"; empty converts every page. For multi-frame image input it selects the frames. |
Example:
ConvertOptions opt;
opt.txt_table_format = false;
opt.formula_to_image = false;
CPDFConversion::StartPDFToTxt("input.pdf", "password", "path/output.txt", opt);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.
This sample demonstrates how to convert from a PDF to TXT file.
ConvertOptions opt;
// PDF to TXT.
CPDFConversion::StartPDFToTxt("txt.pdf", "password", "path/output.txt", opt);