How to Flatten a PDF to Remove Hidden Layers Securely
Learn to flatten PDFs and eliminate hidden layers that leak sensitive data. Step-by-step offline methods using free tools for permanent redaction.
PDF flattening converts a multi-layer document into a single, flat image-like structure where nothing hides beneath the visible surface. This process is essential for secure redaction because hidden layers—annotations, form fields, invisible text, and embedded objects—persist in standard PDFs even when you think you've removed them.
This guide covers why flattening matters, what hidden layers actually contain, and how to flatten PDFs completely using offline tools.
Why hidden layers are a security problem
A PDF is not a flat image. It's a container format that can hold:
- Multiple content layers (often from design software like Illustrator or InDesign)
- Optional Content Groups (OCGs) that toggle visibility
- Annotations and comments (including "deleted" ones)
- Form field data (both visible entries and hidden defaults)
- Invisible text behind images (from OCR or copy-paste operations)
- Embedded files and attachments
- JavaScript and action triggers
When you place a black rectangle over sensitive text, you're adding a new layer on top—the original text remains underneath, fully selectable and extractable. This is the core failure mode described in Why Black Highlighter Redaction Fails to Secure PDFs.
Flattening eliminates this risk by rendering everything to a single visual layer and discarding the underlying structure.
What flattening actually does
True flattening rasterizes the PDF: it converts vector text, shapes, and images into pixel data at a specified resolution, then rebuilds the PDF from that rasterized output. The result:
- Text is no longer selectable (it's now pixels)
- Hidden layers cease to exist
- Annotations merge into the page image
- Form fields become static
- Metadata may persist (address separately)
The tradeoff is file size increase and loss of text searchability. For security-critical documents, this tradeoff is acceptable.
Method 1: Ghostscript (cross-platform, free)
Ghostscript is the most reliable offline tool for PDF flattening. It's available on Windows, macOS, and Linux.
Installation
macOS (Homebrew):
brew install ghostscript
Ubuntu/Debian:
sudo apt install ghostscript
Windows:
Download from ghostscript.com and add to PATH, or use the full path to gswin64c.exe.
Flatten command
gs -dNOPAUSE -dBATCH -sDEVICE=pdfwrite \
-dPDFSETTINGS=/prepress \
-dCompatibilityLevel=1.4 \
-sOutputFile=flattened.pdf \
input.pdf
This preserves reasonable quality. For true rasterization (converting to image), use:
gs -dNOPAUSE -dBATCH -sDEVICE=pdfimage24 \
-r300 \
-sOutputFile=page_%03d.png \
input.pdf
Then reassemble:
gs -dNOPAUSE -dBATCH -sDEVICE=pdfwrite \
-sOutputFile=flattened.pdf \
page_*.png
The 300 DPI setting balances quality and file size. Use 150 for smaller files or 600 for archival quality.
Verify layer removal
gs -dNOPAUSE -dBATCH -sDEVICE=txtwrite \
-sOutputFile=extracted.txt \
flattened.pdf
If the output file is empty or contains only garbled characters, text extraction is no longer possible—flattening succeeded.
Method 2: ImageMagick conversion
ImageMagick provides another rasterization path, though it requires Ghostscript as a backend for PDF handling.
convert -density 300 input.pdf -quality 100 flattened.pdf
For explicit rasterization:
convert -density 300 input.pdf page_%03d.png
convert page_*.png flattened.pdf
This approach guarantees no hidden content survives because the intermediate PNG files contain only visible pixels.
Method 3: macOS Preview (limited)
Preview can flatten some layer types but is not comprehensive.
- Open the PDF in Preview
- Go to File → Export
- Select PDF as format
- Check Quartz Filter → Reduce File Size (optional)
- Save
This removes some annotations and flattens form fields, but does not rasterize text. Hidden OCGs and invisible text may survive. For security-critical work, use Ghostscript instead.
For broader macOS redaction workflows, see How to Redact a PDF Offline on a Mac (Securely).
Method 4: qpdf for structure analysis
qpdf doesn't rasterize, but it's invaluable for inspecting what you're dealing with before and after flattening.
List all objects:
qpdf --show-object=all input.pdf
Check for Optional Content (layers):
qpdf --json input.pdf | grep -i "ocg\|optional"
Linearize and normalize:
qpdf --linearize --object-streams=disable input.pdf cleaned.pdf
Use qpdf to verify that your flattened output contains no OCG references or suspicious object types.
Handling metadata separately
Flattening removes hidden visual layers but typically preserves document metadata: author names, creation dates, software versions, GPS coordinates (if images were embedded), and revision history.
After flattening, strip metadata with exiftool:
exiftool -all= flattened.pdf
Or use mat2:
mat2 flattened.pdf
For comprehensive metadata removal, see Free Local PDF Metadata Scrubber: A Complete Guide.
The print-to-PDF fallback
If command-line tools aren't available, printing to PDF through a virtual printer forces rasterization:
macOS:
- Open PDF in any viewer
- File → Print
- Click PDF → Save as PDF
Windows:
- Open PDF
- Print → Microsoft Print to PDF
Linux (CUPS-PDF):
- Install cups-pdf
- Print to the virtual PDF printer
This method is less controllable than Ghostscript but effective for basic flattening. Resolution depends on print settings.
Common mistakes that leave layers intact
Using "Save As" instead of export/flatten: Simply saving a PDF preserves all layers and hidden content.
Relying on "Remove Hidden Information" in Acrobat: This feature is inconsistent and may miss OCGs, embedded files, or JavaScript.
Flattening annotations only: Some tools flatten form fields and comments but leave OCGs and invisible text intact.
Skipping verification: Always test the output with text extraction tools before distribution.
Verify your work
After flattening, perform these checks:
-
Text extraction test:
pdftotext flattened.pdf - | head -50Should return empty or nonsensical output if properly rasterized.
-
Layer inspection:
qpdf --json flattened.pdf | grep -c "OCG"Should return 0.
-
Visual comparison: Open both files side-by-side. The flattened version should look identical but with no selectable text.
-
File structure check:
pdfinfo flattened.pdfLook for reduced page count of embedded objects.
-
Metadata verification:
exiftool flattened.pdfShould show minimal or no metadata after stripping.
For additional verification techniques, see How to Check a PDF for Hidden Metadata.
Key takeaways
- PDF layers hide content that survives naive redaction—black boxes don't delete underlying text
- True flattening requires rasterization: converting the document to images, then back to PDF
- Ghostscript is the most reliable cross-platform tool for secure flattening
- Flattening removes visual layers but not metadata—strip both
- Always verify with text extraction and structure analysis tools
- The print-to-PDF method works as a fallback but offers less control
- Never trust "flatten" features in PDF editors without verification
Flattening is one component of secure document sanitization. Combine it with metadata removal and verification to ensure nothing sensitive survives in your exported files.