Ver Mensaje Individual
  #1 (permalink)  
Antiguo 08/08/2013, 12:36
abulon81
 
Fecha de Ingreso: mayo-2010
Mensajes: 99
Antigüedad: 14 años
Puntos: 5
codigo de barras itext servlet

Wenas les comparto el codigo para generar codigo de barras en un pdf mediante servlet cuando no funciona PdfWriter y PdfContentByte y usan newPage en un pdf, el codigo de barras se genero como imagen en memoria , sin necesidad de guardarlo en disco

Código:
 
private void getMensajePDF(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {.....

Document document = new Document(PageSize.A4);
		try {
			
response.setContentType("application/pdf");
PdfWriter.getInstance(document, response.getOutputStream());
writer = PdfWriter.getInstance(document,response.getOutputStream());

 document.open();
//PdfContentByte contentByte = writer.getDirectContent(); //FIXME
float width = document.getPageSize().width();
// step 4
float[] columnDefinitionSize = { 25F, 13F, 12F, 13F, 12F, 13F, 12F };
			
PdfPTable table = null;
PdfPCell cell = null;
table = new PdfPTable(columnDefinitionSize);
table.getDefaultCell().setBorder(0);
table.setHorizontalAlignment(0);
table.setTotalWidth(width - 72);
table.setLockedWidth(true);

Font font10 = FontFactory.getFont(FontFactory.HELVETICA, 10);

cell = new PdfPCell(new Phrase("C-01" , font10));
		cell.setColspan(3);
		cell.setBorderWidth(0);
		cell.setHorizontalAlignment(Element.ALIGN_JUSTIFIED);
		table.addCell(cell);
		
		//String rec = "/WebContent/img/barcodes"+ File.separator+acc+numtienda+".jpg";
        //String    imgDir = "http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + rec; //FIXME
		String acc = "222291032943225";
		Image codbar = Image.getInstance(getBarcodeImg(acc));
		//Image codbar = Image.getInstance("WebContent/img/barcodes/"+cuenta+numtienda +".jpg");
		codbar.scaleToFit(200, 200); //escala de la imagen
		codbar.setAlignment(Image.ALIGN_RIGHT); 
		
		PdfPTable tim = new PdfPTable(1);
		PdfPCell celimg;
		
		celimg = new PdfPCell(codbar);
		celimg.setBorderWidth(0);
		tim.addCell(celimg);
		
		
		cell = new PdfPCell(tim);
		cell.setColspan(4);
		cell.setBorderWidth(0);
		table.addCell(cell);
		
		cell = new PdfPCell(); //celdas vacias
		cell.setColspan(2);
		cell.setBorderWidth(0);
		table.addCell(cell);
		
		cell = new PdfPCell(new Phrase(acc, font10));
		cell.setColspan(5);
		cell.setBorderWidth(0);
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		table.addCell(cell);
		document.newPage();
		document.add(table);

        document.close();
    }

Código:
private byte[] getBarcodeImg(String cod) throws Exception {

  Barcode128 code128 = new Barcode128();
  code128.setCode(cod);
   java.awt.Image img = code128.createAwtImage(Color.BLACK, Color.WHITE);
        
  BufferedImage outImage = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB);
  outImage.getGraphics().drawImage(img, 0, 0, null);
  ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
  ImageIO.write(outImage, "jpeg", bytesOut);
  bytesOut.flush();
  byte[] jpgImageData = bytesOut.toByteArray();
  //String path = ctx.getRealPath("WebContent/img/barcodes")+ File.separator + cod+".jpg";
//       
//        System.out.println(path);
//        
//        FileOutputStream fos = new FileOutputStream(path);
//        fos.write( jpgImageData);
//        fos.flush();
//        fos.close();
//        
        //Image image = Image.getInstance(jpgImageData);
         return jpgImageData;
        
 }

Espero le sirva a alguien