Sunday, June 15, 2025
HomeJavaThe right way to create PDF File in Java

The right way to create PDF File in Java


Whats up guys, producing PDF recordsdata in at present’s enterprise purposes is kind of frequent. Doing this with Java just isn’t a straightforward activity as Java doesn’t provides default api’s to deal with PDF recordsdata. No worries, iText jar is for you. Earlier, I’ve shared about iText vs Apache FOP, two of the preferred libraries to create PDF recordsdata and at present, I’ll present you an instance of the right way to create a PDF recordsdata utilizing the iText library in Java. In case you are not acquainted,  iText is a free Java-PDF library that means that you can generate PDF recordsdata on the fly (dynamically). iText is a perfect library for builders seeking to improve web- and different purposes with dynamic PDF doc technology and/or manipulation.

By the way in which, iText just isn’t an end-user software. Sometimes you received’t apply it to your Desktop as you’ll use Acrobat or every other PDF utility. Reasonably, you’ll construct iText into your individual purposes so as to automate the PDF creation and manipulation course of.


Getting iText

You may both obtain iText jar from its house web page http://www.lowagie.com/iText/obtain.html


iText core: iText-5.5.13.jar

or you can even obtain from the Maven repository by including iText dependency in your pom.xml file. 

iText Maven Dependency


<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <model>5.5.13</model>
</dependency>

iText Gradle Dependency

compile group: 'com.itextpdf', identify: 'itextpdf', model: '5.5.13'

iText JARS :
C:m2repositorycomlowagieitext4.2.1itext-4.2.1.jar
C:m2repositoryorgbouncycastlebctsp-jdk141.38bctsp-jdk14-1.38.jar
C:m2repositoryorgbouncycastlebcprov-jdk141.38bcprov-jdk14-1.38.jar
C:m2repositoryorgbouncycastlebcmail-jdk141.38bcmail-jdk14-1.38.jar
C:m2repositoryjfreejfreechart1.0.12jfreechart-1.0.12.jar
C:m2repositoryjfreejcommon1.0.15jcommon-1.0.15.jar
C:m2repositoryorgswinglabspdf-renderer1.0.5pdf-renderer-1.0.5.jar

The right way to create PDF recordsdata/paperwork in Java – iText library Instance

How to create PDF files in Java using iText

Here’s a full code instance to generate a PDF file utilizing the iText library in Java.

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.Date;

import com.lowagie.textual content.Doc;
import com.lowagie.textual content.Paragraph;
import com.lowagie.textual content.pdf.PdfWriter;


public class Testing {

    public static void important(String args[]) {

        OutputStream file = null;
        strive {
            file = new FileOutputStream(new File("Contacts.pdf"));

            
            Doc doc = new Doc();

            
            PdfWriter.getInstance(doc, file);

            
            doc.open();

            
            doc.add(new Paragraph("Whats up World, Creating PDF 
              doc in Java is straightforward"));
            doc.add(new Paragraph("You might be buyer # 2345433"));
            doc.add(new Paragraph(new Date(new java.util.Date().getTime()).toString()));

                   
            doc.addCreationDate();
            doc.addAuthor("Javarevisited");
            doc.addTitle("The right way to create PDF doc in Java");
            doc.addCreator("Because of iText, writing into PDF is straightforward");


            
            doc.shut();

            System.out.println("Your PDF File is succesfully created");

        } catch (Exception e) {
            e.printStackTrace();

        } lastly {

            
            strive {
                if (file != null) {
                    file.shut();
                }
            } catch (IOException io) {

            }

        }

    }

}

Output:
Your PDF File is efficiently created


Including metadata/Setting attributes of PDF utilizing free Java-PDF library

Whilst you generate a PDF, chances are you’ll need to set its totally different attribute like: writer identify, title, file description and so on. iText jar may also help you to set totally different attributes of a PDF file. Doc object present totally different strategies so as to add varied attributes to a PDF file.

doc.addCreationDate();
doc.addAuthor("");
doc.addTitle("The right way to create PDF doc in Java");
doc.addCreator("Because of iText, writing into PDF is straightforward");

That is all on the right way to generate PDF recordsdata in Java utilizing iText. A lot of open supply framework makes use of iText to offer PDF assist of their utility. for instance show tag, which is a well-liked JSP tag library to generate dynamic HTML tables, let you export desk in PDF format. It does this conversion through the use of iText library, so all you want is to incorporate iText.jar in your classpath and growth, you possibly can export PDF paperwork from Java utility.

For those who like this text, chances are you’ll discover the next articles fascinating as nicely:

Thanks for studying this text to date. In case you have any suggestions or questions then please share your ideas by commenting, I extremely recognize your remark and attempt to reply as quickly as doable. 



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments