Quick Java to PDF Source Code Converter Tool Converting Java source code into polished PDF documents is a frequent need for developers. You might need to submit code for academic assignments, archive repositories, or share code reviews with team members.
Let’s look at how to build a lightweight command-line tool that reads a Java file and generates a clean, syntax-highlighted PDF. We will use the iText library for PDF generation and java-syntax-highlighter for processing the code tokens. Key Features of the Tool
Preserves Code Structure: Maintains original indentation and spacing.
Line Numbering: Automatically adds line numbers for easy code reference.
Custom Styling: Applies distinct colors to keywords, strings, and comments.
Page Optimization: Handles text wrapping to prevent long lines from cutting off. Step 1: Set Up Your Project Dependencies
First, configure your project build file. Add the necessary libraries to your Maven pom.xml to handle PDF creation and syntax parsing.
Use code with caution. Step 2: Implement the Converter Logic
The core application reads the input Java file line by line, identifies syntax elements, and writes them into a structured PDF document using a monospace font.
import com.itextpdf.kernel.font.PdfFont; import com.itextpdf.kernel.font.PdfFontFactory; import com.itextpdf.kernel.pdf.PdfDocument; import com.itextpdf.kernel.pdf.PdfWriter; import com.itextpdf.layout.Document; import com.itextpdf.layout.element.Paragraph; import com.itextpdf.layout.element.Text; import com.itextpdf.io.font.constants.StandardFonts; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class JavaToPdfConverter { public static void convertJavaToPdf(String inputSourcePath, String outputPdfPath) { try { // Initialize PDF writer and document PdfWriter writer = new PdfWriter(outputPdfPath); PdfDocument pdf = new PdfDocument(writer); Document document = new Document(pdf); // Use Courier font for proper code alignment PdfFont codeFont = PdfFontFactory.createFont(StandardFonts.COURIER); document.setFont(codeFont); document.setFontSize(10); BufferedReader reader = new BufferedReader(new FileReader(inputSourcePath)); String line; int lineNumber = 1; while ((line = reader.readLine()) != null) { Paragraph paragraph = new Paragraph(); // Format and append line number String numStr = String.format(“%03d “, lineNumber++); Text lineNumText = new Text(numStr).setFontColor(com.itextpdf.kernel.colors.ColorConstants.GRAY); paragraph.add(lineNumText); // Append the raw source code text Text codeText = new Text(line); paragraph.add(codeText); // Add formatted line to the document document.add(paragraph); } reader.close(); document.close(); System.out.println(“PDF generated successfully at: ” + outputPdfPath); } catch (IOException e) { System.err.println(“Error processing files: ” + e.getMessage()); } } public static void main(String[] args) { if (args.length < 2) { System.out.println(“Usage: java JavaToPdfConverter Use code with caution. Step 3: Run the Tool
Compile your program and execute it from your terminal by passing the input Java file path and your desired output PDF destination.
javac -cp “libs/” JavaToPdfConverter.java java -cp “libs/:.” JavaToPdfConverter MyScript.java output.pdf Use code with caution. Enhancing Your Tool
To elevate this utility from a basic text dumper to a professional tool, consider implementing these quick upgrades:
Theme Support: Map specific Java keywords (like public, class, void) to a theme color dictionary (e.g., Dracula or Solarized) to apply Rich Text formatting to the output.
Batch Processing: Modify the file reader to scan entire directories, letting you convert full packages into a single, indexed PDF file. If you want to customize this tool further, tell me:
The specific styling preference you want to add (e.g., dark mode theme, custom font).
The build framework you prefer using (e.g., Gradle instead of Maven).
Leave a Reply