Fast PDF Generation in Java Project
In this article, we will introduce a comprehensive PDF toolkit for quickly generating PDFs in a Java project.
Introduction to iText
iText is a powerful open-source library designed for creating and editing PDFs, XML, and various document types.
iText’s functionality is organized into different modules, allowing you to selectively incorporate only the required modules into your project.
Here are the primary modules and their functions in iText 7:
1. Core Module (itext7-core):
This is the core module of iText, offering basic PDF creation and editing capabilities.
You can create new PDFs from scratch, add content to existing PDFs, or extract content from PDFs, among other functions.
2. Layout Module (itext7-layout):
Building upon the core module, this module adds advanced layout capabilities.
You can create complex document formats and structures, including lists, tables, and images.
3. Forms Module (itext7-forms):
This module provides PDF form-handling functionality. You can create new forms, edit existing forms, or extract form data.
4. HTML to PDF Module (html2pdf):
This module enables the conversion of HTML and CSS into PDFs.
5. Additional Modules:
iText also offers other powerful modules for encryption, font handling, color processing, and more.
Get springboot malina’s stories in your inbox
Join Medium for free to get updates from this writer.
All these modules can be used independently or in combination to help you rapidly generate PDF documents.
Key Features:
1. PDF Creation:
— Generate new PDF documents from scratch.
— Create PDFs from XML, HTML, or database content.
— Support for various page sizes and orientations.
— Add watermarks, backgrounds, headers, and footers to PDF documents.
2. PDF Editing:
— Add, delete, or edit content in existing PDFs.
— Copy, move, delete, or merge PDF pages.
— Update PDF metadata (e.g., author, title, keywords, etc.).
3. PDF Layout and Formatting:
— Add and position paragraphs, lists, tables, and images.
— Convert HTML to PDF using CSS styles.
— Utilize custom fonts and colors.
— Create interactive table of contents.
4. Document Security:
— Encrypt PDFs using passwords.
— Set various access levels and permissions.
— Sign and verify PDF documents using digital signatures.
— Support secure document annotations.
With iText, you have a powerful tool at your disposal for efficiently creating, editing, formatting, and securing PDF documents in Java.
How to use iText in Springboot project?
- We need to add the necessary dependencies.
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext7-core</artifactId>
<version>7.1.19</version>
</dependency>2. Creating PDFs
public class AddPDF {
public static void main(String[] args) {
// Define the path for the output PDF file
String outputPath = "D:/output/first.pdf";
try {
// Create a PdfWriter instance and specify the output file path
PdfWriter writer = new PdfWriter(outputPath);
// Initialize a PdfDocument using PdfWriter
PdfDocument pdf = new PdfDocument(writer);
// Initialize a Document using the PdfDocument
Document document = new Document(pdf);
// Add a simple paragraph to the Document
document.add(new Paragraph("Text content..."));
// Close the Document to complete the PDF writing
document.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}3. Editing PDFs
public class EditPDF {
public static void main(String[] args) {
// Define the paths for the source PDF file and the output PDF file
String inputPath = "C:/files/original.pdf";
String outputPath = "C:/files/editing.pdf";
try {
// Create PdfReader and PdfWriter instances, specifying input and output file paths
PdfDocument pdfDoc = new PdfDocument(new PdfReader(inputPath), new PdfWriter(outputPath);
// Initialize a Document using the PdfDocument
Document document = new Document(pdfDoc);
// Add a new paragraph to the first page of the Document
document.add(new Paragraph("New paragraph content...").setFixedPosition(100, 700, 400));
// Close the Document to complete the PDF writing
document.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}4. Converting PDFs (HTML to PDF, for instance)
We will require the dependency for the html2pdf module.
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>html2pdf</artifactId>
<version>specific_version_number</version>
</dependency>public class HTMLToPDF {
public static void main(String[] args) {
// Define the source of html file and output file
String inputPath = "D:/files/source.html";
String outputPath = "D:/files/converted.pdf";
try {
// Convertying file from html to pdf
HtmlConverter.convertToPdf(new FileInputStream(inputPath), new FileOutputStream(outputPath));
} catch (Exception e) {
e.printStackTrace();
}
}
}








