Automating Document Accessibility: Patching Raw OOXML and Parsing PDF Structure Trees with AI

Calendar iconSeptember 2, 20264 min read
Automating Document Accessibility: Patching Raw OOXML and Parsing PDF Structure Trees with AI

At NUS Technology, we specialize in building Operations Backbone Platforms and designing Complex System Integrations where failure is not an option. A major part of this commitment involves automating tedious, compliance-grade data flows that traditional SaaS platforms avoid.

Recently, our team designed an asynchronous, AI-powered document processing pipeline for an enterprise compliance platform. The objective was to automate accessibility compliance—specifically ensuring documents like PDFs, Word files (DOCX), and presentations (PPTX) conform to strict accessibility standards like PDF/UA and WCAG.

This required adding structured headings and descriptive "alt-text" for images automatically. However, doing this at scale without corrupting complex document layouts, losing semantic structure, or skyrocketing API costs required deep-level file system manipulation and a highly creative engineering strategy.


The Problem: Why Simple Rendering Engines Fail

When developing a solution to inject accessibility data (like headings and image descriptions) into documents, standard rendering libraries are often too bulky or fragile. Traditional document processing approaches suffer from two fatal flaws:

  1. The "Render Order" Trap in PDFs: Standard PDF parsers read files based on paint operators or raw visual coordinates (xObjects). While this tells you where an image or text appears on a page, it does not reflect the logical reading order. Relying on render order frequently leads to extracting the wrong images, mismatching them with their surrounding text, or destroying the reading flow for screen readers.
  2. Layout and Namespace Corruption in Office Docs: Word and PowerPoint documents are actually zipped archives containing XML files (the Office Open XML or OOXML standard). Using heavy, third-party desktop rendering engines to re-save these files often strips custom namespaces, alters complex styling, or corrupts the internal XML schema, leaving the output file unreadable.
  3. The Cost of "Dumb" AI: Relying entirely on Large Language Models (LLMs) to scan full documents and output complete, restructured files is incredibly slow, prone to formatting errors, and highly cost-prohibitive at scale.

The Architecture: A Multi-Format, Dual-Branch Pipeline

To solve these challenges, we built an asynchronous pipeline powered by Bull and Redis. This isolates the resource-heavy document parsing and AI generations from the main application thread.

Instead of treating every document the same, the pipeline splits into two highly specialized branches: one for PDFs, and one for OOXML (DOCX/PPTX) files.

Deep Dive 1: Taming PDFs via Structure Tree Traversal

To guarantee that image extraction and alt-text placement adhere strictly to accessibility standards (PDF/UA), our pipeline bypasses visual rendering order entirely and focuses on the document's semantic structure.

1. Handling Tagged PDFs

For pre-tagged PDFs, we use pdf-lib to read and traverse the document's Structure Tree directly.

  • The parser starts at the StructTreeRoot and recursively navigates down the tree.
  • It specifically locates /Figure elements, which represent semantic images in the reading flow.
  • By resolving the reference directly to the underlying image object, the pipeline extracts the raw image stream and converts it into a standardized PNG.
  • This PNG, along with its nearby textual context, is sent to OpenAI to generate highly accurate alt-text. Because the extraction is bound to the structure tree, there is zero risk of misattributing or misordering the image description.

2. The Untagged PDF Fallback

Many legacy or poorly formatted PDFs lack a tagged structure entirely. If our parser detects an untagged PDF, it triggers an automated fallback to the Adobe API. This service uses advanced layout analysis to reconstruct and convert the document into a properly Tagged PDF. Once tagged, the file is seamlessly routed back into our primary pdf-lib structure-tree pipeline.


Deep Dive 2: Safe OOXML Patching via JSZip & fast-xml-parser

For Microsoft Office files (DOCX, PPTX), our objective was to identify unstructured headings and images, determine their context, generate tags, and inject them directly into the document—all without altering the file's visual theme or corruption-sensitive XML layout.

