Java Swing is a powerful GUI toolkit for creating desktop applications. It's often used for building interactive and visually appealing programs. One of the common tasks in Swing is generating reports, which can be exported to PDF for easy sharing and printing. In this article, we'll explore Java Swing programs that generate PDF outputs, with practical examples and their respective outputs.

Before we dive into the examples, ensure you have the necessary libraries installed. For PDF generation, we'll use iText, a popular Java library. You can add it to your project using Maven with the following dependency:

```xml
Creating Simple PDF Reports
Let's start with a simple example that creates a PDF report with basic text and table data.

Here's a simple Java Swing program that generates a PDF report:
```java import com.itextpdf.text.Document; import com.itextpdf.text.pdf.PdfWriter; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.io.FileOutputStream; public class SimplePDFReport extends JFrame { public SimplePDFReport() { JButton generatePDF = new JButton("Generate PDF"); generatePDF.addActionListener(e -> { try { Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream("report.pdf")); document.open(); document.add("Simple PDF Report"); document.add("\n\nTable Data:\n"); document.add("Name\tAge\n"); document.add("John Doe\t30\n"); document.add("Jane Smith\t28\n"); document.close(); JOptionPane.showMessageDialog(this, "PDF generated successfully!"); } catch (Exception ex) { ex.printStackTrace(); } }); add(generatePDF, BorderLayout.CENTER); setDefaultCloseOperation(EXIT_ON_CLOSE); pack(); setLocationRelativeTo(null); } public static void main(String[] args) { SwingUtilities.invokeLater(() -> new SimplePDFReport().setVisible(true)); } } ```
PDF Output

The generated PDF will contain the following content:
Simple PDF Report
Table Data:
Name Age
John Doe 30
Jane Smith 28
Generating PDFs with Java Swing Tables
Java Swing's JTable can be used to display data in a tabular format. We can also use this data to generate PDFs with tables.

Here's an example that generates a PDF with a table from a JTable:
```java import com.itextpdf.text.Document; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; import javax.swing.*; import javax.swing.table.DefaultTableModel; import java.awt.*; import java.awt.event.ActionEvent; import java.io.FileOutputStream; public class SwingTableToPDF extends JFrame { // ... JTable setup and event handling code ... private void generatePDF() { try { Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream("table_report.pdf")); document.open(); PdfPTable table = new PdfPTable(tableModel.getColumnCount()); for (int i = 0; i < tableModel.getColumnCount(); i++) { table.addCell(tableModel.getColumnName(i)); } for (int i = 0; i < tableModel.getRowCount(); i++) { for (int j = 0; j < tableModel.getColumnCount(); j++) { table.addCell(String.valueOf(tableModel.getValueAt(i, j))); } } document.add(table); document.close(); JOptionPane.showMessageDialog(this, "PDF generated successfully!"); } catch (Exception ex) { ex.printStackTrace(); } } // ... rest of the code ... } ```
PDF Output
The generated PDF will contain a table with the same data as the JTable in the Swing application.

Java Swing's flexibility and iText's powerful PDF generation capabilities make it easy to create complex PDF reports from Swing applications. Whether you're creating simple text-based reports or complex tables, Java Swing and iText have you covered.
Now that you've seen the possibilities, it's time to explore and create your own PDF-generating Java Swing programs. Happy coding!



















