In this tutorial, we will learn to create pdf files in Java. We will first create a simple pdf file, then format that files and add a watermark.
You need to have a basic understanding of HTML, and Java for understanding this tutorial easily.
We need a third-party library to create pdf files. There are multiple libraries available that can be helpful for this purpose. Most of them are either difficult to use, lose formatting, are paid, or have performance issues. It is, therefore, very difficult to find a library that is free, provides accuracy, and has high speed.
I have found IronPDF useful for creating pdf files in Java because it prioritizes accuracy, ease of use, and speed. I will be using IronPDF throughout this tutorial, so it is better to first learn more about the features of IronPDF.
IronpDF:
IronPdf is a library developed and maintained by Iron Software that helps Software Engineers to create, edit and extract PDF content in their projects.
IronPDF is supported by Java, Kotlin, and Scala. It is developer-friendly and provides a variety of features in a single library.
Now, we need to create a sample project for demonstrating the creation of a pdf file in java.
The following are the features:
- Generating PDFs from HTML, URL, JavaScript, CSS, and many image formats
- Adding headers/footers, signatures, attachments, passwords, and security
- Performance optimization: Full Multithreading and Async support
- And many more! Visit their website to see all the code examples and a full list of their 50+ features Now, we need to create a sample project for demonstrating the creation of a pdf file in java.
Create a Project:
I am using IntelliJ IDE. You can use any. Steps for creating a new project may differ from IDE to IDE.
Open IntelliJ IDE. Click on File > New > Project from the Top Menu. The following Screen will appear.
Name your Project, Choose Project Location, Language, Build System, and JDK. Click on Create button. New Project will be created as shown below. Pom.XML will open by default.
Now, we need to install IronPDF Library for using its features.
Install IronPDF Library:
Add the following XML Content in the Pom.XML file. Don't remove the already existing content. Just place this behind the properties tag.
<dependencies>
<dependency>
<groupId>com.ironsoftware</groupId>
<artifactId>ironpdf</artifactId>
<version>2022.11.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>2.0.3</version>
</dependency>
</dependencies>
Open Terminal and enter the following command.
mvn install
It will install the library and build the project as shown below.
Now, Let's write the code to create a pdf file in Java.
Create pdf files:
Now, First of all, create a simple pdf file to understand the process, Later on, in the tutorial, we will format the file.
PdfDocument myPdf = PdfDocument.renderHtmlAsPdf("This is Sample PDF File");
// Save the PdfDocument to a file
try {
myPdf.saveAs(Paths.get("html_saved.pdf"));
} catch (IOException e) {
throw new RuntimeException(e);
}
renderHtmlAsPdf() function provided by PdfDocument Class takes a string as an argument and converts that into a pdf document.
saveAs() function takes the file path as an argument and saves the pdfs in the given path. If a document with the same name already exists, it will overwrite, else will create a new document.
Output:
Following is the output generated by our program.
Now, Let's Format our pdf files.
Format our pdf file:
In the following sample code, I have added a heading, paragraph, bold, and italics to my text. We can add any type of formatting to our pdf files using HTML tags.
public static void main(String[] args) throws IOException {
PdfDocument myPdf = PdfDocument.renderHtmlAsPdf("<h1>This is Heading</h1><p>This is <b>Paragraph</b> for creating pdf file in <i>java</i></p>");
// Save the PdfDocument to a file
try {
myPdf.saveAs(Paths.get("html_saved.pdf"));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
h1 tag will add heading, p tag will add new paragraph, b tag will bold the text and i tag will make text italic.
So, for formatting the pdf file, we need to have a basic understanding of HTML tags.
Output:
Following is the output generated by our code.
Now Let's add the list to our pdf document.
Code for creating a list in pdf documents using java:
PdfDocument myPdf = PdfDocument.renderHtmlAsPdf("<h1>This is Heading</h1><p>This is <b>Paragraph</b> for creating pdf file in <i>java</i></p><h2>An Unordered List</h2>\n" +
"\n" +
"<ul>\n" +
" <li>Coffee</li>\n" +
" <li>Tea</li>\n" +
" <li>Milk</li>\n" +
"</ul> \n" +
"\n" +
"<h2>An Ordered List</h2>\n" +
"\n" +
"<ol>\n" +
" <li>Coffee</li>\n" +
" <li>Tea</li>\n" +
" <li>Milk</li>\n" +
"</ol> ");
The above sample code will create ordered and unordered lists in our pdf document. Generated pdf is as:
Output:
It can be seen how easy is to create a pdf file using java and format it according to our requirements. We can work like just we are working with the HTML pages, and manipulate pdf documents.
We can easily add a watermark to our document via IronPDF with 1 line of code.
Add Watermark to our pdf file:
The following line of code will add a watermark to our pdf file.
myPdf.applyWatermark("MY Custom Watermark");
applyWaternark() function is provided by the Pdf Document class. It takes Watermark text and other optional arguments such as Opacity, Vertical Alignment, Horizontal Alignment, and others.
Output:
Following is the generated pdf file by our program.
Summary:
In the above tutorial, we have learned to create a pdf file, format the pdf file, add a list, and watermark in our pdf file. Apart from this, we can also create a password-protected pdf file, digitally sign pdf files, and pdf forms, add image files to pdf, extract content, and images from pdf, add a footer, headers, and many more. we cannot cover all in one tutorial. You can find more code examples from this link.
There is a watermark added in the development version of the IronPdf. you can remove this by purchasing the license or by getting a free trial for 30 days. Please click here for license information.
Top comments (0)