How to Remove Malicious Code and JavaScript from a PDF
Question details
The user requires a secure method to inspect and sanitize untrusted PDFs by stripping out potentially malicious JavaScript, macros, and embedded active content.

- Product
- Device & OS
- not provided
- Scenario
- Preparing third-party PDFs for secure publishing or viewing without accidentally executing embedded malicious scripts or active content.
- Observed behavior
- The PDF needs to be stripped of active elements such as scripts, forms, and macros to ensure a secure, static document is generated.
Ensure your antivirus software is updated and active before handling the suspicious file. Do not open the untrusted PDF directly on a production server or computer; strongly consider handling the initial inspection within an isolated virtual environment or sandbox.
Use PDF Optimization Tools to Sanitize Content
Advanced PDF editors offer built-in optimization and redaction tools to safely remove unwanted active content while retaining document layout and text.
This method utilizes advanced preflight and optimization options to inspect the file structure and permanently strip JavaScript, forms, and embedded files without altering the main textual content.
Open advanced PDF editing software in a secure environment and navigate to the PDF Optimizer or Preflight tool suite.
Locate settings labeled 'Remove Hidden Information', 'Discard Objects', or 'Clean Up', and ensure the option to 'Discard all JavaScript actions' is actively checked.
Check additional options to remove file attachments, cross-references, and interactive form fields that could potentially harbor hidden malicious code.
Apply the optimization and save the document as a new, clean PDF file that is safe for distribution and publishing.

Disable JavaScript in Your PDF Viewer Settings
If you simply need to view a potentially unsafe PDF, you can mitigate risks by completely disabling JavaScript execution natively in your reader.
Neutralize Malicious PDFs Quickly with WPS Office
WPS Office offers a secure environment for handling documents. You can completely neutralize hidden JavaScript and malicious macros by converting an untrusted PDF into a static image-based format, ensuring 100% safe publishing and viewing.
- 1. Open the PDF in WPS Office: Launch WPS Office and safely open the untrusted PDF document within the application's secure viewer.
- 2. Access the Conversion Tools: Navigate to the 'Tools' tab on the top ribbon and select the 'PDF to Picture' feature.
- 3. Convert to Static Images: Export the document pages as JPG or PNG images. This process completely strips away the file's original code, JavaScript, and macros.
- 4. Rebuild a Secure PDF: Use the 'Picture to PDF' tool to combine those flat, secure images back into a new PDF document that is entirely safe to share and publish.

Frequently Asked Questions
What are the risks of leaving JavaScript in a PDF?
If left unchecked, malicious JavaScript within a PDF can exploit software vulnerabilities in your reader, potentially triggering malware downloads, executing unauthorized scripts, or compromising your system's security.
Can standard antivirus software remove JavaScript from PDFs?
While standard antivirus programs can scan and quarantine known malicious PDF signatures, they typically do not sanitize or selectively remove just the JavaScript. You need dedicated PDF optimization or flattening tools to strip the code while maintaining the document's structure.
Is it safe to view an untrusted PDF in a web browser?
Modern web browsers use sandboxing techniques that offer some protection against malicious PDFs. However, to guarantee safety—especially before publishing files on your own server—it is highly recommended to sanitize the document completely using optimization or flattening methods first.




