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.

Basic GUI in Java | Swing GUI Tutorial for Beginners
Basic GUI in Java | Swing GUI Tutorial for Beginners

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:

Java Swing/GUI Cheat Sheet
Java Swing/GUI Cheat Sheet

```xml com.itextpdf itext7-core 7.1.13 ```

Creating Simple PDF Reports

Let's start with a simple example that creates a PDF report with basic text and table data.

the press button is shown in this screenshote screen shot, and it appears to be empty
the press button is shown in this screenshote screen shot, and it appears to be empty

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

screenshot of the windows file window with text and numbers highlighted in each section,
screenshot of the windows file window with text and numbers highlighted in each section,

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.

Java Swing Tutorial
Java Swing Tutorial

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 Bootcamp London & UK – Part Time Java Developer Program
Java Bootcamp London & UK – Part Time Java Developer Program

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!

an image of a computer screen with text on it and the words public class charge
an image of a computer screen with text on it and the words public class charge
the student registration screen is shown in yellow
the student registration screen is shown in yellow
Java Cheatsheet
Java Cheatsheet
☕ Java Basics Cheat Sheet for Beginners | Learn Java in One Page 🚀
☕ Java Basics Cheat Sheet for Beginners | Learn Java in One Page 🚀
Java Swings #1  Simple Login UI
Java Swings #1 Simple Login UI
Java Swing Sign Up using Eclipse WindowBuilder
Java Swing Sign Up using Eclipse WindowBuilder
Top 5 Java Projects for Beginners
Top 5 Java Projects for Beginners
java array operations
java array operations
Structure of a Java Program + Hello World Example | Java Programming Basics 💻
Structure of a Java Program + Hello World Example | Java Programming Basics 💻
JAVA STRING METHOD
JAVA STRING METHOD
How to design a simple dashboard UI using Swing and Java -Netbeans
How to design a simple dashboard UI using Swing and Java -Netbeans
Java Collection Framework: ArrayList Explained Visually
Java Collection Framework: ArrayList Explained Visually
java programs
java programs
an image of a computer screen with the text'string a, now it is time to call '
an image of a computer screen with the text'string a, now it is time to call '
Free Java Programming Book
Free Java Programming Book
☕ Java Operators Cheat Sheet | Master Java Operators Easily 🚀
☕ Java Operators Cheat Sheet | Master Java Operators Easily 🚀
Java Programming Essentials: A Guide to Mastery with Java Course in Pune
Java Programming Essentials: A Guide to Mastery with Java Course in Pune
Java Programming Features
Java Programming Features
Logical Operators in Java: Think Smarter, Code Better ☕🚀
Logical Operators in Java: Think Smarter, Code Better ☕🚀
the top 100 programming projects list is shown in black and white, with orange lettering
the top 100 programming projects list is shown in black and white, with orange lettering