1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | import java.awt.image.BufferedImage; import java.util.HashMap; import java.util.Map; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDPage; import org.apache.pdfbox.pdmodel.PDPageContentStream; import org.apache.pdfbox.pdmodel.common.PDRectangle; import org.apache.pdfbox.pdmodel.graphics.image.JPEGFactory; import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject; import org.krysalis.barcode4j.impl.code128.Code128Bean; import org.krysalis.barcode4j.impl.code39.Code39Bean; import org.krysalis.barcode4j.impl.pdf417.PDF417Bean; import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.client.j2se.MatrixToImageConfig; import com.google.zxing.client.j2se.MatrixToImageWriter; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; public class PDFBarcode { public static void main(String[] args) { try { // Creating PDF document object String myPDF = "docs/barcode_doc.pdf" ; PDDocument document = new PDDocument(); PDPage page = new PDPage(PDRectangle.LETTER); document.addPage(page); float margin = 25 ; float y = 700 ; // add CODE 128 Barcode addBarcode128(document, page, "123456789" , margin, y); // add CODE 39 Barcode y = y - 100 ; addBarcode39(document, page, "123456789" , margin, y); // 2D Barcode format PDF417 y = 700 ; addBarcodePdf417(document, page, "123456789" , margin + 300 , y); // add QRCode y = y - 100 ; // Saving the document document.save(myPDF); // Closing the document document.close(); } catch (Exception e) { e.printStackTrace(); } } private static void addBarcode128(PDDocument document, PDPage page, String text, float x, float y) { try { PDPageContentStream contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true ); int dpi = 300 ; BitmapCanvasProvider canvas = new BitmapCanvasProvider(dpi, BufferedImage.TYPE_BYTE_BINARY, false , 0 ); Code128Bean code128Bean = new Code128Bean(); code128Bean.generateBarcode(canvas, text.trim()); canvas.finish(); BufferedImage bImage = canvas.getBufferedImage(); PDImageXObject image = JPEGFactory.createFromImage(document, bImage); contentStream.drawImage(image, x, y, 100 , 50 ); contentStream.close(); } catch (Exception e) { e.printStackTrace(); } } private static void addBarcode39(PDDocument document, PDPage page, String text, float x, float y) { try { PDPageContentStream contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true ); int dpi = 300 ; BitmapCanvasProvider canvas = new BitmapCanvasProvider(dpi, BufferedImage.TYPE_BYTE_BINARY, false , 0 ); Code39Bean code39Bean = new Code39Bean(); code39Bean.generateBarcode(canvas, text.trim()); canvas.finish(); BufferedImage bImage = canvas.getBufferedImage(); PDImageXObject image = JPEGFactory.createFromImage(document, bImage); contentStream.drawImage(image, x, y, 100 , 50 ); contentStream.close(); } catch (Exception e) { e.printStackTrace(); } } private static void addBarcodePdf417(PDDocument document, PDPage page, String text, float x, float y) { try { PDPageContentStream contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true ); int dpi = 300 ; BitmapCanvasProvider canvas = new BitmapCanvasProvider(dpi, BufferedImage.TYPE_BYTE_BINARY, false , 0 ); PDF417Bean pdf417Bean = new PDF417Bean(); pdf417Bean.generateBarcode(canvas, text.trim()); canvas.finish(); BufferedImage bImage = canvas.getBufferedImage(); PDImageXObject image = JPEGFactory.createFromImage(document, bImage); contentStream.drawImage(image, x, y, 100 , 50 ); contentStream.close(); } catch (Exception e) { e.printStackTrace(); } } private static void addQRCode(PDDocument document, PDPage page, String text, float x, float y) { try { PDPageContentStream contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true ); Map<encodehinttype, object= "" > hintMap = new HashMap<encodehinttype, object= "" >(); hintMap.put(EncodeHintType.MARGIN, 0 ); hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); BitMatrix matrix = new MultiFormatWriter().encode( new String(text.getBytes( "UTF-8" ), "UTF-8" ), BarcodeFormat.QR_CODE, 100 , 100 , hintMap); MatrixToImageConfig config = new MatrixToImageConfig( 0xFF000001 , 0xFFFFFFFF ); BufferedImage bImage = MatrixToImageWriter.toBufferedImage(matrix, config); PDImageXObject image = JPEGFactory.createFromImage(document, bImage); contentStream.drawImage(image, x, y, 75 , 75 ); contentStream.close(); } catch (Exception e) { e.printStackTrace(); } } } </encodehinttype,></encodehinttype,> |
All one can think and do in a short time is to think what one already knows and to do as one has always done!
No comments:
Post a Comment
NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.