Friday, October 16, 2015

Generating PDF having barcode of a String (i.e. SKU), using open source APIs.

The acceptance criterion is that the generated bar-code should be able to be scanned by using any bar code scanner.
To generate Barcode, I have used “ Zxing “ API. The used jars used in the below example are: core-2.2.jar, javase.jar.
To generate PDF, I have used PDFBox  API. In this example I have used the following jars: pdfbox-1.8.10.jar, pdfbox-app-1.8.10.jar.
The above mentioned jar can be found at below link:

https://sites.google.com/site/sujeetcorp/files/PDFBox%26Zxing_jars.zip?attredirects=0&d=1

In the below example I am creating a PDF file having two pages and on each page there are two different barcode.
Here is the code:
package com.barcode;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType1Font;

import org.apache.pdfbox.pdmodel.graphics.xobject.PDPixelMap;
import org.apache.pdfbox.pdmodel.graphics.xobject.PDXObjectImage;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.oned.Code128Writer;


public class BarCode {

    public static void main (String[] args) throws Exception {
        BitMatrix bitMatrix;
             
         String outputFileName = "Simple.pdf";
         File outPutFile=new File(outputFileName);    
       //create document object
        PDDocument document = new PDDocument();
       //create a new page
        PDPage page = new PDPage(PDPage.PAGE_SIZE_A4);         
        PDRectangle rect = page.getMediaBox();
       //adding a page to PDF document          
        document.addPage(page);

        // Create a new font object selecting one of the PDF base fonts
    
        PDFont fontMono = PDType1Font.COURIER;

        // Start a new content stream which will "hold" the to be created content
        PDPageContentStream cos = new PDPageContentStream(document, page);

        int line = 0;
     // add an image
        try {   
              
               cos.beginText();
             cos.setFont(fontMono, 20);
               cos.setNonStrokingColor(Color.BLUE);
               cos.moveTextPositionByAmount(100, rect.getHeight() - 50*(++line));
             cos.drawString("APL IPHONE 5C BLUE 16GB KIT");
             cos.endText();
       
             cos.beginText();
             cos.setFont(fontMono, 20);
             cos.setNonStrokingColor(Color.BLUE);
               cos.moveTextPositionByAmount(100, rect.getHeight() - 50*(++line));
             cos.drawString("SKU:");
             cos.endText();
    
             bitMatrix = new Code128Writer().encode("M1G542LL/A", BarcodeFormat.CODE_128, 150, 80, null);         
              BufferedImage buffImg=MatrixToImageWriter.toBufferedImage(bitMatrix);          
            PDXObjectImage ximage = new PDPixelMap(document, buffImg);          
            cos.drawXObject(ximage, 150, rect.getHeight() - 50*(++line), 150, 50); 
           
            cos.beginText();
              cos.setFont(fontMono, 10);
            cos.setNonStrokingColor(Color.BLUE);
            cos.moveTextPositionByAmount(200, rect.getHeight() - 50*(++line));
              cos.drawString("M1G542LL/A");
              cos.endText();           
      
            cos.beginText();
                     cos.setFont(fontMono, 20);
            cos.setNonStrokingColor(Color.BLUE);
            cos.moveTextPositionByAmount(100, rect.getHeight() - 50*(++line));
                     cos.drawString("IMEI:");
                     cos.endText();

            bitMatrix = new Code128Writer().encode("123456789", BarcodeFormat.CODE_128, 150, 80, null);      
            buffImg=MatrixToImageWriter.toBufferedImage(bitMatrix);      
            ximage = new PDPixelMap(document, buffImg);               
            cos.drawXObject(ximage, 150, rect.getHeight() - 50*(++line), 150, 50);  
    
            cos.close(); // essential to close content stream before opening it for next page.
             
           
         // Now creating another page and embedding it into pdf document.  
             page = new PDPage(PDPage.PAGE_SIZE_A4);         
             rect = page.getMediaBox();          
             document.addPage(page);
             line = 0;

            // Start a new content stream which will "hold" the to be created content
            cos = new PDPageContentStream(document, page);         
              cos.beginText();
             cos.setFont(fontMono, 20);
               cos.setNonStrokingColor(Color.BLUE);
               cos.moveTextPositionByAmount(100, rect.getHeight() - 50*(++line));
             cos.drawString("APL IPHONE 5C BLUE 16GB KIT");
             cos.endText();
        
             cos.beginText();
             cos.setFont(fontMono, 20);
             cos.setNonStrokingColor(Color.BLUE);
               cos.moveTextPositionByAmount(100, rect.getHeight() - 60*(++line));
             cos.drawString("SKU:");
             cos.endText();
  
  
             bitMatrix = new Code128Writer().encode("M1G542LL/A", BarcodeFormat.CODE_128, 150, 80, null);       
              buffImg=MatrixToImageWriter.toBufferedImage(bitMatrix);
        
             ximage = new PDPixelMap(document, buffImg);    
             cos.drawXObject(ximage, 150, rect.getHeight() - 60*(++line), 150, 50);    
             cos.beginText();
              cos.setFont(fontMono, 10);
              cos.setNonStrokingColor(Color.BLUE);
              cos.moveTextPositionByAmount(200, rect.getHeight() - 60*(line)-10);
              cos.drawString("M1G542LL/A");
              cos.endText();
         
    
              cos.beginText();
              cos.setFont(fontMono, 20);
              cos.setNonStrokingColor(Color.BLUE);
              cos.moveTextPositionByAmount(100, rect.getHeight() - 60*(++line));
              cos.drawString("IMEI:");
              cos.endText();
             
              bitMatrix = new Code128Writer().encode("352065061762230", BarcodeFormat.CODE_128, 150, 80, null);    
              buffImg=MatrixToImageWriter.toBufferedImage(bitMatrix);  
              ximage = new PDPixelMap(document, buffImg);               
              cos.drawXObject(ximage, 150, rect.getHeight() - 60*(++line), 150, 50);           

           
        } catch (FileNotFoundException fnfex) {
            System.out.println("No image for you");
        }

        cos.close();

        document.save(outputFileName);      
        document.close();
    }

}

No comments:

Post a Comment