Tuesday, November 10, 2009

Bar code generation with print option

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package barcode;

/**
*
* @author amber imam
*/
import java.io.FileOutputStream;

import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.Barcode;
import com.lowagie.text.pdf.Barcode128;
import com.lowagie.text.pdf.BarcodeEAN;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
import java.awt.Color;
import java.io.FileInputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.ServiceUI;
import javax.print.SimpleDoc;
import javax.print.attribute.DocAttributeSet;
import javax.print.attribute.HashDocAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Media;
import javax.print.attribute.standard.MediaPrintableArea;
import javax.print.attribute.standard.MediaSize;

public class BarcodePrint {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Document document = new Document(PageSize.NOTE, 50, 50, 50, 50);
try {
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("test.pdf"));
document.open();
PdfContentByte cb = writer.getDirectContent();

Barcode128 code128 = new Barcode128();
SimpleDateFormat sdf = new SimpleDateFormat("ddMMyyyy");
SimpleDateFormat sdf1 = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");
int no = 5;
String data = "23456 "+no+" " + sdf.format(new Date());
code128.setCodeType(Barcode.CODE128_RAW);
code128.setCode(data.trim());
//code128.setBarHeight(10);
//code128.setSize(20);
Image image128 = code128.createImageWithBarcode(cb, Color.black, Color.blue);
Chunk park = new Chunk("Zoological Park of India , Kolkata",new Font(Font.COURIER, 8,Font.BOLD) );
//park.setBackground(Color.yellow);
//park.setTextRenderMode(PdfContentByte.TEXT_RENDER_MODE_FILL,0f,Color.red);
document.add(new Phrase(park));
document.add(new Phrase("\n"));
document.add(Chunk.NEWLINE);
document.add(new Phrase(new Chunk(image128, 0, 0)));
document.add(new Phrase("\n", new Font(Font.COURIER, 6)));
document.add(new Phrase(new Chunk(sdf1.format(new Date()) , new Font(Font.COURIER, 6, Font.BOLD))));
document.add(new Phrase("\n", new Font(Font.COURIER, 6)));
document.add(new Phrase(new Chunk("Rs-250.50/- C4" , new Font(Font.COURIER, 6, Font.BOLD))));
document.add(new Phrase("\n", new Font(Font.COURIER, 6)));
document.add(new Phrase(new Chunk("solution by : KMA infonet solution (p) Ltd." , new Font(Font.COURIER, 6, Font.BOLD))));

document.add(Chunk.NEWLINE);
document.add(Chunk.NEWLINE);

code128.setCodeType(Barcode.CODE128);
code128.setCode(data.trim());

image128 = code128.createImageWithBarcode(cb, Color.black, Color.blue);
park = new Chunk("Zoological Park of India",new Font(Font.TIMES_ROMAN, 8,Font.BOLD) );
//park.setBackground(Color.yellow);
//park.setTextRenderMode(PdfContentByte.TEXT_RENDER_MODE_FILL,0f,Color.red);
document.add(new Phrase(park));
document.add(new Phrase("\n"));
document.add(Chunk.NEWLINE);
document.add(new Phrase(new Chunk(image128, 0, 0)));
document.add(new Phrase("\n", new Font(Font.COURIER, 6)));
document.add(new Phrase(new Chunk(sdf1.format(new Date()) , new Font(Font.COURIER, 6, Font.BOLD))));
document.add(new Phrase("\n", new Font(Font.COURIER, 6)));
document.add(new Phrase(new Chunk("Rs-250.50/- C4" , new Font(Font.COURIER, 6, Font.BOLD))));
document.add(new Phrase("\n", new Font(Font.COURIER, 6)));
document.add(new Phrase(new Chunk("solution by : KMA infonet solution (p) Ltd." , new Font(Font.COURIER, 6, Font.BOLD))));


// document.add(Chunk.NEWLINE);

// UPC E
// BarcodeEAN codeEAN = new BarcodeEAN();
// document.add(new Paragraph("Barcode UPC-E"));
// codeEAN.setCodeType(Barcode.UPCE);
// codeEAN.setCode(data.trim());
// document.add(codeEAN.createImageWithBarcode(cb, null, null));
// codeEAN.setBarHeight(codeEAN.getSize() * 3f);



document.close();

String filename = "test.pdf";
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();


//pras.add(MediaSize.findMedia((float) 12, (float) 8, MediaSize.MM));
//Media printable area
// pras.add(new MediaPrintableArea(0, 0, (float) 12, (float) 8,MediaPrintableArea.MM));




DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras);
PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();

