QR code is getting famous because of its easy readable nature. You can take snapshot and get the data from it. Now its time to generate PDF file with full QR codes.

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

Code to generate QR code

    /**
     * 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
     */
    public static BufferedImage createQRCode(String qrCodeData, String charset,
            int qrCodeheight, int qrCodewidth) throws WriterException,
            IOException {
        // Generate BitMatrix
        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);
            }
        }
        return image;
    }

Code to generate PDF file

Document document = new Document();
        
// Get file
PdfWriter.getInstance(document, new FileOutputStream("qrcode.pdf"));
document.open();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
        
// Get QR code for "Hello"
BufferedImage bi = createQRCode("Hello","UTF-8", 200, 200);
ImageIO.write(bi, "png", baos);
baos.flush();
byte[] imageInByte = baos.toByteArray();
baos.close();
Image image = Image.getInstance(imageInByte);

// Add image to PDF document
document.add(image);
document.close();

Java class for Exporting QR Codes to PDF files

import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfWriter;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.NotFoundException;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import javax.imageio.ImageIO;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;

public class ExporToPDF {
    // color codes
    private static final int BLACK = 0xFF000000;
    private static final int WHITE = 0xFFFFFFFF;

    public static void main(String[] args) throws WriterException, IOException,
            NotFoundException {
        
        String charset = "UTF-8"; // or "ISO-8859-1"
        Document document = new Document();
        String filePath="qrcode.pdf";
        try {
            
            // Get file
            PdfWriter.getInstance(document, new FileOutputStream("qrcode.pdf"));
            document.open();
            
            // data to covert to QR code
            String[] qrdata = {"srinivas","dasari","blog.sodhanalibrary.com","pojo.sodhanalibrary.com","flaticon.sodhanalibrary.com","google","facebook","reddit","blogger","yourname"};
            float x = 100f, y = 830f, posx, posy;
            int n=0;
            for (int j = 0; j < 5; j++) {
                posy = y - j * 200f;
                for (int i = 0; i < 2; i++) {
                    posx = x + i * 200f;
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    
                    // Get QR code
                    BufferedImage bi = createQRCode(qrdata[n], charset, 200, 200);
                    ImageIO.write(bi, "png", baos);
                    baos.flush();
                    byte[] imageInByte = baos.toByteArray();
                    baos.close();
                    Image image = Image.getInstance(imageInByte);
                    image.setAbsolutePosition(posx, posy);
                    
                    // Add image to PDF document
                    document.add(image);
                    n++;
                }
            }
            document.close();
            System.out.println(new File(filePath).getAbsolutePath());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 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
     */
    public static BufferedImage createQRCode(String qrCodeData, String charset,
            int qrCodeheight, int qrCodewidth) throws WriterException,
            IOException {
        
        // Generate BitMatrix
        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);
            }
        }
        return image;
    }
    
}

4 comments:

  1. I really appreciate your blog post on how to export a QR Code to a PDF file. I like that you were able to use it as a part of the code to generate a QR code and then the code to generate a PDF file. That way, you can use the PDF file as a way to scan the QR code. I also like that you mentioned how to use a library to generate a QR code or PDF file. Free QR Code Generator

    ReplyDelete
  2. I found your blog post about exporting QR codes to PDF files in Java to be informative and helpful. I wanted to let you know that I found your blog post about libraries to be interesting as well. I'm so glad you mentioned that you need a library for generating QR codes and PDF files. I'll have to do some research and see if I can find one that works well for me.Best QR Code Generator

    ReplyDelete
  3. I'm so impressed with how much you've been able to accomplish with your library. I think that's because I know how to accomplish so little in comparison. I love the QR code generator and how you were able to make it work so quickly. I think you've done a great job with this library and I'm so happy you shared it with the world!Free Online Photo Editor

    ReplyDelete

Blogroll

Popular Posts