Rather than utilizing heavy, platform-dependent SDKs, we engineered a lightweight, lightning-fast "patching" mechanism:

  1. Extracting with JSZip: Because OOXML files are essentially compressed ZIP archives, we use JSZip to extract the raw XML file structure in-memory.
  2. Fast Parsing with fast-xml-parser: We use fast-xml-parser to translate the XML into highly navigable JavaScript objects, taking care to preserve all XML namespaces and element ordering.
  3. The Hybrid Heuristic + AI Model:
    • Heuristic Pre-Filtering: Before wasting API tokens, the pipeline runs a local heuristic analysis. It scans for existing styles, outlines, font sizes, and layout patterns.
    • Targeted AI Classification: Only when a heading's hierarchy is ambiguous does the system send a prompt containing the full-text outline to OpenAI to classify the correct tag (e.g., H1, H2, or H3).
    • Contextual Alt-Text Generation: For images, we pass the image along with its neighboring text to OpenAI. This ensures the generated alt-text isn't just a generic description (e.g., "a chart"), but is highly contextualized to what the surrounding paragraph is discussing.
  4. Preservation-First Patching: Once the AI returns the generated attributes, we "patch" them directly into the exact node targets in the XML object graph. The patched object is converted back to XML and re-zipped, keeping namespaces and element orders 100% intact and immune to layout shifting.
  5. Resilience under Pressure: Built-in retry mechanisms with exponential backoffs protect the process from failing when hitting OpenAI's rate limits, ensuring stable batch operations.

The Impact: Performance, Savings, and Scalability

By combining low-level file format manipulation with a hybrid AI model, we delivered a highly optimized and reliable document compliance platform:

  • 100% Structural Precision: Navigating PDF Structure Trees and patching direct OOXML elements completely eliminated visual layout drift and document corruption.
  • High Cost-Efficiency: Utilizing a "Heuristic + AI" hybrid model allowed us to bypass OpenAI calls for straightforward elements, significantly reducing API usage costs while maintaining maximum accuracy.
  • Infinite Scalability: Offloading the entire process to an asynchronous, worker-based queue (Bull/Redis) allows the system to process massive backlogs of enterprise documents continuously without affecting web interface response times.

Is Your Enterprise Drowning in Manual Operational Workflows?

This project perfectly highlights our core engineering approach at NUS Technology: Clarity in Strategy. Excellence in Execution. We don't just build basic apps; we specialize in Workflow Automation & Operational Visibility, implementing deep integrations that handle complex compliance guidelines.

If your organization is struggling to manage compliance-grade documents, legacy data structures, or complex integrations where manual workarounds are slowing you down, you don't need generic software—you need a reliable operational backbone.

Schedule a Strategy Session with NUS Technology today to discuss how we can automate your most complex business processes.

Written By

NUS Team

Share This Article

Copied!

Read More

Live Captions With the Internet Switched Off: Inside ClearHear
Calendar iconAugust 24, 20265 min read

Live Captions With the Internet Switched Off: Inside ClearHear

How we built real-time speech recognition, speaker identification, and AI summarization that never leave the phone. A full engineering breakdown, with open source code.

Why Paperless Workflows Matter More in Field Operations
Calendar iconAugust 4, 20266 min read

Why Paperless Workflows Matter More in Field Operations

Paperless workflows matter more in field operations because paper breaks billing, compliance, and offline work. Here is what makes them actually stick.

Scaling a Data Intelligence Pipeline: Conquering Memory Leaks and AI-Powered Fuzzy Matching
Calendar iconJuly 17, 20264 min read

Scaling a Data Intelligence Pipeline: Conquering Memory Leaks and AI-Powered Fuzzy Matching

Scraping 15,000+ companies caused severe memory leaks. Discover how we fixed Heroku OOM crashes using a fan-out queue architecture, SHA-256 change detection, and a two-tier AI fuzzy matching system to build a highly scalable Data Intelligence platform.

Turn Insights into Action

Enjoying our articles?
Let’s have a strategic conversation about how these principles can
be applied to solve your specific business challenges.

Schedule a Strategy Session
CodeMonitorGrid with light