PrintService service = ServiceUI.printDialog(null, 200, 200,printService, defaultService, flavor, pras);


if (service != null) {
DocPrintJob job = service.createPrintJob();
FileInputStream fis = new FileInputStream(filename);
DocAttributeSet das = new HashDocAttributeSet();

Doc doc = new SimpleDoc(fis, flavor, das);
job.print(doc, pras);
//Thread.sleep(10000);

}
System.exit(0);

}
catch (Exception de)
{
System.out.println(de);
}
}

}

Bar Code generation with iText.jar

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package barcode;

/**
*
* @author amber imam
*/


import java.awt.Color;
import java.io.FileOutputStream;
import java.io.IOException;

import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Image;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.Barcode;
import com.lowagie.text.pdf.Barcode128;
import com.lowagie.text.pdf.Barcode39;
import com.lowagie.text.pdf.BarcodeCodabar;
import com.lowagie.text.pdf.BarcodeEAN;
import com.lowagie.text.pdf.BarcodeEANSUPP;
import com.lowagie.text.pdf.BarcodeInter25;
import com.lowagie.text.pdf.BarcodePDF417;
import com.lowagie.text.pdf.BarcodePostnet;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;

/**
* This example was written by Bruno Lowagie. It is part of the book 'iText in
* Action' by Manning Publications.
* ISBN: 1932394796
* http://www.1t3xt.com/docs/book.php
* http://www.manning.com/lowagie/
*/

