Barcode in Vaadin

Barcode in Vaadin

Author: Akshay Mathur

In one of our projects for one of our clients, we have used the Vaadin framework to develop the application. One of the requirements of this application was to Generate Barcode.

For that we created a PDF with the barcode .One way to display this PDF to the end-user is to open a new browser window. Unfortunately this was hindered by popup blockers which are not acceptable to end-users of the application. So we used Embedded and Window components to display the PDF.

Using an embedded component to display the PDF inside a native Vaadin popup window. This way, the popup with the PDF contents is not hindered by browser popup blockers because the popup is just a regular div. By using this method, the PDF window also integrated nicely with the rest of the application. The following code demonstrates how to implement this functionality:


/**
* This class creates a PDF with the iText library. This class implements
* the StreamSource interface which defines the getStream method.
*/
public class Pdf implements StreamSource {
private final ByteArrayOutputStream os = new ByteArrayOutputStream();

public Pdf() {
Document document = null;
PdfWriter writer;

try {
document = new Document (PageSize.A4);
PdfWriter.getInstance(document, os);
document.open();
document.add(new Paragraph("This is BARCODE PDF!"));
PdfContentByte cb = writer.getDirectContent();
Barcode128 barcd = new Barcode128();
barcd.setCode("10031");
Image barcode1 = barcd.createImageWithBarcode(cb, null, null);
document.add(barcode1);
}
catch (Exception e) {
e.printStackTrace();
} finally {
if (document != null) {
document.close ();
}
}
}

@Override
public InputStream getStream() {
// Here we return the pdf contents as a byte-array
return new ByteArrayInputStream(os.toByteArray());
}
}

And the code for displaying a popup with the actual PDF viewer:


Window window = new Window();
window.setWidth("900px");
window.setHeight("600px");
window.setModal(true);
String Title = "Barcode PDF";
e = new Embedded();
e.setWidth("850px");
e.setHeight("550px");
e.window.center();
e.setType(Embedded.TYPE_BROWSER);
StreamResource resource = new StreamResource(new Pdf(), Title+".pdf?", this);
resource.setMIMEType("application/pdf");
e.setSource(resource);
window.addComponent(e);
getMainWindow().addWindow(window);

Note: For proper display of Barcode PDF the filename of PDF must be Different.

We can also use PDF in the application to print content. The following code demonstrates how to implement this functionality:


/**
* This class creates a PDF with the iText library. This class implements
* the StreamSource interface which defines the getStream method.
*/
public class Pdf implements StreamSource {
private final ByteArrayOutputStream os = new ByteArrayOutputStream();
PdfWriter writer ;
public Pdf() {
Document document = null;
PdfPTable table = null;

try {
document = new Document(PageSize.A4);
writer = PdfWriter.getInstance(document, os);
document.open();
Paragraph preface = new Paragraph("Loyalty Information",
FontFactory.getFont(FontFactory.TIMES_ROMAN , 20,Font.BOLD));
preface.setAlignment(Element.ALIGN_CENTER);
document.add(preface);

Paragraph p1 = new Paragraph("Loyalty Number : 10031",
FontFactory.getFont(FontFactory.TIMES_ROMAN, 15));
p1.setAlignment(Element.ALIGN_LEFT);
p1.setSpacingBefore(30);
p1.setSpacingAfter(20);
document.add(p1);

Paragraph p2 = new Paragraph("Barcode : ",FontFactory.getFont(FontFactory.TIMES_ROMAN, 15));
p2.setAlignment(Element.ALIGN_LEFT);
p2.setSpacingAfter(60);
document.add(p2);

PdfContentByte cb = writer.getDirectContent();
Barcode128 barcd = new Barcode128();
barcd.setCode("10031");
Image barcode = barcd.createImageWithBarcode(cb, null, null);
barcode.scaleToFit(80,40);
barcode.setAbsolutePosition(100, 658);
document.add(barcode);

table = new PdfPTable(3);// number of columns in table
table.setWidthPercentage(95);
table.setSpacingAfter(7);

// the cell object

table.addCell(new Phrase("Serial Number",FontFactory.getFont(FontFactory.TIMES_ROMAN, 13,Font.BOLD)));
table.addCell(new Phrase("Name",FontFactory.getFont(FontFactory.TIMES_ROMAN, 13,Font.BOLD)));
table.addCell(new Phrase("Date of Birth",FontFactory.getFont(FontFactory.TIMES_ROMAN, 13,Font.BOLD)));
table.addCell(new Phrase("1",FontFactory.getFont(FontFactory.TIMES_ROMAN,12)));
table.addCell(new Phrase("TEST",FontFactory.getFont(FontFactory.TIMES_ROMAN,12)));
table.addCell(new Phrase("12/11/1976",FontFactory.getFont(FontFactory.TIMES_ROMAN,12)));
table.addCell(new Phrase("2",FontFactory.getFont(FontFactory.TIMES_ROMAN,12)));
table.addCell(new Phrase("TEST",FontFactory.getFont(FontFactory.TIMES_ROMAN,12)));
table.addCell(new Phrase("09/20/1992",FontFactory.getFont(FontFactory.TIMES_ROMAN,12)));
table.addCell(new Phrase("3",FontFactory.getFont(FontFactory.TIMES_ROMAN,12)));
table.addCell(new Phrase("TEST",FontFactory.getFont(FontFactory.TIMES_ROMAN,12)));
table.addCell(new Phrase("07/02/1995",FontFactory.getFont(FontFactory.TIMES_ROMAN,12)));
table.addCell(new Phrase("4",FontFactory.getFont(FontFactory.TIMES_ROMAN,12)));
table.addCell(new Phrase("TEST",FontFactory.getFont(FontFactory.TIMES_ROMAN,12)));
table.addCell(new Phrase("20/09/1998",FontFactory.getFont(FontFactory.TIMES_ROMAN,12)));
document.add(table);

} catch (Exception e) {
e.printStackTrace();
} finally {
if (document != null) {
document.close();
}
}
}

@Override
public InputStream getStream() {
// Here we return the pdf contents as a byte-array
return new ByteArrayInputStream(os.toByteArray());
}
}

Screenshots

Number Entered



Popup PDF Window

0 comments:

Post a Comment