Java?批量生成條碼的示例代碼
更新時(shí)間:2023年08月06日 16:04:21 作者:宇翔苦澀
這篇文章主要介紹了Java?批量生成條碼的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
批量生成PDF條碼
效果圖:
//調(diào)用下方接口注意編碼格式 ?if(CollectionUtil.isNotEmpty(productExList)){ ? ? ?String exportFileName = URLEncoder.encode("商品條碼", "UTF-8") + DateUtil.format(new Date(), "yyyyMMddHHmmss"); ? ? ?response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + exportFileName + ".pdf"); ? ? ?response.setHeader("Access-Control-Expose-Headers", "Content-Disposition"); ? ? ?response.setHeader("content-Type", "application/pdf"); ? ? ?generateProdBarcodePDF(productExList, response.getOutputStream(),response); ?}else { ? ? ?response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "沒有要生成的條碼請(qǐng)調(diào)整查詢條件后重新生成"); ?} /** ? ? ?* 批量生成商品條形碼pdf文件 ? ? ?* ? ? ?* @param productExList 條碼數(shù)據(jù)x信息【對(duì)象集合】 ? ? ?* @param os ? ?輸出流 ? ? ?* @throws IOException ? ? ?*/ ? ? public static void generateProdBarcodePDF(List<MProductEx> productExList, OutputStream os,HttpServletResponse response) throws IOException { ? ? ? ? Document document = null; ? ? ? ? try { ? ? ? ? ? ? document = new Document(new Rectangle(283F, 425F), 10, 10, 10, 10); ? ? ? ? ? ? PdfWriter writer = PdfWriter.getInstance(document, os); ? ? ? ? ? ? document.open(); ? ? ? ? ? ? PdfContentByte cb = writer.getDirectContent(); ? ? ? ? ? ? //判斷列,一條數(shù)據(jù)只允許單列 ? ? ? ? ? ? int dataCount = productExList.stream().filter(f -> StringUtils.isNotEmpty(f.getProdLabel())).collect(Collectors.toList()).size(); ? ? ? ? ? ? int numColumns = 1; ? ? ? ? ? ? if (dataCount > 1) { ? ? ? ? ? ? ? ? numColumns = 2; ? ? ? ? ? ? } ? ? ? ? ? ? BaseFont bfChinese = BaseFont.createFont("C:\\Windows\\Fonts\\simsun.ttc,1", ?BaseFont.IDENTITY_H, ? ? ? ? ? ? ? ? ? ? BaseFont.NOT_EMBEDDED); ? ? ? ? ? ? Font fontChinese = new Font(bfChinese, 5, Font.NORMAL); ? ? ? ? ? ? //創(chuàng)建一個(gè)兩列的表格 ? ? ? ? ? ? PdfPTable headerTable = new PdfPTable(numColumns); ? ? ? ? ? ? if(numColumns == 1){ ? ? ? ? ? ? ? ? headerTable.setWidthPercentage(40.0f); ? ? ? ? ? ? }else { ? ? ? ? ? ? ? ? headerTable.setWidthPercentage(80.0f); ? ? ? ? ? ? } ? ? ? ? ? ? PdfPCell rightCell = null; ? ? ? ? ? ? Pattern pat = Pattern.compile("[\u4e00-\u9fa5]"); ? ? ? ? ? ? Matcher m = null; ? ? ? ? ? ? for (MProductEx p : productExList) { ? ? ? ? ? ? ? ? m = pat.matcher(p.getProdLabel()); ? ? ? ? ? ? ? ? if(StringUtils.isNullOrEmpty(p.getProdLabel()) || m.find()){ ? ? ? ? ? ? ? ? ? ? response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "商品編號(hào)不能為中文且不能為空"); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? rightCell = new PdfPCell(); ? ? ? ? ? ? ? ? Image codeImage = BarcodeUtil.generateBarcodePrefixSuffix(cb, p.getProdLabel(), 72, 24); ? ? ? ? ? ? ? ? Phrase imageP = new Phrase("", fontChinese); ? ? ? ? ? ? ? ? //自己調(diào)整偏移值 主要是負(fù)值往下 ? ? ? ? ? ? ? ? imageP.add(new Chunk(codeImage, 15, -4)); ? ? ? ? ? ? ? ? String textNmShow ="商品名稱:" + (StringUtils.isNotEmpty(p.getProdNm()) ? p.getProdNm() : ""); ? ? ? ? ? ? ? ? String textUnitShow ="計(jì)量單位:" + (StringUtils.isNotEmpty(p.getUnitNm()) ? p.getUnitNm() : "") ; ? ? ? ? ? ? ? ? String textSpecShow ="規(guī)格: " + (StringUtils.isNotEmpty(p.getSpec()) ? p.getSpec() : ""); ? ? ? ? ? ? ? ? String textModelAndUnitShow ="型號(hào):" + (StringUtils.isNotEmpty(p.getModel()) ? p.getModel() : "") + ? ? ? ? ? ? ? ? ? ? ? ? " ?" + textUnitShow; ? ? ? ? ? ? ? ? //Chunk chunkCd = new Chunk(textCdShow,fontChinese); ? ? ? ? ? ? ? ? Chunk chunkNm = new Chunk(textNmShow,fontChinese); ? ? ? ? ? ? ? ? //Chunk chunkUnit = new Chunk(textUnitShow,fontChinese); ? ? ? ? ? ? ? ? Chunk chunkSpec = new Chunk(textSpecShow,fontChinese); ? ? ? ? ? ? ? ? Chunk chunkModel = new Chunk(textModelAndUnitShow,fontChinese); ? ? ? ? ? ? ? ? rightCell = new PdfPCell(); ? ? ? ? ? ? ? ? //imageP.setLeading(2f,1.5f); ? ? ? ? ? ? ? ? rightCell.addElement(imageP); ? ? ? ? ? ? ? ? //rightCell.addElement(chunkCd); ? ? ? ? ? ? ? ? rightCell.addElement(new Chunk(" ?",fontChinese)); ? ? ? ? ? ? ? ? rightCell.addElement(chunkNm); ? ? ? ? ? ? ? ? //rightCell.addElement(chunkUnit); ? ? ? ? ? ? ? ? rightCell.addElement(chunkSpec); ? ? ? ? ? ? ? ? rightCell.addElement(chunkModel); ? ? ? ? ? ? ? ? //false自動(dòng)換行 ? ? ? ? ? ? ? ? rightCell.setNoWrap(false); ? ? ? ? ? ? ? ? //行間距 ? ? ? ? ? ? ? ? //rightCell.setLeading(40f,10.0f); ? ? ? ? ? ? ? ? rightCell.setHorizontalAlignment(Element.ALIGN_LEFT); ? ? ? ? ? ? ? ? rightCell.setVerticalAlignment(Element.ALIGN_MIDDLE); ? ? ? ? ? ? ? ? rightCell.setFixedHeight(70.0f); ? ? ? ? ? ? ? ? //rightCell.setPadding(4.0f);//填充 ? ? ? ? ? ? ? ? headerTable.addCell(rightCell); ? ? ? ? ? ? } ? ? ? ? ? ? if(productExList.size()%2 ==1){ ? ? ? ? ? ? ? ? rightCell = new PdfPCell(); ? ? ? ? ? ? ? ? new Chunk("END",fontChinese); ? ? ? ? ? ? ? ? headerTable.addCell(rightCell); ? ? ? ? ? ? } ? ? ? ? ? ? document.add(headerTable); ? ? ? ? ? ? os.flush(); ? ? ? ? } catch (DocumentException e) { ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? } finally { ? ? ? ? ? ? if(Objects.nonNull(document)){ ? ? ? ? ? ? ? ? document.close(); ? ? ? ? ? ? } ? ? ? ? ? ? if (Objects.nonNull(os)) { ? ? ? ? ? ? ? ? os.close(); ? ? ? ? ? ? } ? ? ? ? } ? ? }
到此這篇關(guān)于Java 批量生成條碼的示例代碼的文章就介紹到這了,更多相關(guān)Java 批量生成條碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:
相關(guān)文章
java二維數(shù)組實(shí)現(xiàn)推箱子小游戲
這篇文章主要為大家詳細(xì)介紹了java二維數(shù)組實(shí)現(xiàn)推箱子小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-11-11Windows10系統(tǒng)下JDK1.8環(huán)境變量的配置
今天帶大家學(xué)習(xí)在Windows10系統(tǒng)下怎么配置JDK1.8環(huán)境變量,文中有非常詳細(xì)的安裝及配置教程,對(duì)正在學(xué)習(xí)的小伙伴們很有幫助,需要的朋友可以參考下2021-05-05云服務(wù)器環(huán)境搭建及部署(jdk、mysql、redis、nginx環(huán)境搭建)詳細(xì)步驟
這篇文章主要給大家介紹了關(guān)于云服務(wù)器環(huán)境搭建及部署(jdk、mysql、redis、nginx環(huán)境搭建)詳細(xì)步驟的相關(guān)資料,要在云服務(wù)器上搭建JDK、MySQL、Redis和Nginx的環(huán)境,可以按照以下步驟進(jìn)行操作,需要的朋友可以參考下2024-01-01在SpringBoot中實(shí)現(xiàn)一個(gè)訂單號(hào)生成系統(tǒng)的示例代碼
在Spring Boot中設(shè)計(jì)一個(gè)訂單號(hào)生成系統(tǒng),主要考慮到生成的訂單號(hào)需要滿足的幾個(gè)要求:唯一性、可擴(kuò)展性、以及可能的業(yè)務(wù)相關(guān)性,本文給大家介紹了幾種常見的解決方案及相應(yīng)的示例代碼,需要的朋友可以參考下2024-02-02