BarcodeMSI extends Barcode1D { @Override public Rectangle placeBarcode(PdfCanvas canvas, Color barColor, Color textColor) { String fullCode = this.code; if (this.checksumText) { fullCode = fullCode + Integer.toString(getChecksum(this.code)); } float fontX = 0.0f; if (this.font != null) { String var10001 = this.altText != null ? this.altText : fullCode; fullCode = this.altText != null ? this.altText : fullCode; fontX = this.font.getWidth(var10001, this.size); } String bCode = this.code; if (this.generateChecksum) { bCode += getChecksum(bCode); } int idx; idx = bCode.length(); final float fullWidth = (idx * BARS_PER_CHARACTER + BARS_FOR_START + BARS_FOR_STOP) * this.x; float barStartX = 0.0f; float textStartX = 0.0f; switch (this.textAlignment) { case 1: break; case 2: if (fontX > fullWidth) { barStartX = fontX - fullWidth; } else { textStartX = fullWidth - fontX; } break; default: if (fontX > fullWidth) { barStartX = (fontX - fullWidth) / 2.0f; } else { textStartX = (fullWidth - fontX) / 2.0f; } } float barStartY = 0.0f; float textStartY = 0.0f; if (this.font != null) { if (this.baseline <= 0.0f) { textStartY = this.barHeight - this.baseline; } else { textStartY = -this.getDescender(); barStartY = textStartY + this.baseline; } } byte[] bars = getBarsMSI(bCode); if (barColor != null) { canvas.setFillColor(barColor); } for (int k = 0; k < bars.length; ++k) { float w = (float) bars[k] * this.x; if (bars[k] == 1) canvas.rectangle((double) barStartX, (double) barStartY, (double) (w - this.inkSpreading), (double) this.barHeight); barStartX += this.x; } canvas.fill(); if (this.font != null) { if (textColor != null) { canvas.setFillColor(textColor); } canvas.beginText(); canvas.setFontAndSize(this.font, this.size); canvas.setTextMatrix(textStartX, textStartY); canvas.showText(fullCode); canvas.endText(); } return this.getBarcodeSize(); } BarcodeMSI(PdfDocument document);  BarcodeMSI(PdfDocument document, PdfFont font); @Override Rectangle getBarcodeSize(); @Override Rectangle placeBarcode(PdfCanvas canvas, Color barColor, Color textColor); @Override Image createAwtImage(java.awt.Color foreground, java.awt.Color background); static byte[] getBarsMSI(String text); static int getChecksum(String text);  }
@Test public void barcode01Test() throws IOException, PdfException, InterruptedException { String filename = "barcodeMSI_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 BarcodeMSI(document); barcode.setCode("123456789"); barcode.setGenerateChecksum(true); barcode.setTextAlignment(Barcode1D.ALIGN_LEFT); barcode.placeBarcode(canvas, ColorConstants.BLACK, ColorConstants.WHITE); document.close(); Assert.assertNull(new CompareTool().compareByContent(destinationFolder + filename, sourceFolder + "cmp_" + filename, destinationFolder, "diff01_")); }
@Test public void barcode02Test() throws IOException, InterruptedException { String filename = "barcodeMSI_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 BarcodeMSI(document); barcode.setCode("9781935182610"); barcode.setTextAlignment(Barcode1D.ALIGN_LEFT); barcode.placeBarcode(canvas, ColorConstants.BLACK, ColorConstants.WHITE); document.close(); Assert.assertNull(new CompareTool().compareByContent(destinationFolder + filename, sourceFolder + "cmp_" + filename, destinationFolder, "diff02_")); }
@Test public void barcodeAlignRightTest() throws IOException, InterruptedException { final String filename = "barcodeMSI_AlignRight.pdf"; final PdfWriter writer = new PdfWriter(destinationFolder + filename); final PdfDocument document = new PdfDocument(writer); final PdfPage page = document.addNewPage(); final PdfCanvas canvas = new PdfCanvas(page); final Barcode1D barcode = new BarcodeMSI(document); barcode.setCode("123456789"); barcode.setGenerateChecksum(true); barcode.setTextAlignment(Barcode1D.ALIGN_RIGHT); barcode.placeBarcode(canvas, ColorConstants.BLACK, ColorConstants.RED); document.close(); Assert.assertNull(new CompareTool().compareByContent(destinationFolder + filename, sourceFolder + "cmp_" + filename, destinationFolder, "diff01_")); }
@Test public void barcodeAlignCenterTest() throws IOException, InterruptedException { final String filename = "barcodeMSI_AlignCenter.pdf"; final PdfWriter writer = new PdfWriter(destinationFolder + filename); final PdfDocument document = new PdfDocument(writer); final PdfPage page = document.addNewPage(); final PdfCanvas canvas = new PdfCanvas(page); final Barcode1D barcode = new BarcodeMSI(document); barcode.setCode("123456789"); barcode.setGenerateChecksum(true); barcode.setTextAlignment(Barcode1D.ALIGN_CENTER); barcode.placeBarcode(canvas, ColorConstants.BLACK, ColorConstants.RED); document.close(); Assert.assertNull(new CompareTool().compareByContent(destinationFolder + filename, sourceFolder + "cmp_" + filename, destinationFolder, "diff01_")); }