private Image generateBarCode() { try { Barcode128 code128 = new Barcode128(); code128.setSize(12f); code128.setBaseline(12f); code128.setCode(barcode); Image img = code128.createImageWithBarcode(writer.getDirectContent(), null, null); img.scalePercent(50, 50); //img.setAbsolutePosition(PageSize.A4.width() - img.width() - 10,10); //document.add(img); return img; }catch (Exception ex){ ex.printStackTrace(); return null; } }
/** * M�todo que permite introducir un c�digo de barras code128 que represente el texto * que se pasa como par�metro. Se debe indicar la p�gina del PDF donde se debe introducir * el c�digo (normlamente la p�gina 1) y la posici�n absoluta X e Y dentro de la p�gina. */ public void establecerBarCode (int Pagina, String texto, int XPos, int YPos) throws Exception { Barcode128 code128 = new Barcode128(); code128.setSize(12f); code128.setBaseline(12f); code128.setCode(texto); Image img = code128.createImageWithBarcode(pdfs.getOverContent(Pagina), null, null); img.setAbsolutePosition(XPos, YPos); //Se hace un poco mas peque�o en la escala X para que no ocupe tanto img.scalePercent(75, 100); pdfs.getOverContent(Pagina).addImage(img); }
/** * Generate a Code128C barcode * * @param rrn * unique Rijksregister number * @param cardNumber * number of the card * @return Image containing barcode * @throws IOException * @throws BadElementException */ private Image createBarcodeImage(String rrn, String cardNumber) throws IOException, BadElementException { if (null == rrn || rrn.length() != 11 || null == cardNumber || cardNumber.length() < 9) { throw new IllegalArgumentException("Missing or invalid length for RRN or Card Number"); } String lastDigits = cardNumber.substring(cardNumber.length() - 9); String code = rrn + lastDigits; Barcode128 barcode = new Barcode128(); barcode.setCodeType(Barcode128.CODE_C); barcode.setCode(code); barcode.setFont(null); return Image.getInstance(barcode.createAwtImage(Color.BLACK, Color.WHITE), null, true); }
public void addBarcode(String barcode, int x, int y) throws ParserException { try { Barcode128 code128 = new Barcode128(); code128.setCode(barcode); PdfContentByte cb = stamp.getOverContent(1); Image image128 = Image.getInstance(code128.createImageWithBarcode(cb, null, null)); cb.addImage(image128, image128.width(), 0, 0, image128.height(), x, y); cb.moveTo(0, 0); } catch (DocumentException e) { throw new ParserException("Could not add barcode", e); } }
/** * Generate barcode in the form * * @param pdfStamper * @throws DocumentException */ private void generateBarcode(PdfStamper pdfStamper, String userId) throws DocumentException { // add barcode on the first page PdfContentByte cb = pdfStamper.getOverContent(BARCODE_PAGE); // barcode format 128C Barcode128 code128 = new Barcode128(); // barcode format e.g. *1502A1234567890 // asterisk - * [constant] // WebPOS Transaction - 1502 [constant] // Form Version - A [constant for MyPost 1.5] // 10-digit APCN code128.setCode(ASTERISK + WEBPOS_TRANSACTIION + MYPOST_FORM_VERSION + userId); code128.setCodeType(Barcode128.CODE128); // convert barcode into image Image code128Image = code128.createImageWithBarcode(cb, null, null); // set barcode position x pixel, y pixel code128Image.setAbsolutePosition(BARCODE_POSITION_X, BARCODE_POSITION_Y); code128Image.scalePercent(BARCODE_SCALE_PERCENTAGE); // add barcode image into PDF template cb.addImage(code128Image); }
/** * List with different Barcode types. */ @Test public void main() throws Exception { // step 1: creation of a document-object Document document = new Document(PageSize.A4, 50, 50, 50, 50); // step 2: creation of the writer PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("barcodes.pdf")); // step 3: we open the document document.open(); // step 4: we add content to the document PdfContentByte cb = writer.getDirectContent(); Barcode39 code39 = new Barcode39(); code39.setCode("CODE39-1234567890"); code39.setStartStopText(false); Image image39 = code39.createImageWithBarcode(cb, null, null); Barcode39 code39ext = new Barcode39(); code39ext.setCode("The willows."); code39ext.setStartStopText(false); code39ext.setExtended(true); Image image39ext = code39ext.createImageWithBarcode(cb, null, null); Barcode128 code128 = new Barcode128(); code128.setCode("1Z234786 hello"); Image image128 = code128.createImageWithBarcode(cb, null, null); BarcodeEAN codeEAN = new BarcodeEAN(); codeEAN.setCodeType(Barcode.EAN13); codeEAN.setCode("9780201615883"); Image imageEAN = codeEAN.createImageWithBarcode(cb, null, null); BarcodeInter25 code25 = new BarcodeInter25(); code25.setGenerateChecksum(true); code25.setCode("41-1200076041-001"); Image image25 = code25.createImageWithBarcode(cb, null, null); BarcodePostnet codePost = new BarcodePostnet(); codePost.setCode("12345"); Image imagePost = codePost.createImageWithBarcode(cb, null, null); BarcodePostnet codePlanet = new BarcodePostnet(); codePlanet.setCode("50201402356"); codePlanet.setCodeType(Barcode.PLANET); Image imagePlanet = codePlanet.createImageWithBarcode(cb, null, null); BarcodeEAN codeSUPP = new BarcodeEAN(); codeSUPP.setCodeType(Barcode.SUPP5); codeSUPP.setCode("54995"); codeSUPP.setBaseline(-2); BarcodeEANSUPP eanSupp = new BarcodeEANSUPP(codeEAN, codeSUPP); Image imageEANSUPP = eanSupp.createImageWithBarcode(cb, null, Color.blue); PdfPTable table = new PdfPTable(2); table.setWidthPercentage(100); table.getDefaultCell().setBorder(Rectangle.NO_BORDER); table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER); table.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE); table.getDefaultCell().setFixedHeight(70); table.addCell("CODE 39"); table.addCell(new Phrase(new Chunk(image39, 0, 0))); table.addCell("CODE 39 EXTENDED"); table.addCell(new Phrase(new Chunk(image39ext, 0, 0))); table.addCell("CODE 128"); table.addCell(new Phrase(new Chunk(image128, 0, 0))); table.addCell("CODE EAN"); table.addCell(new Phrase(new Chunk(imageEAN, 0, 0))); table.addCell("CODE EAN\nWITH\nSUPPLEMENTAL 5"); table.addCell(new Phrase(new Chunk(imageEANSUPP, 0, 0))); table.addCell("CODE INTERLEAVED"); table.addCell(new Phrase(new Chunk(image25, 0, 0))); table.addCell("CODE POSTNET"); table.addCell(new Phrase(new Chunk(imagePost, 0, 0))); table.addCell("CODE PLANET"); table.addCell(new Phrase(new Chunk(imagePlanet, 0, 0))); document.add(table); // step 5: we close the document document.close(); }
/** * Example Barcode EAN128. */ @Test public void main() throws Exception { // step 1 Document document = new Document(); // step 2 PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("ean128.pdf")); // step 3 document.open(); // step 4 PdfContentByte cb = writer.getDirectContent(); PdfPTable pageTot = new PdfPTable(1); pageTot.getDefaultCell().setPadding(0f); pageTot.getDefaultCell().setBorder(Rectangle.NO_BORDER); pageTot.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT); pageTot.setWidthPercentage(100f); // Data for the barcode : it is composed of 3 blocks whith AI 402, 90 // and 421 // The blocks whith the type 402 and 90 are of variable size so you must // put a FNC1 // to delimitate the block String code402 = "24132399420058289" + Barcode128.FNC1; String code90 = "3700000050" + Barcode128.FNC1; String code421 = "422356"; String data = code402 + code90 + code421; PdfPTable cell = new PdfPTable(1); cell.getDefaultCell().setBorder(Rectangle.NO_BORDER); cell.getDefaultCell().setPadding(0f); PdfPCell info = new PdfPCell(new Phrase("Barcode EAN 128")); info.setBorder(Rectangle.NO_BORDER); pageTot.addCell(info); Barcode128 shipBarCode = new Barcode128(); shipBarCode.setX(0.75f); shipBarCode.setN(1.5f); shipBarCode.setChecksumText(true); shipBarCode.setGenerateChecksum(true); shipBarCode.setSize(10f); shipBarCode.setTextAlignment(Element.ALIGN_CENTER); shipBarCode.setBaseline(10f); shipBarCode.setCode(data); shipBarCode.setBarHeight(50f); Image imgShipBarCode = shipBarCode.createImageWithBarcode(cb, Color.black, Color.blue); PdfPCell shipment = new PdfPCell(new Phrase(new Chunk(imgShipBarCode, 0, 0))); shipment.setFixedHeight(shipBarCode.getBarcodeSize().getHeight() + 16f); shipment.setPaddingTop(5f); shipment.setPaddingBottom(10f); shipment.setBorder(Rectangle.BOX); shipment.setVerticalAlignment(Element.ALIGN_TOP); shipment.setHorizontalAlignment(Element.ALIGN_CENTER); cell.addCell(shipment); pageTot.addCell(cell); document.add(pageTot); // step 5 document.close(); }
/** * Example Barcode EAN128. * * @param args * no arguments needed */ public static void main(String[] args) { // step 1 Document document = new Document(); try { // step 2 PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(android.os.Environment.getExternalStorageDirectory() + java.io.File.separator + "droidtext" + java.io.File.separator + "ean128.pdf")); // step 3 document.open(); // step 4 PdfContentByte cb = writer.getDirectContent(); PdfPTable pageTot = new PdfPTable(1); pageTot.getDefaultCell().setPadding(0f); pageTot.getDefaultCell().setBorder(Rectangle.NO_BORDER); pageTot.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT); pageTot.setWidthPercentage(100f); // Data for the barcode : it is composed of 3 blocks whith AI 402, // 90 and 421 // The blocks whith the type 402 and 90 are of variable size so you // must put a FNC1 // to delimitate the block String code402 = "24132399420058289" + Barcode128.FNC1; String code90 = "3700000050" + Barcode128.FNC1; String code421 = "422356"; String data = code402 + code90 + code421; PdfPTable cell = new PdfPTable(1); cell.getDefaultCell().setBorder(Rectangle.NO_BORDER); cell.getDefaultCell().setPadding(0f); PdfPCell info = new PdfPCell(new Phrase("Barcode EAN 128")); info.setBorder(Rectangle.NO_BORDER); pageTot.addCell(info); Barcode128 shipBarCode = new Barcode128(); shipBarCode.setX(0.75f); shipBarCode.setN(1.5f); shipBarCode.setChecksumText(true); shipBarCode.setGenerateChecksum(true); shipBarCode.setSize(10f); shipBarCode.setTextAlignment(Element.ALIGN_CENTER); shipBarCode.setBaseline(10f); shipBarCode.setCode(data); shipBarCode.setBarHeight(50f); Image imgShipBarCode = shipBarCode.createImageWithBarcode(cb, Color.black, Color.blue); PdfPCell shipment = new PdfPCell(new Phrase(new Chunk(imgShipBarCode, 0, 0))); shipment.setFixedHeight(shipBarCode.getBarcodeSize().getHeight() + 16f); shipment.setPaddingTop(5f); shipment.setPaddingBottom(10f); shipment.setBorder(Rectangle.BOX); shipment.setVerticalAlignment(Element.ALIGN_TOP); shipment.setHorizontalAlignment(Element.ALIGN_CENTER); cell.addCell(shipment); pageTot.addCell(cell); document.add(pageTot); } catch (Exception e) { e.printStackTrace(); } // step 5 document.close(); }