How to Diagnose and Repair Corrupted PDF & Office Documents
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 Type | Symptoms | File Doctor Diagnostic | Repair Mechanism |
|---|---|---|---|
| Prepended Junk Bytes | PDF fails to open in Acrobat | Magic bytes offset shifted | Trims corrupt prefix to restore %PDF header at offset 0 |
| Missing [Content_Types].xml | Word reports unreadable content | Corrupted OpenXML zip container | Reconstructs standard OpenXML content types manifest |
| Unclosed XML Tags | Office crashes on document open | Malformed XML syntax tree | Balances XML tag stack and repairs closing nodes |
| Disguised Executable | Security risk (malware disguise) | MZ binary header detected | Permanently blocks execution to protect user device |