This is the continuation of Export QR Code to PDF in Java. In this article I am going to explain how to add QR Code images to PDF table cells with Sample Example Program.

Libraries we need 

Libraries needed for QR Code generation
  1. zxing-core-1.7.jar
  2. zxing-j2se-1.7.jar
Libraries needed for PDF generation
  1. itextpdf-5.5.0

Create PDF document 

Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(filename));
document.open();

Create Table

// a table with two columns
PdfPTable table = new PdfPTable(2);
// add table to pdf document
document.add(table);

Create Table Cell

// create paragraph
Paragraph para = new Paragraph();
// create pdf cell
PdfPCell cell = new PdfPCell(para);
table.addCell(cell);

Add Image to PDF Cell

// get image instance
Image img = Image.getInstance(RESOURCE);
PdfPCell cell = new PdfPCell();
// add image to pdf cell
cell.addElement(img);

Convert BufferedImage to Image

We will get QR code image in BufferedImage format. We have to convert that into Image format and then we can add that to table cell
ByteArrayOutputStream baos = new ByteArrayOutputStream();
BufferedImage bi = createQRCode("srinivas", "UTF-8",40, 40);
ImageIO.write(bi, "png", baos);
baos.flush();
byte[] imageInByte = baos.toByteArray();
baos.close();
Image image = Image.getInstance(imageInByte);

QRCodeFirstTable.java 

Execute this file, You can see generated QR Codes in PDF Table

package sample;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import javax.imageio.ImageIO;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.itextpdf.text.BadElementException;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.qrcode.WriterException;

public class QRCodeFirstTable {

    /** The resulting PDF file. */
    public static final String RESULT = "C:/students.pdf";
    // color codes
        private static final int BLACK = 0xFF000000;
        private static final int WHITE = 0xFFFFFFFF;
     /**
     * Main method.
     * 
     * @param args
     *            no arguments needed
     * @throws DocumentException
     * @throws IOException
     * @throws com.google.zxing.WriterException 
     * @throws WriterException 
     */
    public static void main(String[] args) throws IOException,
            DocumentException, WriterException, com.google.zxing.WriterException {
        new QRCodeFirstTable().createPdf(RESULT);
    }

    /**
     * Creates a PDF with information about the movies
     * 
     * @param filename
     *            the name of the PDF file that will be created.
     * @throws DocumentException
     * @throws IOException
     * @throws com.google.zxing.WriterException 
     * @throws WriterException 
     */
    public void createPdf(String filename) throws IOException,
            DocumentException, WriterException, com.google.zxing.WriterException {
        File f=new File(filename);
        if(!f.exists()) {
            f.createNewFile();
        }
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter.getInstance(document, new FileOutputStream(filename));
        // step 3
        document.open();
        // step 4
        document.add(createMainTable());
        document.close();
        System.out.println("File was successfully saved ----- " + new File(filename).getAbsolutePath());
    }

    /**
     * Creates our first table
     * 
     * @return our first table
     * @throws IOException
     * @throws MalformedURLException
     * @throws BadElementException
     * @throws com.google.zxing.WriterException 
     * @throws WriterException 
     */
    public static PdfPTable createSubTable(boolean align_left)
            throws BadElementException, MalformedURLException, IOException, WriterException, com.google.zxing.WriterException {
        BaseColor bc = new BaseColor(150, 150, 150);
        PdfPTable table = new PdfPTable(2);
        table.setWidthPercentage(100);
        // the cell object
        PdfPCell cell, cell1, cell2;
        Font font = new Font();
        font.setColor(BaseColor.WHITE);
        Chunk c = new Chunk("Srinivas", font);
        Paragraph head = new Paragraph(c);
        head.setFont(font);
        cell = new PdfPCell(head);
        cell.setColspan(2);
        cell.setBackgroundColor(bc);
        cell.setPadding(5f);
        table.addCell(cell);
        Image img = getQRCodeImage("Srinivas", "UTF-8", 50, 50);

        font = new Font();
        font.setSize(10f);
        c = new Chunk("\nSrinivas \n\nSodhanaLibrary \n\nIndia", font);
        Paragraph body = new Paragraph(c);
        cell1 = new PdfPCell(body);
        cell1.setBorderWidthRight(0);
        cell1.setPadding(10f);

        cell2 = new PdfPCell();
        cell2.addElement(img);
        cell2.setBorderWidthLeft(0);
        cell2.setPadding(10f);

        // now we add a cell
        table.addCell(cell1);
        table.addCell(cell2);
        System.out.println("Sub Table was created");
        return table;
    }

    public static PdfPTable createMainTable() throws BadElementException,
            MalformedURLException, IOException, WriterException, com.google.zxing.WriterException {
        // a table with two columns
        PdfPTable table = new PdfPTable(2);
        System.out.println("Main Table was created");
        for (int i = 0; i < 30; i++) {
            // cell object
            PdfPCell cell1 = new PdfPCell();
            cell1.setBorderWidth(0);
            cell1.setPadding(10f);
            cell1.addElement(createSubTable(false));
            table.addCell(cell1);
        }
        return table;
    }

    /**
     * To Generate QR Code Image from text
     * 
     * @param qrCodeData
     *            - text for QR Code image
     * @param charset
     * @param qrCodeheight
     *            - QR Code image height
     * @param qrCodewidth
     *            - QR Code image width
     * @return
     * @throws WriterException
     * @throws IOException
     * @throws com.google.zxing.WriterException
     */
    public static BufferedImage createQRCode(String qrCodeData, String charset,
            int qrCodeheight, int qrCodewidth) throws WriterException,
            IOException, com.google.zxing.WriterException {
        // Generate BitMatrix
        com.google.zxing.common.BitMatrix matrix = new MultiFormatWriter()
                .encode(new String(qrCodeData.getBytes(charset), charset),
                        BarcodeFormat.QR_CODE, qrCodewidth, qrCodeheight);
        int width = matrix.getWidth();
        int height = matrix.getHeight();
        // Converting BitMatrix to Buffered Image
        BufferedImage image = new BufferedImage(width, height,
                BufferedImage.TYPE_INT_ARGB);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
            }
        }
        System.out.println("qr code image was generated for "+qrCodeData );
        return image;
    }

    public static Image getQRCodeImage(String qrCodeData, String charset,
            int qrCodeheight, int qrCodewidth) throws WriterException,
            IOException, com.google.zxing.WriterException, BadElementException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        BufferedImage bi = createQRCode("srinivas", "UTF-8",40, 40);
        ImageIO.write(bi, "png", baos);
        baos.flush();
        byte[] imageInByte = baos.toByteArray();
        baos.close();
        Image image = Image.getInstance(imageInByte);
        System.out.println("Image was generated for qr code image");
        return image;
    }

}

0 comments:

Blogroll

Follow this blog by Email

Popular Posts