Generate barcode and QR code using itext

Source: Internet
Author: User

The code for generating barcode mainly comes from the sample code provided on the itext official website. However, when using the Chinese tag, the Chinese part is blank.

When using Chinese in itext, you first need to download the itext-asian.jar file and add it to the project class path.

For more information, see the code.

/***** @ Author Chen Yi <njchenyi@gmail.com> */import Java. io. fileoutputstream; import Java. io. ioexception; import COM. itextpdf. text. basecolor; import COM. itextpdf. text. document; import COM. itextpdf. text. extends entexception; import COM. itextpdf. text. element; import COM. itextpdf. text. font; import COM. itextpdf. text. image; import COM. itextpdf. text. paragraph; import COM. itextpdf. text. rectangle; import com.itext;.text. barcode; import com.itext;.text=. barcode128; import com.itext;.text=. barcode39; import com.itext;.text. barcodecodabar; import com.itext;.text=. barcodedatamatrix; import com.itext;.text=. barcodeean; import com.itext;.text. barcodeeansupp; import com.itext;.text=. barcodeinter25; import com.itext;.text=. barcode424417; import com.itext;.text=. barcodepostnet; import com.itext;.text=. barcodeqrcode; import com.itext;.text=. basefont; import com.itext;.text=. using contentbyte; import com.itext;.text=. extends writer; public class barcodes {/** the resulting PDF. */Private Static final string result = "barcodes.pdf";/*** generates a PDF file with different types of barcodes. ** @ Param ARGs * No arguments needed here * @ throws writable entexception * @ throws ioexception */public static void main (string [] ARGs) throws ioexception, writable entexception {New barcodes (). createpdf (result);}/*** creates a PDF document. * @ Param filename the path to the new pdf document * @ throws define entexception * @ throws ioexception */Public void createpdf (string filename) throws ioexception, incluentexception {basefont BF = NULL; font fontchinese = NULL; try {// use itextasian. jar font BF = basefont. createfont ("stsong-light", "UniGB-UCS2-H", basefont. not_embedded); // use the Windows system font (TrueType) // basefont. createfont ("C:/Windows/fonts/simyou. TTF ", basefont. identity_h, basefont. not_embedded); // use the resource font (classpath) // basefont. createfont ("/simyou. TTF ", basefont. identity_h, basefont. not_embedded); fontchinese = new font (Bf, 12, Font. normal);} catch (effecentexception e) {// todo auto-generated Catch Block E. printstacktrace ();} catch (ioexception e) {// todo auto-generated Catch Block E. printstacktrace ();} // Step 1 Document document = new document (New rectangle (340,842); // Step 2 preview writer = preview writer. getinstance (document, new fileoutputstream (filename); // step 3 document. open (); // Step 4 Export contentbyte cb = writer. getdirectcontent (); document. add (new paragraph ("Chen Yu njchenyi@gmail.com", fontchinese); // EAN 13 document. add (new paragraph ("barcode EAN.UCC-13"); barcodeean codeean = new barcodeean (); codeean. setcode ("4512345678906"); document. add (new paragraph ("Default:"); document. add (codeean. createimagewithbarcode (CB, null, null); codeean. setguardbars (false); document. add (new paragraph ("without guard bars"); document. add (codeean. createimagewithbarcode (CB, null, null); codeean. setbaseline (-1f); codeean. setguardbars (true); document. add (new paragraph ("text above:"); document. add (codeean. createimagewithbarcode (CB, null, null); codeean. setbaseline (codeean. getsize (); // UPC a document. add (new paragraph ("barcode UCC-12 (UPC-A)"); codeean. setcodetype (barcode. upca); codeean. setcode ("785342304749"); document. add (codeean. createimagewithbarcode (CB, null, null); // EAN 8 document. add (new paragraph ("barcode EAN.UCC-8"); codeean. setcodetype (barcode. ean8); codeean. setbarheight (codeean. getsize () * 1.5f); codeean. setcode ("34569870"); document. add (codeean. createimagewithbarcode (CB, null, null); // upc e document. add (new paragraph ("barcode UPC-E"); codeean. setcodetype (barcode. upce); codeean. setcode ("03456781"); document. add (codeean. createimagewithbarcode (CB, null, null); codeean. setbarheight (codeean. getsize () * 3f); // eansupp document. add (new paragraph ("bookland"); document. add (new paragraph ("ISBN 0-321-30474-8"); codeean. setcodetype (barcode. ean13); codeean. setcode ("9781935182610"); barcodeean codesupp = new barcodeean (); codesupp. setcodetype (barcode. supp5); codesupp. setcode ("55999"); codesupp. setbaseline (-2); barcodeeansupp eansupp = new barcodeeansupp (codeean, codesupp); document. add (eansupp. createimagewithbarcode (CB, null, basecolor. blue); // code 128 document. add (new paragraph ("barcode 128"); barcode128 code128 = new barcode128 (); code128.setcode ("0123456789 hello"); document. add (code128.createimagewithbarcode (CB, null, null); code128.setcode ("0123456789 \ uffffmy raw barcode (0-9)"); code128.setcodetype (barcode. code128_raw); document. add (code128.createimagewithbarcode (CB, null, null); // data for the barcode: String code402 = "24132399420058289"; string code90 = "3700000050"; string code421 = "422356 "; stringbuffer DATA = new stringbuffer (code402); data. append (barcode128.fnc1); data. append (code90); data. append (barcode128.fnc1); data. append (code421); barcode128 shipbarcode = new barcode128 (); shipbarcode. setx (0.75f); shipbarcode. setn (1.5f); shipbarcode. setsize (10f); shipbarcode. settextalignment (element. align_center); shipbarcode. setbaseline (10f); shipbarcode. setbarheight (50f); shipbarcode. setcode (data. tostring (); document. add (shipbarcode. createimagewithbarcode (CB, basecolor. black, basecolor. blue); // It is composed of 3 blocks whith AI 01,310 1 and 10 barcode128 uccean128 = new barcode128 (); uccean128.setcodetype (barcode. code128_ucc); uccean128.setcode ("(01) 00000090311314 (10) ABC123 (15) 060916"); document. add (uccean128.createimagewithbarcode (CB, basecolor. blue, basecolor. black); uccean128.setcode ("0191234567890121310100035510abc123"); document. add (uccean128.createimagewithbarcode (CB, basecolor. blue, basecolor. red); uccean128.setcode ("(01) 28880123456788"); document. add (uccean128.createimagewithbarcode (CB, basecolor. blue, basecolor. black); // inter25 document. add (new paragraph ("barcode interleaved 2 of 5"); barcodeinter25 code25 = new barcodeinter25 (); code25.setgeneratechecksum (true); code25.setcode ("41-1200076041-001 "); document. add (code25.createimagewithbarcode (CB, null, null); code25.setcode ("411200076041001"); document. add (code25.createimagewithbarcode (CB, null, null); code25.setcode ("0611012345678"); code25.setchecksumtext (true); document. add (code25.createimagewithbarcode (CB, null, null); // PostNet document. add (new paragraph ("barcode PostNet"); barcodepostnet codepost = new barcodepostnet (); document. add (new paragraph ("Zip"); codepost. setcode ("01234"); document. add (codepost. createimagewithbarcode (CB, null, null); document. add (new paragraph ("Zip + 4"); codepost. setcode ("012345678"); document. add (codepost. createimagewithbarcode (CB, null, null); document. add (new paragraph ("Zip + 4 and DP"); codepost. setcode ("01234567890"); document. add (codepost. createimagewithbarcode (CB, null, null); document. add (new paragraph ("barcode planet"); barcodepostnet codeplanet = new barcodepostnet (); codeplanet. setcode ("01234567890"); codeplanet. setcodetype (barcode. planet); document. add (codeplanet. createimagewithbarcode (CB, null, null); // code 39 Document. add (new paragraph ("barcode 3 of 9"); barcode39 code39 = new barcode39 (); code39.setcode ("itext in action"); document. add (code39.createimagewithbarcode (CB, null, null); document. add (new paragraph ("barcode 3 of 9 extended"); barcode39 code39ext = new barcode39 (); code39ext. setcode ("itext in action"); code39ext. setstartstoptext (false); code39ext. setextended (true); document. add (code39ext. createimagewithbarcode (CB, null, null); // Codabar document. add (new paragraph ("Codabar"); barcodecodabar Codabar = new barcodecodabar (); Codabar. setcode ("a123a"); Codabar. setstartstoptext (true); document. add (Codabar. createimagewithbarcode (CB, null, null); // document. add (new paragraph ("barcode %417"); barcode%417 %417 = new barcode%417 (); string text = "Call me Ishmael. some years ago -- never mind how long "+" Precisely -- having little or no money in my purse, and nothing "+" particle to interest me on shore, I thought I wowould sail about "+" a little and see the watery part of the world. "; paiyun.settext (text); image IMG = paiyun.getimage (); IMG. scalepercent (50, 50 * Rows 2017.getyheight (); document. add (IMG); document. add (new paragraph ("barcode datamatrix"); barcodedatamatrix datamatrix = new barcodedatamatrix (); datamatrix. generate (text); IMG = datamatrix. createimage (); document. add (IMG); document. add (new paragraph ("barcode qrcode"); barcodeqrcode qrcode = new barcodeqrcode ("Moby Dick by Herman Melville", 1, 1, null); IMG = qrcode. getimage (); document. add (IMG); // Step 5 document. close ();}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.