Skip to content
DemoFAQ

PDF Generation Template Editor

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

View on GitHub

Extract PDF To JSON ​

Overview ​

Extract text, tables and images from PDF documents to Json file.

Conversion Options ​

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

OptionDefaultMeaning
json_contain_tabletrueOn: each table is output once as an HTML table with the row, column, span and text of every cell; off: the text of each cell is output as a separate text item of the page content.
contain_imagetrueOn: the output is a .zip that holds the JSON, the page renderings and the image items; off: a plain .json without image items. With enable_ocr and enable_ai_layout on, it also drops the OCR text inside image regions found by AI layout analysis.
enable_ai_layouttrueGives the object types (title, list, table, header, footer, ...), the grouping, the reading order and the table detection. Ignored if the license does not include AI layout analysis.
enable_ai_table_recognitiontrueRecognizes the structure of tables with the table model; when off, the table structure is rebuilt from the ruling lines. 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. Without OCR, image input gives only an image item (if contain_image is on) and no text. 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_classificationfalseTurns the page image upright before OCR; the page width, height and coordinates follow the rotation. 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.
page_rangesEmptyPages to convert, e.g. "1-3,5,7-9"; empty converts every page. For multi-frame image input it selects the frames.
contain_annotationtrueOutputs the appearance of annotations as image items. Only for PDF pages not recognized by OCR, and only when contain_image is on.
formula_to_imagetrueWhen on, formulas found by AI layout analysis become text items with empty text; 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.
enable_auto_page_sizefalseImage input only: the page width, height and all positions use the page size from the image resolution, matched to the nearest standard paper size.
custom_page_width / custom_page_height0Image input only: the page width, height and all positions use this exact page size in points; both must be greater than 0. Takes precedence over enable_auto_page_size.

Example:

cpp
ConvertOptions opt;
opt.json_contain_table = false;
opt.contain_image = false;
CPDFConversion::StartPDFToJson("input.pdf", "password", "path/output.json", opt);

Standard table and non-standard table ​

Commonly, tables can be divided into two categories: standard tables and non-standard tables. The specific definitions are as follows:

  • Standard table: The table border and the inner lines of the table are complete and clear. There is no need to manually add table lines to divide the table content.image-20231116145224545
  • Non-Standard Tables: Tables lacking borders or clear inner lines, requiring manual additions of table lines to separate contents.image-20231116145517818

Table Extraction Option ​

ComPDF Conversion SDK supports the option json_contain_table, when enabled, will extract table content from PDFs and output the table structure; otherwise, table content will be treated as regular text.

Notice ​

  • Without enabling AI layout analysis or OCR options, tables in the original PDF cannot be extracted. It is recommended to enable AI layout analysis or OCR for high-precision table recognition.

Sample ​

Full sample code which illustrates the text extraction capabilities.

c++
ConvertOptions opt;
// Extract PDF to JSON.
CPDFConversion::StartPDFToJson("json.pdf", "password", "path/output.json", opt);