Create Calendar Event QR Code in Java iText PDF Example

In this QR code series tutorial, we will describe how to create a calendar event inside a QR Code using Java programming language. We will also describe how to add the calendar event QR code generated in Java, into a PDF document using iText API.

We will be using the following options for the event we will create in Java:

Summary
QR code for calendar event
Start Date
20121107
End Date
20121108
Location
Paris
Event Description
Java, iText, QR Code, Example Code


Note that there are also other options that you can specify when creating the code; I will be detailing them at the end of this post. This tutorial is divided into following parts:

This is part -1 of this tutorial. For other examples, refer part 2 and part 3. Please make sure you use the search at the top right of this page to locate other parts.

1) Create QR code calendar event using Java iText library and stamp the code in a PDF document
2) Return the QR code image alone for the event
3) Create a JSP servlet that will accept parameters to create a calendar QR code, and will use Java programming language to return a QR image back to the browser.

We will provide working code examples for all these cases. You may be in need of these examples for cases where you have a requirement to insert a calendar event in a PDF document using QR code. We will also provide how this code opens up in an Android device that uses QR Droid application.

The step by step code examples for all these cases are provided below:

Example -1: Creating a calendar type QR image and stamp it in a PDF document using Java iText API
 1 import java.io.FileOutputStream; 
 2 import com.itextpdf.text.Document;
 3 import com.itextpdf.text.pdf.PdfWriter;
 4 import com.itextpdf.text.Paragraph; 
 5 import com.itextpdf.text.Image;
 6 import com.itextpdf.text.pdf.BarcodeQRCode;
 7 import com.itextpdf.text.Rectangle;
 8 class QRCodeCalendar {
 9    public static void main (String[] args) throws Exception
10    {
11         //Step - 1 :Create Document Object that will map to the final document.
12         Document qr_code_Example = new Document(new Rectangle(360, 852));
13         // Step-2: Create PdfWriter object for the document
14         PdfWriter writer = PdfWriter.getInstance(qr_code_Example, new FileOutputStream("QR_PDF_Output.pdf"));
15         // Step -3: Open document for editing
16         qr_code_Example.open();            
17         //Step-4: Create New paragraph for QR Summary
18         qr_code_Example.add(new Paragraph("QR Code - Itext PDF - Calender Example"));
19         //Step-5: Define the calendar event in Java Code
20         //Note that you need to combine the static event string with the value at runtime
21         //A new line separator is required between all these lines
22         String begin="BEGIN:VEVENT";
23         String summary="SUMMARY:QR code for calendar event";
24         String startdate="DTSTART;VALUE=DATE:20121107";
25         String enddate="DTEND;VALUE=DATE:20121108";
26         String location="LOCATION:Paris";
27         String description="DESCRIPTION:Java, iText, QR Code, Example Code";
28         String endevent="END:VEVENT";
29         //Step -6: Construct one final calendar string with new line separator
30         String finala=String.format("%s%n%s%n%s%n%s%n%s%n%s%n%s%n", begin, summary, startdate, enddate,location,description,endevent);
31         //Step -7: Invoke BarcodeQRCode class to create QR Code for the calendar
32         BarcodeQRCode my_code = new BarcodeQRCode(finala, 1, 1, null);
33         //Step-8: Read output QR Code calendar object into an image
34         Image qr_image = my_code.getImage();
35         //Step-7: Stamp the QR image into the PDF document
36         qr_code_Example.add(qr_image);
37         //Step-8: Close the PDF document
38         qr_code_Example.close();
39    }
40  }
This example creates a PDF document with QR code embedded in it. A screenshot of the output code is provided below:

Create QR Code for Calendar in PDF Using Java iText Example
Create QR Code for Calendar in PDF Using Java iText Example

When you scan this QR code with a supported reader (I use QR Droid for Android), you can instantly see that the calendar event is recognized. This is shown in the screenshot below:

QR Code - Calendar - Example Scan Output in QR Droid Reader
QR Code - Calendar - Example Scan Output in QR Droid Reader
Ok, with this we have completed part 1 of this tutorial. The next part will provide a plan Java code based example to create QR code for a calendar event. Stay connected to the blog. It will be there soon!

Do you have a suggestion to improve this code? Better idea? Contribute to Thinktibits by posting in the comments section. Stay Connected.

No comments:

Post a Comment