Document Reading Module

A unified interface for extracting text and rendering images from a wide variety of document formats. Leverages industry-standard libraries for robust document processing capabilities.

Features: 📄 Unified API · 📑 Pagination · 🖼️ Rendering · 📎 Attachments · ✂️ Smart Splitting

Supported Formats

Format Extension Reader Class Features
PDF .pdf PDFReader Text, Pagination, Rendering
Word .docx DocxReader Text, Tables
Word (Legacy) .doc DocReader Text
Excel .xlsx XlsxReader Text, Sheet-aware
Excel (Legacy) .xls XlsReader Text, Sheet-aware
PowerPoint .pptx PptxReader Text, Slide-aware, Notes
PowerPoint (Legacy) .ppt PptReader Text, Slide-aware
OpenDocument .odt OdtReader Text
Rich Text .rtf RtfReader Text
HTML .html, .htm HTMLReader Text, Pagination
Email .eml EmlReader Text, Headers, Attachments
Plain Text .txt (default) TextReader Text, Pagination

Core Interfaces

DocumentReader

Base interface for all readers. Extends AutoCloseable for proper resource management.

interface DocumentReader : AutoCloseable {
  fun getText(): String
}

PaginatedDocumentReader

Extends DocumentReader for formats that support or simulate pagination.

interface PaginatedDocumentReader : DocumentReader {
  fun getPageCount(): Int
  fun getText(startPage: Int, endPage: Int): String
}

RenderableDocumentReader

Extends DocumentReader for formats that can be rendered as images.

interface RenderableDocumentReader : DocumentReader {
  fun getPageCount(): Int
  fun renderImage(pageIndex: Int, dpi: Float): BufferedImage
}

Usage

Basic Usage

Get a reader via the File extension function:

import com.simiacryptus.cognotik.docs.getDocumentReader
import com.simiacryptus.cognotik.docs.isDocumentFile
import java.io.File

val file = File("document.pdf")
if (file.isDocumentFile()) {
  file.getDocumentReader().use { reader ->
    val text = reader.getText()
    println(text)
  }
}

Check file support with isDocumentFile():

val file = File("example.docx")
if (file.isDocumentFile()) {
  // File format is supported
}

Pagination

val reader = file.getDocumentReader()
if (reader is PaginatedDocumentReader) {
  val pageCount = reader.getPageCount()
  println("Document has $pageCount pages")

  // Get text from first page only
  val firstPageText = reader.getText(0, 1)
}

Rendering

val reader = file.getDocumentReader()
if (reader is RenderableDocumentReader) {
  val pageCount = reader.getPageCount()
  for (i in 0 until pageCount) {
    val image = reader.renderImage(i, 150f) // 150 DPI
    // Process the BufferedImage
  }
}

Configuration

The Settings data class allows you to configure behavior for certain readers:

data class Settings(
  val dpi: Float = 120f,
  val maxPages: Int = Int.MAX_VALUE,
  val outputFormat: String = "PNG",
  val fileInputs: List<String>? = null,
  val showImages: Boolean = true,
  val pagesPerBatch: Int = 1,
  val saveImageFiles: Boolean = false,
  val saveTextFiles: Boolean = false,
  val saveFinalJson: Boolean = true,
  val fastMode: Boolean = true,
  val addLineNumbers: Boolean = false
)

Example with TextReader:

val settings = Settings(addLineNumbers = true)
val reader = TextReader(file)
reader.configure(settings)
val textWithLineNumbers = reader.getText()

Reader Details

PDFReader

Implements both PaginatedDocumentReader and RenderableDocumentReader. Uses Apache PDFBox for text extraction and image rendering.

DocxReader / DocReader

Extracts text from Microsoft Word files using Apache POI.

XlsxReader / XlsReader

Extracts text from Excel spreadsheets.

PptxReader / PptReader

Extracts text from PowerPoint presentations.

HTMLReader

Parses HTML files using Jsoup.

TextReader

Reads plain text files with pagination support.

EmlReader

Parses email files using Jakarta Mail.

OdtReader / RtfReader

Dependencies

Library Used For
Apache POI .doc, .docx, .xls, .xlsx, .ppt, .pptx
Apache PDFBox PDF processing & rendering
Jsoup HTML parsing & extraction
Jakarta Mail .eml file parsing
ODF Toolkit .odt files
Java Swing RTF support (built-in)

Resource Management

All readers implement AutoCloseable. Always use use blocks or try-with-resources to ensure proper cleanup:

file.getDocumentReader().use { reader ->
  // Work with reader
} // Automatically closed

Note: The PDFReader temporarily sets the thread's context class loader during image rendering to ensure proper ImageIO service provider discovery. This is handled internally and transparent to users.