itext - 2D barcode generation issue in Java -
i generating barcodes using itext api, looks when linear barcodes when 2d barcodes barcodes placed pdf document images, hence reducing quality of barcode on low resolution printers , unable scan barcode. below code
barcodepdf417 pdf417 = new barcodepdf417(); string text = "barcodepdf417 barcode"; pdf417.settext(text); image img = pdf417.getimage(); document.add(img); now alternative draw barcode , found palcebarcode method might favor requirement.
i have seen below code in barcodepdf417 class in itext source , not able find out way how use it
public void placebarcode(pdfcontentbyte cb, basecolor foreground, float moduleheight, float modulewidth) { paintcode(); int stride = (bitcolumns + 7) / 8; cb.setcolorfill(foreground); (int k = 0; k < coderows; ++k) { int p = k * stride; (int j = 0; j < bitcolumns; ++j) { int b = outbits[p + j / 8] & 0xff; b <<= j % 8; if ((b & 0x80) != 0) { cb.rectangle(j * modulewidth, (coderows - k - 1) * moduleheight, modulewidth, moduleheight); } } } cb.fill(); } can suggest way use above method?
i have written code below getting dark page whole.
rectangle pagesize = new rectangle(w * 72, h * 72); document doc = new document(pagesize, 1f, 1f, 1f, 1f); pdfwriter writer = pdfwriter.getinstance(doc, getoutputstream()); doc.open(); pdfcontentbyte cb = writer.getdirectcontent(); barcodepdf417 pf = new barcodepdf417(); pf.settext("barcodepdf417 barcode"); pf.getimage(); rectangle rc = pf.getbarcodesize(); pf.placebarcode(cb, basecolor.black, rc.getheight(), rc.getwidth()); doc.close();
please take @ barcodeplacement example. in example, create 3 pdf417 barcodes:
pdfcontentbyte cb = writer.getdirectcontent(); image img = createbarcode(cb, "this 2d barcode", 1, 1); document.add(new paragraph( string.format("this barcode measures %s %s user units", img.getscaledwidth(), img.getscaledheight()))); document.add(img); img = createbarcode(cb, "this not raster image", 3, 3); document.add(new paragraph( string.format("this barcode measures %s %s user units", img.getscaledwidth(), img.getscaledheight()))); document.add(img); img = createbarcode(cb, "this vector data drawn on pdf page", 1, 3); document.add(new paragraph( string.format("this barcode measures %s %s user units", img.getscaledwidth(), img.getscaledheight()))); the result looks on outside:

one particular barcode looks on inside:

i'm adding inside view show 2d barcode not added raster image (as case initial approach you've tried). vector image consisting of series of small rectangles. can check taking @ barcode_placement.pdf file.
please don't confused because use image object. if @ createbarcode() method, can see image is, in fact, vector image:
public image createbarcode(pdfcontentbyte cb, string text, float mh, float mw) throws badelementexception { barcodepdf417 pf = new barcodepdf417(); pf.settext("barcodepdf417 barcode"); rectangle size = pf.getbarcodesize(); pdftemplate template = cb.createtemplate( mw * size.getwidth(), mh * size.getheight()); pf.placebarcode(template, basecolor.black, mh, mw); return image.getinstance(template); } the height , width passed placebarcode() method, define height , width of small rectangles drawn. if @ inside view, can see instance:
0 21 3 1 re this rectangle x = 0, y = 21, width 3 , height 1.
when ask barcode size, number of rectangles drawn. hence dimensions of barcode is:
rectangle size = pf.getbarcodesize(); float width = mw * size.getwidth(); float height = mh * size.getheight(); your assumption size size in user units correct if mw , mh equal 1.
i use these values create pdftemplate instance , draw barcode form xobject. of times, it's easier work image class working pdftemplate, wrap pdftemplate inside image.
i can add image document other image. main difference ordinary images, image vector image.
Comments
Post a Comment