public class Barcodes {

/**
* Generates a PDF file with different types of barcodes.
*
* @param args
* no arguments needed here
*/
public static void main(String[] args) {
System.out.println("Chapter 5: example Barcodes");
System.out.println("-> Creates a PDF file with barcodes.");
System.out.println("-> jars needed: iText.jar");
System.out.println("-> resulting PDF: barcodes.pdf");
// step 1: creation of a document-object
Document document = new Document();
try {
// step 2:
// we create a writer
PdfWriter writer = PdfWriter.getInstance(
// that listens to the document
document,
// and directs a PDF-stream to a file
new FileOutputStream("barcodes.pdf"));
// step 3: we open the document
document.open();
// step 4: we add a paragraph to the document
PdfContentByte cb = writer.getDirectContent();

// EAN 13
document.add(new Paragraph("Barcode EAN.UCC-13"));
BarcodeEAN codeEAN = new BarcodeEAN();
codeEAN.setCode("4512345678906");
Paragraph p = new Paragraph("default: ");
p.add(new Chunk(codeEAN.createImageWithBarcode(cb, null, null), 0,
-5));
codeEAN.setGuardBars(false);
p.add(" without guard bars: ");
p.add(new Chunk(codeEAN.createImageWithBarcode(cb, null, null), 0,
-5));
codeEAN.setBaseline(-1f);
codeEAN.setGuardBars(true);
p.add(" text above: ");
p.add(new Chunk(codeEAN.createImageWithBarcode(cb, null, null), 0,
-5));
p.setLeading(codeEAN.getBarHeight());
document.add(p);
codeEAN.setBaseline(codeEAN.getSize());

// UPC A
document.add(new Paragraph("Barcode UCC-12 (UPC-A)"));
codeEAN.setCodeType(Barcode.UPCA);
codeEAN.setCode("785342304749");
document.add(codeEAN.createImageWithBarcode(cb, null, null));

// EAN 8
document.add(new Paragraph("Barcode EAN.UCC-8"));
codeEAN.setCodeType(Barcode.EAN8);
codeEAN.setBarHeight(codeEAN.getSize() * 1.5f);
codeEAN.setCode("34569870");
document.add(codeEAN.createImageWithBarcode(cb, null, null));

// UPC E
document.add(new Paragraph("Barcode UPC-E"));
codeEAN.setCodeType(Barcode.UPCE);
codeEAN.setCode("03456781");
document.add(codeEAN.createImageWithBarcode(cb, null, null));
codeEAN.setBarHeight(codeEAN.getSize() * 3f);

// EANSUPP
document.add(new Paragraph("Bookland"));
document.add(new Paragraph("ISBN 0-321-30474-8"));
codeEAN.setCodeType(Barcode.EAN13);
codeEAN.setCode("9780321304742");
BarcodeEAN codeSUPP = new BarcodeEAN();
codeSUPP.setCodeType(Barcode.SUPP5);
codeSUPP.setCode("55499");
codeSUPP.setBaseline(-2);
BarcodeEANSUPP eanSupp = new BarcodeEANSUPP(codeEAN, codeSUPP);
document.add(eanSupp.createImageWithBarcode(cb, null, Color.blue));

// CODE 128
document.add(new Paragraph("Barcode 128"));
Barcode128 code128 = new Barcode128();
code128.setCode("0123456789 hello");
document.add(code128.createImageWithBarcode(cb, null, null));
code128.setCode("0123456789\uffffMy Raw Barcode (0 - 9)");
code128.setCodeType(Barcode.CODE128_RAW);
document.add(code128.createImageWithBarcode(cb, null, null));

// Data for the barcode :
String code402 = "24132399420058289";
String code90 = "3700000050";
String code421 = "422356";
StringBuffer data = new StringBuffer(code402);
data.append(Barcode128.FNC1);
data.append(code90);
data.append(Barcode128.FNC1);
data.append(code421);
Barcode128 shipBarCode = new Barcode128();
shipBarCode.setX(0.75f);
shipBarCode.setN(1.5f);
shipBarCode.setSize(10f);
shipBarCode.setTextAlignment(Element.ALIGN_CENTER);
shipBarCode.setBaseline(10f);
shipBarCode.setBarHeight(50f);
shipBarCode.setCode(data.toString());
document.add(shipBarCode.createImageWithBarcode(cb, Color.black,
Color.blue));

// it is composed of 3 blocks whith AI 01, 3101 and 10
Barcode128 uccEan128 = new Barcode128();
uccEan128.setCodeType(Barcode.CODE128_UCC);
uccEan128.setCode("(01)00000090311314(10)ABC123(15)060916");
document.add(uccEan128.createImageWithBarcode(cb, Color.blue,
Color.black));
uccEan128.setCode("0191234567890121310100035510ABC123");
document.add(uccEan128.createImageWithBarcode(cb, Color.blue,
Color.red));
uccEan128.setCode("(01)28880123456788");
document.add(uccEan128.createImageWithBarcode(cb, Color.blue,
Color.black));

// INTER25
document.add(new Paragraph("Barcode Interleaved 2 of 5"));
BarcodeInter25 code25 = new BarcodeInter25();
code25.setGenerateChecksum(true);
code25.setCode("41-1200076041-001");
document.add(code25.createImageWithBarcode(cb, null, null));
code25.setCode("411200076041001");
document.add(code25.createImageWithBarcode(cb, null, null));
code25.setCode("0611012345678");
code25.setChecksumText(true);
document.add(code25.createImageWithBarcode(cb, null, null));
document.newPage();

// POSTNET
document.add(new Paragraph("Barcode Postnet"));
BarcodePostnet codePost = new BarcodePostnet();
document.add(new Paragraph("ZIP"));
codePost.setCode("01234");
document.add(codePost.createImageWithBarcode(cb, null, null));
document.add(new Paragraph("ZIP+4"));
codePost.setCode("012345678");
document.add(codePost.createImageWithBarcode(cb, null, null));
document.add(new Paragraph("ZIP+4 and dp"));
codePost.setCode("01234567890");
document.add(codePost.createImageWithBarcode(cb, null, null));

document.add(new Paragraph("Barcode Planet"));
BarcodePostnet codePlanet = new BarcodePostnet();
codePlanet.setCode("01234567890");
codePlanet.setCodeType(Barcode.PLANET);
document.add(codePlanet.createImageWithBarcode(cb, null, null));

// CODE 39
document.add(new Paragraph("Barcode 3 of 9"));
Barcode39 code39 = new Barcode39();
code39.setCode("ITEXT IN ACTION");
document.add(code39.createImageWithBarcode(cb, null, null));

document.add(new Paragraph("Barcode 3 of 9 extended"));
Barcode39 code39ext = new Barcode39();
code39ext.setCode("iText in Action");
code39ext.setStartStopText(false);
code39ext.setExtended(true);
document.add(code39ext.createImageWithBarcode(cb, null, null));

// CODABAR
document.add(new Paragraph("Codabar"));
BarcodeCodabar codabar = new BarcodeCodabar();
codabar.setCode("A123A");
codabar.setStartStopText(true);
document.add(codabar.createImageWithBarcode(cb, null, null));

// PDF417
document.add(new Paragraph("Barcode PDF417"));
BarcodePDF417 pdf417 = new BarcodePDF417();
String text = "It was the best of times, it was the worst of times, "
+ "it was the age of wisdom, it was the age of foolishness, "
+ "it was the epoch of belief, it was the epoch of incredulity, "
+ "it was the season of Light, it was the season of Darkness, "
+ "it was the spring of hope, it was the winter of despair, "
+ "we had everything before us, we had nothing before us, "
+ "we were all going direct to Heaven, we were all going direct "
+ "the other way - in short, the period was so far like the present "
+ "period, that some of its noisiest authorities insisted on its "
+ "being received, for good or for evil, in the superlative degree "
+ "of comparison only.";
pdf417.setText(text);
Image img = pdf417.getImage();
img.scalePercent(50, 50 * pdf417.getYHeight());
document.add(img);

} catch (DocumentException de) {
System.err.println(de.getMessage());
} catch (IOException ioe) {
System.err.println(ioe.getMessage());
}

// step 5: we close the document
document.close();
}
}

