最近BarCode128 不夠用了,隨著硬體設備(掃描器)跟標籤機增購所以QRCode也上了時程,做個JAVA iTextPDF產製QRCODE的備忘文章。
import java.util.HashMap;
import java.util.Map;
import com.itextpdf.text.pdf.qrcode.EncodeHintType;
import com.itextpdf.text.pdf.qrcode.ErrorCorrectionLevel;
import com.itextpdf.text.pdf.BarcodeQRCode;
//QRCODE
StringBuffer sb = new StringBuffer ();
sb.append ("QRCODE內容");
Map<EncodeHintType, Object> qrParam = new HashMap<EncodeHintType, Object> ();
qrParam.put ( EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M );
qrParam.put ( EncodeHintType.CHARACTER_SET, "UTF-8" );
//size_x, size_y 為QRCODE寬跟高
BarcodeQRCode qrcode = new BarcodeQRCode ( sb.toString (), size_x, size_y, qrParam );
Image qrcode_img = qrcode.getImage();
InputStream input = new FileInputStream("source.pdf");
OutputStream output= new FileOutputStream(new File("target.pdf"));
PdfReader reader = new PdfReader(input);
PdfStamper stamper = new PdfStamper(reader, output);
PdfContentByte cb = stamper.getOverContent(1);
//x,y 放置在PDF內的座標位置
qrcode_img.setAbsolutePosition(x, y);
cb.addImage(qrcode_img);
stamper.setFormFlattening(true);
stamper.close();
reader.close();
沒有留言:
張貼留言