Skip to content

Quick Start

This guide walks you through Kreuzberg’s core API — extracting text, handling errors, running OCR, and working with metadata. Install your binding first if you haven’t: Installation.

Pass a file path to get its text content. Kreuzberg detects the format automatically:

C
#include "kreuzberg.h"
#include <stdio.h>
int main(void) {
struct CExtractionResult *result = kreuzberg_extract_file_sync("document.pdf");
if (!result || !result->success) {
struct CErrorDetails err = kreuzberg_get_error_details();
fprintf(stderr, "Error: %s\n", err.message);
return 1;
}
printf("%s\n", result->content);
printf("MIME type: %s\n", result->mime_type);
kreuzberg_free_result(result);
return 0;
}

Wrap extractions in error handling before going further. Kreuzberg raises specific exceptions for missing files, parse failures, and OCR problems:

C
#include "kreuzberg.h"
#include <stdio.h>
int main(void) {
struct CExtractionResult *result = kreuzberg_extract_file_sync("missing.pdf");
if (!result || !result->success) {
struct CErrorDetails err = kreuzberg_get_error_details();
fprintf(stderr, "Error [%s]: %s\n",
kreuzberg_error_code_name(err.error_code),
err.message);
if (err.error_code == kreuzberg_error_code_io()) {
fprintf(stderr, "File not found or unreadable\n");
} else if (err.error_code == kreuzberg_error_code_unsupported_format()) {
fprintf(stderr, "Unsupported file format\n");
}
if (result) kreuzberg_free_result(result);
return 1;
}
printf("%s\n", result->content);
kreuzberg_free_result(result);
return 0;
}

Kreuzberg runs OCR automatically when it detects an image or scanned PDF. You can also force OCR on any document:

C
#include "kreuzberg.h"
#include <stdio.h>
int main(void) {
struct ConfigBuilder *builder = kreuzberg_config_builder_new();
kreuzberg_config_builder_set_ocr(builder,
"{\"tesseract\":{\"language\":\"eng\"}}");
ExtractionConfig *config = kreuzberg_config_builder_build(builder);
char *config_json = kreuzberg_config_to_json(config);
struct CExtractionResult *result =
kreuzberg_extract_file_sync_with_config("scanned.png", config_json);
if (result && result->success) {
printf("OCR text: %s\n", result->content);
} else {
fprintf(stderr, "OCR error: %s\n", kreuzberg_get_error_details().message);
}
kreuzberg_free_result(result);
kreuzberg_free_string(config_json);
kreuzberg_config_free(config);
return 0;
}

Pass a list of paths to extract them in parallel:

C
#include "kreuzberg.h"
#include <stdio.h>
int main(void) {
const char *files[] = {"doc1.pdf", "doc2.docx", "doc3.txt"};
uintptr_t count = 3;
struct CBatchResult *batch = kreuzberg_batch_extract_files_sync(files, count, NULL);
if (!batch) {
fprintf(stderr, "Batch error: %s\n", kreuzberg_get_error_details().message);
return 1;
}
for (uintptr_t i = 0; i < batch->count; i++) {
struct CExtractionResult *r = batch->results[i];
if (r && r->success) {
printf("--- %s ---\n%s\n", files[i], r->content);
}
}
kreuzberg_free_batch_result(batch);
return 0;
}

Every extraction result includes format-specific metadata — page count for PDFs, sheet names for Excel, dimensions for images:

C
#include "kreuzberg.h"
#include <stdio.h>
int main(void) {
struct CExtractionResult *result = kreuzberg_extract_file_sync("document.pdf");
if (!result || !result->success) {
fprintf(stderr, "Error: %s\n", kreuzberg_get_error_details().message);
return 1;
}
printf("Content: %s\n", result->content);
printf("MIME: %s\n", result->mime_type);
if (result->language)
printf("Language: %s\n", result->language);
if (result->date)
printf("Date: %s\n", result->date);
if (result->subject)
printf("Subject: %s\n", result->subject);
if (result->metadata_json)
printf("Metadata: %s\n", result->metadata_json);
kreuzberg_free_result(result);
return 0;
}

Kreuzberg extracts format-specific metadata for:

  • PDF: page count, title, authors (list), creation date, modification date
  • HTML: SEO tags, Open Graph, Twitter Card, structured data, headers, links, images
  • Excel: sheet count, sheet names
  • Email: from, to, CC, BCC, message ID, attachments
  • PowerPoint: title, author, description, fonts
  • Images: dimensions, format, EXIF data
  • Archives: format, file count, file list, sizes
  • XML: element count, unique elements
  • Text/Markdown: word count, line count, headers, links

See Types Reference for complete metadata reference.

Tables come back as both structured cells and Markdown. Kreuzberg extracts them from PDFs, spreadsheets, and HTML:

C
#include "kreuzberg.h"
#include <stdio.h>
int main(void) {
struct CExtractionResult *result = kreuzberg_extract_file_sync("spreadsheet.xlsx");
if (!result || !result->success) {
fprintf(stderr, "Error: %s\n", kreuzberg_get_error_details().message);
return 1;
}
if (result->tables_json) {
printf("Tables (JSON): %s\n", result->tables_json);
} else {
printf("No tables found\n");
}
kreuzberg_free_result(result);
return 0;
}

Use async extraction in web servers, background workers, or anywhere you need non-blocking I/O:

You’ve covered the core API. Go deeper: