Learning Outcomes
Task 1 – Include external library (iText) in Java Project
1
AST111103 Problem Solving with Programming Skills
Lab 10: Generate PDF Files from Java Applications
Use external library in Java application
GeneratePDFfilesfromJavaapplications
Format the PDF file and add image and QR code
We are writing an application that can generate receipt this week.
Download Lab10_CafeReceiptStart.zip from the Canvas and import it to your NetBeans IDE (If you have
forgotten how to import, please check the steps in previous labs). This is an extended version of Lab7.
You shall see a partially completed GUI application. We will start building our lab with this application.
The iText library is not included in Java API, it is a third party Java library. You may visit iText website at http://itextpdf.com.
We will use the iText Java library to generate PDF documents. We will go through a sample application so we can use this feature in the project. The iText library contains classes to generate PDF text in various fonts, generate different page size, add bar codes and QR code, and so on. There are many more features available with iText. It is not possible to try all of them in a single lab. We will cover the basics required for PDF generation.
Download itextpdf-5.5.3.jar from the course website and save the file in your project folder.
The first step to use external library in Java project is to
© Copyright 2018, Prepared by Dr. Lau Ho Lam. All Rights Reserved
Task 2 Write Text String in PDF file
2
AST111103 Problem Solving with Programming Skills
include it in add it to the project.
Right click on the project folder in the Netbeans IDE and select properties.
In the Project Properties window, select Libraries category and click Add JAR/Folder
Browser to the itextpdf-5.5.3.jar that you downloaded from the course website and click Open.
Click OK to close the Project Properties window. Your project is ready to use iText!
Double click the Print Receipt button in Design view and edit the code. © Copyright 2018, Prepared by Dr. Lau Ho Lam. All Rights Reserved
Display a simple receipt (target: 15 minutes)
We have been working with various GUI components. Modify the program to produce a pdf file called “receipt.pdf”, the content of this file should looks like the following picture:
3
AST111103 Problem Solving with Programming Skills
Import the following libraries at the top of the code: import com.itextpdf.text.Document; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.PdfWriter; import java.io.FileOutputStream;
Add the following code to private void jButton3ActionPerformed(…): try {
// Create new PDF document
Document document = new Document();
// Get the pdf writer stream
PdfWriter writer = PdfWriter.getInstance(document, new
FileOutputStream(“src/hello.pdf”));
// open the pdf file to write
document.open();
// write text string in the pdf file
document.add(new Paragraph(“Hello AST11103!”));
// close the pdf file
document.close();
} catch (Exception ex) { // handle all kind of possible error
System.err.println(ex);
}
Run your program and click the “Print Receipt” button. Check the hello.pdf in your “src” folder.
© Copyright 2018, Prepared by Dr. Lau Ho Lam. All Rights Reserved
If you find this difficult, you may refer to the code in private void jButton1ActionPerformed(…), and add document.add(new Paragraph(“XXXXX”)); to the corresponding items.
Task 3 Format the Receipt
4
AST111103 Problem Solving with Programming Skills
** If you are unable to finish Task 2, you may download lab10_task2.txt from the course website and copy and paste the code in jButton3ActionPerformed(…), please only copy the content of jButton3ActionPerformed(…) inside the pair of { }. **
We will format the receipt such that its appearance is tidier for the customer.
Add the two import statements at the top of your code:
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
Let us change the font style of our receipt, add the following two lines to define two different styles: Font regular = new Font(FontFamily.COURIER, 12);
Font bold = new Font(FontFamily.COURIER, 12, Font.BOLD);
Add the import statement “import com.itextpdf.text.Font;” at the top of program if necessary (Netbeans may popup to ask whether you wish to add the import lines, click OK in this case)
Modify the line for displaying the bill in Task 2:
document.add(new Paragraph(“Total……………$” + bill, bold));
Run your program and generate a pdf file and check out the result.
Besides change style, you may also make your text aligned to the centre of the page.
Modify the line that display the Café title from:
document.add(new Paragraph(“
© Copyright 2018, Prepared by Dr. Lau Ho Lam. All Rights Reserved
Task 4 Add Image QR Code to PDF file
5
AST111103 Problem Solving with Programming Skills
to
Paragraph title = new Paragraph(“
title.setAlignment(Element.ALIGN_CENTER);
document.add(title);
NetBeans may ask you to add the import line “import com.itextpdf.text.Element;”, add it.
iText library provides a lot of feature for us to change the format of our PDF document, you may explore more of its function at https://developers.itextpdf.com/content/itext-5-examples.
Add the two import statements at the top of your code: import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.BarcodeQRCode;
The receipt may be more attractive if we could add our Café logo at the top of the receipt.
Download logo.png from the course website and save it into
Add the following code under the code for adding title:
Image image1 = Image.getInstance(“src/logo.png”);
image1.setAlignment(Element.ALIGN_CENTER);
document.add(image1);
Run your program and generate a pdf file and check out the result.
Adding a QR code is a useful way to promote and help your business, add the following code to your
program after the bill:
BarcodeQRCode barcodeQRCode = new BarcodeQRCode(“https://www.facebook.com/CCCUAST/”, 1000, 1000, null);
Image codeQrImage = barcodeQRCode.getImage();
codeQrImage.scaleAbsolute(100, 100);
document.add(codeQrImage);
Run your program and generate a pdf file and check out the result.
Use your mobile phone to test the QR code, also click “like” to FB fan page.
© Copyright 2018, Prepared by Dr. Lau Ho Lam. All Rights Reserved
Task 5 Exercise – Study the program
6
AST111103 Problem Solving with Programming Skills
Study the program and make revision on how to generate pdf files and QR codes, it would be very useful in your group project.
Complete all tasks in the lab and upload your exported file to the Canvas.
Complete the exercise before next lab session and ask the instructor if you have any uncertainty.
© Copyright 2018, Prepared by Dr. Lau Ho Lam. All Rights Reserved