BarcodeEAN extends Barcode1D { @Override public Rectangle placeBarcode(PdfCanvas canvas, Color barColor, Color textColor) { Rectangle rect = getBarcodeSize(); float barStartX = 0; float barStartY = 0; float textStartY = 0; if (font != null) { if (baseline <= 0) { textStartY = barHeight - baseline; } else { textStartY = -getDescender(); barStartY = textStartY + baseline; } } switch (codeType) { case EAN13: case UPCA: case UPCE: if (font != null) { barStartX += font.getWidth(code.charAt(0), size); } break; } byte[] bars; int[] guard = GUARD_EMPTY; switch (codeType) { case EAN13: bars = getBarsEAN13(code); guard = GUARD_EAN13; break; case EAN8: bars = getBarsEAN8(code); guard = GUARD_EAN8; break; case UPCA: bars = getBarsEAN13("0" + code); guard = GUARD_UPCA; break; case UPCE: bars = getBarsUPCE(code); guard = GUARD_UPCE; break; case SUPP2: bars = getBarsSupplemental2(code); break; case SUPP5: bars = getBarsSupplemental5(code); break; default: throw new PdfException("Invalid code type"); } float keepBarX = barStartX; boolean print = true; float gd = 0; if (font != null && baseline > 0 && guardBars) { gd = baseline / 2; } if (barColor != null) { canvas.setFillColor(barColor); } for (int k = 0; k < bars.length; ++k) { float w = bars[k] * x; if (print) { if (Arrays.binarySearch(guard, k) >= 0) { canvas.rectangle(barStartX, barStartY - gd, w - inkSpreading, barHeight + gd); } else { canvas.rectangle(barStartX, barStartY, w - inkSpreading, barHeight); } } print = !print; barStartX += w; } canvas.fill(); if (font != null) { if (textColor != null) { canvas.setFillColor(textColor); } canvas.beginText(); canvas.setFontAndSize(font, size); switch (codeType) { case EAN13: canvas.setTextMatrix(0, textStartY); canvas.showText(code.substring(0, 1)); for (int k = 1; k < 13; ++k) { String c = code.substring(k, k + 1); float len = font.getWidth(c, size); float pX = keepBarX + TEXTPOS_EAN13[k - 1] * x - len / 2; canvas.setTextMatrix(pX, textStartY); canvas.showText(c); } break; case EAN8: for (int k = 0; k < 8; ++k) { String c = code.substring(k, k + 1); float len = font.getWidth(c, size); float pX = TEXTPOS_EAN8[k] * x - len / 2; canvas.setTextMatrix(pX, textStartY); canvas.showText(c); } break; case UPCA: canvas.setTextMatrix(0, textStartY); canvas.showText(code.substring(0, 1)); for (int k = 1; k < 11; ++k) { String c = code.substring(k, k + 1); float len = font.getWidth(c, size); float pX = keepBarX + TEXTPOS_EAN13[k] * x - len / 2; canvas.setTextMatrix(pX, textStartY); canvas.showText(c); } canvas.setTextMatrix(keepBarX + x * (11 + 12 * 7), textStartY); canvas.showText(code.substring(11, 12)); break; case UPCE: canvas.setTextMatrix(0, textStartY); canvas.showText(code.substring(0, 1)); for (int k = 1; k < 7; ++k) { String c = code.substring(k, k + 1); float len = font.getWidth(c, size); float pX = keepBarX + TEXTPOS_EAN13[k - 1] * x - len / 2; canvas.setTextMatrix(pX, textStartY); canvas.showText(c); } canvas.setTextMatrix(keepBarX + x * (9 + 6 * 7), textStartY); canvas.showText(code.substring(7, 8)); break; case SUPP2: case SUPP5: for (int k = 0; k < code.length(); ++k) { String c = code.substring(k, k + 1); float len = font.getWidth(c, size); float pX = (7.5f + (9 * k)) * x - len / 2; canvas.setTextMatrix(pX, textStartY); canvas.showText(c); } break; } canvas.endText(); } return rect; } BarcodeEAN(PdfDocument document);  BarcodeEAN(PdfDocument document, PdfFont font); static int calculateEANParity(String code); static String convertUPCAtoUPCE(String text); static byte[] getBarsEAN13(String _code); static byte[] getBarsEAN8(String _code); static byte[] getBarsUPCE(String _code); static byte[] getBarsSupplemental2(String _code); static byte[] getBarsSupplemental5(String _code); @Override Rectangle getBarcodeSize(); @Override Rectangle placeBarcode(PdfCanvas canvas, Color barColor, Color textColor); @Override Image createAwtImage(java.awt.Color foreground, java.awt.Color background); static final int EAN13; static final int EAN8; static final int UPCA; static final int UPCE; static final int SUPP2; static final int SUPP5; }
@Test public void barcode01Test() throws IOException, PdfException, InterruptedException { String filename = "barcodeEAN_01.pdf"; PdfWriter writer = new PdfWriter(destinationFolder + filename); PdfDocument document = new PdfDocument(writer); PdfPage page = document.addNewPage(); PdfCanvas canvas = new PdfCanvas(page); Barcode1D barcode = new BarcodeEAN(document); barcode.setCodeType(BarcodeEAN.EAN13); barcode.setCode("9781935182610"); barcode.setTextAlignment(Barcode1D.ALIGN_LEFT); barcode.placeBarcode(canvas, ColorConstants.BLACK, ColorConstants.BLACK); document.close(); Assert.assertNull(new CompareTool() .compareByContent(destinationFolder + filename, sourceFolder + "cmp_" + filename, destinationFolder, "diff_")); }
@Test public void barcode02Test() throws IOException, PdfException, InterruptedException { String filename = "barcodeEAN_02.pdf"; PdfWriter writer = new PdfWriter(destinationFolder + filename); PdfReader reader = new PdfReader(sourceFolder + "DocumentWithTrueTypeFont1.pdf"); PdfDocument document = new PdfDocument(reader, writer); PdfCanvas canvas = new PdfCanvas(document.getLastPage()); Barcode1D barcode = new BarcodeEAN(document); barcode.setCodeType(BarcodeEAN.EAN8); barcode.setCode("97819351"); barcode.setTextAlignment(Barcode1D.ALIGN_LEFT); barcode.placeBarcode(canvas, ColorConstants.BLACK, ColorConstants.BLACK); document.close(); Assert.assertNull(new CompareTool() .compareByContent(destinationFolder + filename, sourceFolder + "cmp_" + filename, destinationFolder, "diff_")); }
@Test public void barcode03Test() throws IOException, PdfException, InterruptedException { String filename = "barcodeEANSUP.pdf"; PdfWriter writer = new PdfWriter(destinationFolder + filename); PdfDocument document = new PdfDocument(writer); PdfPage page = document.addNewPage(); PdfCanvas canvas = new PdfCanvas(page); BarcodeEAN codeEAN = new BarcodeEAN(document); codeEAN.setCodeType(BarcodeEAN.EAN13); codeEAN.setCode("9781935182610"); BarcodeEAN codeSUPP = new BarcodeEAN(document); codeSUPP.setCodeType(BarcodeEAN.SUPP5); codeSUPP.setCode("55999"); codeSUPP.setBaseline(-2); BarcodeEANSUPP eanSupp = new BarcodeEANSUPP(codeEAN, codeSUPP); eanSupp.placeBarcode(canvas, null, ColorConstants.BLUE); document.close(); Assert.assertNull(new CompareTool() .compareByContent(destinationFolder + filename, sourceFolder + "cmp_" + filename, destinationFolder, "diff_")); }
@Test public void placeBarcodeUPCATest() throws IOException, PdfException, InterruptedException { String filename = "placeBarcodeUPCATest.pdf"; PdfWriter writer = new PdfWriter(destinationFolder + filename); PdfDocument document = new PdfDocument(writer); PdfPage page = document.addNewPage(); PdfCanvas canvas = new PdfCanvas(page); Barcode1D barcode = new BarcodeEAN(document); barcode.setCodeType(BarcodeEAN.UPCA); barcode.setCode("012340000006"); barcode.placeBarcode(canvas, ColorConstants.BLACK, ColorConstants.BLACK); document.close(); Assert.assertNull(new CompareTool() .compareByContent(destinationFolder + filename, sourceFolder + "cmp_" + filename, destinationFolder, "diff_")); }
@Test public void placeBarcodeUPCETest() throws IOException, PdfException, InterruptedException { String filename = "placeBarcodeUPCETest.pdf"; PdfWriter writer = new PdfWriter(destinationFolder + filename); PdfDocument document = new PdfDocument(writer); PdfPage page = document.addNewPage(); PdfCanvas canvas = new PdfCanvas(page); Barcode1D barcode = new BarcodeEAN(document); barcode.setCodeType(BarcodeEAN.UPCE); barcode.setCode("03456781"); barcode.placeBarcode(canvas, ColorConstants.BLACK, ColorConstants.BLACK); document.close(); Assert.assertNull(new CompareTool() .compareByContent(destinationFolder + filename, sourceFolder + "cmp_" + filename, destinationFolder, "diff_")); }
@Test public void placeBarcodeSUPP2Test() throws IOException, PdfException, InterruptedException { String filename = "placeBarcodeSUPP2Test.pdf"; PdfWriter writer = new PdfWriter(destinationFolder + filename); PdfDocument document = new PdfDocument(writer); PdfPage page = document.addNewPage(); PdfCanvas canvas = new PdfCanvas(page); Barcode1D barcode = new BarcodeEAN(document); barcode.setCodeType(BarcodeEAN.SUPP2); barcode.setCode("03456781"); barcode.placeBarcode(canvas, ColorConstants.BLACK, ColorConstants.BLACK); document.close(); Assert.assertNull(new CompareTool() .compareByContent(destinationFolder + filename, sourceFolder + "cmp_" + filename, destinationFolder, "diff_")); }
@Test public void placeBarcodeSUPP5Test() throws IOException, PdfException, InterruptedException { String filename = "placeBarcodeSUPP5Test.pdf"; PdfWriter writer = new PdfWriter(destinationFolder + filename); PdfDocument document = new PdfDocument(writer); PdfPage page = document.addNewPage(); PdfCanvas canvas = new PdfCanvas(page); Barcode1D barcode = new BarcodeEAN(document); barcode.setCodeType(BarcodeEAN.SUPP5); barcode.setCode("55999"); barcode.placeBarcode(canvas, ColorConstants.BLACK, ColorConstants.BLACK); document.close(); Assert.assertNull(new CompareTool() .compareByContent(destinationFolder + filename, sourceFolder + "cmp_" + filename, destinationFolder, "diff_")); }