Overview
With the continuous development of e-commerce, electronic invoices are adopted by more and more businesses, among which PDF invoices are one of the most popular ones. In this article, I’ll share with you a free and brilliant solution of creating PDF invoices in Java.
A typical invoice contains the names and addresses of customer and supplier, the invoice number, the descriptions of items purchased, the payment amount, etc. These information varies from one document to another. To generate invoices dynamically, I created a template with MS Word, in which it is easy to have your desired layout pre-defined. Then I modified the template document and saved it as a PDF file using
Free Spire.Doc for Java 2.0.0.
Create a Template in Word
As shown in the figure 1, the Word invoice template consists of three tables. Table one and table two display the seller information and buyer information. Table three specify the products or services the seller had provided the buyer. What we need to do is replace the text in the tables that starts with # and fill the third table with the customer’s purchase list.
In order to calculate the total amount automatically, some formulas have been added to the particular cells. For example, the cell D2 contains a formula of “=B2*C2”, which calculates the total price of the item that will be written in the cell A2. While we add more items (rows) to the table, we need to update the formulas of some cells dynamically.
Figure 1. Invoice Template
Download
Inovice-Template.docx
How to Replace Text in Word
Spire.Doc has a method IBodyRegion.replace(java.lang.String given, java.lang.String replace, boolean caseSensitive, boolean wholeWord) that we can use to replace a certain string in the document with a new string. For instance, to replace “#InvoiceNum” with “17854”, use the following line of code, where doc is a Document object.
How to Update an Existing Table in Word
Now, let’s see how to add rows to the existing table, how to fill the table with data and how to update formulas dynamically. To make the logic clearer, I customized three functons and made the below graph (Figure 2) to demonstrate how they function and the invoke relationship between them.
Figure 2. Custom Functions for Updating Table Three
Here come the code snippets and detailed instuctions for these three custom functions.
addRows()
This function actually duplicates the second row of the existing table and adds the duplicated rows successively next to the second row. The new rows inherit the cell format, font style as well as formula of the second row. Therefore, we need to update the formulas in the new rows in turn, and the following formulas that change as the number of rows increases.
fillTableWithData()
As its name suggests, this fucntion is simply used to write data from String[][] to the table, from the first cell of the second row.
writeDataToDocument()
Since the invoice template already has a row (the second row) for displaying the information of one item, we need to determine if it is necessary to add more rows. If the customer only purchased one item, we’re not required to do anything before inserting the pruchase data. Otherwise, we need to add rows to accommodate more items and update the formulas dynamically so as to get the correct payment amount.
The writeDataToDocument() method takes a parameter of an String[][] object, which stores one customer’s purchase information. Each element of it is a String array that can be set up like this:
The length of the String[][] is acutally the number of the items a customer purchased. If the length is greater than one, we’ll need to add rows at the number of length – 1.
Generating Invoice
Here's the code for generating PDF invoices in the main method.
The result document is showing below.
Figure 3. Invoice for Multiple Items
If you have the purchase data like this, then you’ll get the output shown in Figure 4.
Figure 4. Invoice for One Item
P.S. Free Spire.Doc for Java is capable of loading a Word document having no more than 500 paragraphs or 25 tables, and converting the first 3 pages into PDF. Most PDF invoices have only one or two pages. So, feel free to enjoy this solution, it is completely free.