BarcodePostnet extends Barcode1D { @Override public Rectangle placeBarcode(PdfCanvas canvas, Color barColor, Color textColor) { if (barColor != null) canvas.setFillColor(barColor); byte[] bars = getBarsPostnet(code); byte flip = 1; if (codeType == TYPE_PLANET) { flip = 0; bars[0] = 0; bars[bars.length - 1] = 0; } float startX = 0; for (int k = 0; k < bars.length; ++k) { canvas.rectangle(startX, 0, x - inkSpreading, bars[k] == flip ? barHeight : size); startX += n; } canvas.fill(); return getBarcodeSize(); } BarcodePostnet(PdfDocument document); static byte[] getBarsPostnet(String text); @Override Rectangle getBarcodeSize(); @Override void fitWidth(float width); @Override Rectangle placeBarcode(PdfCanvas canvas, Color barColor, Color textColor); @Override Image createAwtImage(java.awt.Color foreground, java.awt.Color background); static int TYPE_POSTNET; static int TYPE_PLANET; }
@Test public void barcode01Test() throws IOException, PdfException, InterruptedException { String filename = "barcodePostnet.pdf"; PdfWriter writer = new PdfWriter(destinationFolder + filename); PdfDocument document = new PdfDocument(writer); PdfPage page = document.addNewPage(); PdfCanvas canvas = new PdfCanvas(page); BarcodePostnet codePlanet = new BarcodePostnet(document); codePlanet.setCode("01234567890"); codePlanet.setCodeType(BarcodePostnet.TYPE_PLANET); codePlanet.placeBarcode(canvas, null, null); document.close(); Assert.assertNull(new CompareTool().compareByContent(destinationFolder + filename, sourceFolder + "cmp_" + filename, destinationFolder, "diff_")); }