Barcode39 extends Barcode1D { @Override public Rectangle placeBarcode(PdfCanvas canvas, Color barColor, Color textColor) { String fullCode = code; float fontX = 0; String bCode = code; if (extended) { bCode = getCode39Ex(code); } if (font != null) { if (generateChecksum && checksumText) { fullCode += getChecksum(bCode); } if (startStopText) { fullCode = "*" + fullCode + "*"; } fontX = font.getWidth(fullCode = altText != null ? altText : fullCode, size); } if (generateChecksum) { bCode += getChecksum(bCode); } int len = bCode.length() + 2; float fullWidth = len * (6 * x + 3 * x * n) + (len - 1) * x; float barStartX = 0; float textStartX = 0; switch (textAlignment) { case ALIGN_LEFT: break; case ALIGN_RIGHT: if (fontX > fullWidth) { barStartX = fontX - fullWidth; } else { textStartX = fullWidth - fontX; } break; default: if (fontX > fullWidth) { barStartX = (fontX - fullWidth) / 2; } else { textStartX = (fullWidth - fontX) / 2; } break; } float barStartY = 0; float textStartY = 0; if (font != null) { if (baseline <= 0) { textStartY = barHeight - baseline; } else { textStartY = -getDescender(); barStartY = textStartY + baseline; } } byte[] bars = getBarsCode39(bCode); boolean print = true; if (barColor != null) { canvas.setFillColor(barColor); } for (int k = 0; k < bars.length; ++k) { float w = (bars[k] == 0 ? x : x * n); if (print) { canvas.rectangle(barStartX, barStartY, w - inkSpreading, barHeight); } print = !print; barStartX += w; } canvas.fill(); if (font != null) { if (textColor != null) { canvas.setFillColor(textColor); } canvas. beginText(). setFontAndSize(font, size). setTextMatrix(textStartX, textStartY). showText(fullCode). endText(); } return getBarcodeSize(); } Barcode39(PdfDocument document);  Barcode39(PdfDocument document, PdfFont font); static byte[] getBarsCode39(String text); static String getCode39Ex(String text); @Override Rectangle getBarcodeSize(); @Override Rectangle placeBarcode(PdfCanvas canvas, Color barColor, Color textColor); @Override Image createAwtImage(java.awt.Color foreground, java.awt.Color background);  }
@Test public void barcode01Test() throws IOException, PdfException, InterruptedException { String filename = "barcode39_01.pdf"; PdfDocument document = new PdfDocument(new PdfWriter(destinationFolder + filename)); PdfPage page = document.addNewPage(); PdfCanvas canvas = new PdfCanvas(page); Barcode1D barcode = new Barcode39(document); 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 = "barcode39_02.pdf"; PdfDocument document = new PdfDocument(new PdfReader(sourceFolder + "DocumentWithTrueTypeFont1.pdf"), new PdfWriter(destinationFolder + filename)); PdfCanvas canvas = new PdfCanvas(document.getLastPage()); Barcode1D barcode = new Barcode39(document); 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_")); }