How To Create a PDF Document Through Java Using Apache Library
Posted By : Avaneesh Kumar Verma | 31-May-2019
Pdf is a format of file containing information that can be viewed, navigated, printed or forwarded to someone else. In this blog, we will learn how to create a pdf file using java programming and add some text, image and a table to it.
To create a pdf file, first, download and import an apache pdfbox library in your project. The link to download the pdf library is:
www.pdfbox.apache.org/download.cgi
Now to generate the pdf document we will use the following steps process:
1. The first step is creating a document.
PDDocument document = new PDDocument();
PDPage page = new PDPage(new PDRectangle(PDRectangle.A3.getHeight(), PDRectangle.A3.getWidth()));
2. The next step is to add a page to that document.
PDPage page = new PDPage(new PDRectangle(PDRectangle.A3.getHeight(), PDRectangle.A3.getWidth()));
3. The third step is to add the page to the document.
document.addPage(page);
4. The Fourth step is to add content to that page.
5. The next step is to create a File.
File file = new File(filename);
6. The last step is to save the document on that file and close it.
document.save(file);
document.close();
This will create a basic pdf Document with a single page.
The content of the pdf file can be text, image, table or something else.
Adding text to the pdf file:
The content can be added to the document using the object of the class PDPageContentStream.
final PDPageContentStream contentStream = new PDPageContentStream(document, Page);
The object of the class PDPageContentStream can be used to add text on the current page as follows:
contentStream.beginText();
contentStream.setFont(courierBoldFont, fontSize);
contentStream.newLineAtOffset(150, 750);
contentStream.showText("Hello PDFBox");
contentStream.endText();
contentStream.close();
Adding an image:
To add the image on the pdf file we shall first read it from our local system and then draw it on the page of the pdf document.
PDImageXObject class of PDDocument deals with the images. All the methods required to perform operations related to images are present in this class. Object of this class can be created using the method createFromFile().To this method, the path of the image and the name of the document is required to be passed as the parameter.
PDImageXObject pdImage = PDImageXObject.createFromFile("C:/logo.png", doc);
Inserting the content on the document can be done using the object of the class PDPageContentStream.
PDPageContentStream contentStream = new PDPageContentStream(doc, page);
contentstream.drawImage(pdImage, 70, 250);
Adding a table on the page :
The table can be added to the Pdf document using the object of the class BaseTable. The constructor of this class requires yStart, yStartNewPage, pageTopMargin, bottomMargin, tableWidth, margin,document, page, drawLines, drawContent as parameters.
BaseTable table = new BaseTable(yStart, yStartNewPage, pageTopMargin, bottomMargin, tableWidth, margin,
document, page, drawLines, drawContent);
The row and columns can be added to the table as follows:
Row headerRow = table.createRow(30f);
Cell cell = headerRow.createCell(35, "Décompte horaire des interventions");
The styling of the cells can be using the object of the class Cell as follows.
cell.setFont(PDType1Font.HELVETICA_BOLD);
cell.setFontSize(14);
cell.setLeftPadding(40);
cell.setTextColor(new Color(28, 69, 135));
This is how a basic pdf file can be created using apache pdfbox library.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Avaneesh Kumar Verma
Avaneesh is a Software developer Having Knowledge Java , J2EE ,Data Structure and Algorithm ,SQL.