How to call and get output from stored procedure in java

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

-- =============================================
-- Author:
-- Create date:
-- Description:
-- =============================================
ALTER PROCEDURE [dbo].[genTicketGen] ( @head varchar(10),@price varchar(20),@cno varchar(10),@issby varchar(50),@tno varchar(50) output)

AS
BEGIN
DECLARE
@dd varchar(50),
@dd1 varchar(50),
@dd2 varchar(50),
@dt varchar(50),
@seq int,
@no int;


set @seq = 0;
set @dd = (SELECT CONVERT(VARCHAR(50), GETDATE(), 10) AS [MM-DD-YYYY]);
set @dt = (SELECT CONVERT(VARCHAR(50), GETDATE(), 103) AS [DD/MM/YYYY]);
set @dd1 = @dd + ' 00:00:00 AM';
set @dd2 = @dd + ' 11:59:59 PM';


select @no = count (*) from tblGen_Ticket_Issue where issue_datetime > @dd1 and issue_datetime < @dd2

select @seq = max(sequence) from tblGen_Ticket_Issue where issue_datetime > @dd1 and issue_datetime < @dd2


if( @no = 0)
begin
set @seq =1;

end
else
begin
set @seq = @seq+1;

end

set @tno = convert(varchar(20),@seq)+' '+@head+' '+@dt;

begin
insert into tblGen_Ticket_Issue values (@tno,@seq,@head,@price,getdate(),@cno,@issby);
end

return 1
END


..........................................................................
How to call and get output from stored procedure in java
..........................................................................

Connection con = new DbConnection().getConnection();
CallableStatement cst = con.prepareCall("{call genTicketGen(?,?,?,?,?)}");

cst.setString(1,no);
cst.setString(2, price.getText());
cst.setString(3,cno);
cst.setString(4,MainFrame.Uname);

cst.registerOutParameter(5, java.sql.Types.VARCHAR);

cst.execute();

String valtno = cst.getString(5);
System.out.println(valtno);