Document & Spreadsheet Internals

How to Diagnose and Repair Corrupted PDF & Office Documents

Published: 2026-06-24
Updated: 2026-08-27
6 min read

A technical exploration of file signature validation, truncated trailers, XML unbalance, and deterministic client-side document reconstruction with File Doctor.

Why Documents Get Corrupted

File corruption occurs when the binary structure of a file deviates from its official format specification. Common causes include interrupted downloads (premature EOF), faulty USB flash drives (bit flips), mail gateway header prepending, software crashes during save operations, or malicious file extension spoofing.

When an application like Adobe Acrobat or Microsoft Word encounters a damaged structure, it displays a generic failure message such as "The file is damaged and could not be opened", leaving users without actionable diagnostic details.

The File Doctor Diagnostic Engine

FileBuddy’s File Doctor implements a 3-tier deterministic diagnostic scanner:

1. Magic Byte Signature Verification: Reads the initial 16 bytes of the file array buffer to confirm the true container identity regardless of the file extension (e.g. `%PDF-` at byte 0, or `PK` for OpenXML zip containers). Disguised executables (.exe with .pdf name) are immediately flagged with a 0/100 security score.

2. Container Structural Integrity: For PDFs, inspects the xref offset and `%%EOF` marker. For Office documents, verifies the central zip directory and checks for mandatory manifest parts like `[Content_Types].xml` and `_rels/.rels`.

3. Document Health Scoring: Computes an objective 0 to 100 health index based on container integrity, structural readability, and stream parsing validity.

Deterministic Repair Strategies

When corruption is detected, File Doctor applies targeted, deterministic structural repairs:

PDF Stream Repair: Scans backward from the file end to find the last valid `xref` or `startxref` marker, reconstructs missing trailer dictionaries, and trims prepended gateway junk bytes before `%PDF-`.

OpenXML DOCX/PPTX/XLSX Manifest Rebuild: Reconstructs missing `[Content_Types].xml` relationship files and balances unclosed XML tags in `word/document.xml` or `ppt/presentation.xml` so Office parsers can cleanly read the underlying text.

Corruption TypeSymptomsFile Doctor DiagnosticRepair Mechanism
Prepended Junk BytesPDF fails to open in AcrobatMagic bytes offset shiftedTrims corrupt prefix to restore %PDF header at offset 0
Missing [Content_Types].xmlWord reports unreadable contentCorrupted OpenXML zip containerReconstructs standard OpenXML content types manifest
Unclosed XML TagsOffice crashes on document openMalformed XML syntax treeBalances XML tag stack and repairs closing nodes
Disguised ExecutableSecurity risk (malware disguise)MZ binary header detectedPermanently blocks execution to protect user device

Related FileBuddy Utilities