If you’ve ever dealt with a corrupted PDF, you know the frustration. Maybe it won’t open, or it shows garbled text. As a developer, you can fix these issues programmatically using Java. This guide is for developers who want to automate PDF repair without relying on paid tools. If you prefer a non-programmatic approach, you can always repair PDF without software using online tools.
By the end, you’ll have a working Java program that can repair common PDF corruption issues using open-source libraries like iText or Apache PDFBox. You’ll also know how to handle edge cases and avoid common mistakes. For alternative methods on other platforms, check out our PDF repair for Windows guide.
What You’ll Need
- Java Development Kit (JDK) 8 or higher
- Apache PDFBox library (latest version)
- iText library (optional, for advanced repair)
- A corrupted PDF file to test
- Basic knowledge of Java and Maven/Gradle
Step 1: Set Up Your Java Project
Start by creating a new Maven project in your favorite IDE. Add the Apache PDFBox dependency to your pom.xml:
This library handles most low-level PDF operations. You can also use iText for more advanced repairs, but PDFBox is lighter for basic fixes.

Step 2: Load and Analyze the PDF
Write code to load the corrupted PDF using PDDocument.load(). Wrap it in a try-catch block to capture common exceptions like COSVisitorException or IOException.
If loading fails, the file likely has structural damage. Use a PDF diagnostics tool to get more details about the corruption type.

Step 3: Repair Common Issues
Common repairs include rebuilding the cross-reference table and removing broken objects. PDFBox provides utility methods for this:
This recalculates the cross-reference table and salvages readable content. For severely damaged files, you may need to extract pages individually using PDFMergerUtility. If the file is beyond repair, refer to our fix broken PDF article for alternative approaches.

Common Pitfalls
- Library version mismatches: Always use compatible versions of PDFBox and iText. Check official documentation.
- Memory issues with large files: Use MemoryUsageSetting.setupMainMemoryOnly() or stream processing.
- Overwriting original file: Always save the repaired PDF to a new file to avoid permanent data loss.
If you encounter persistent corruption, check our broken PDF repair guide for more advanced troubleshooting.
Where to Next
You’ve learned the basics of PDF repair in Java. Next, explore combining your code with a PDF diagnostics tool to automate analysis, or try repairing PDFs from other sources like email attachments. Happy coding!