Java?批量生成條碼的示例代碼
更新時間:2023年08月06日 16:04:21 作者:宇翔苦澀
這篇文章主要介紹了Java?批量生成條碼的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
批量生成PDF條碼
效果圖:
//調用下方接口注意編碼格式 ?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, "沒有要生成的條碼請調整查詢條件后重新生成"); ?} /** ? ? ?* 批量生成商品條形碼pdf文件 ? ? ?* ? ? ?* @param productExList 條碼數(shù)據(jù)x信息【對象集合】 ? ? ?* @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)建一個兩列的表格 ? ? ? ? ? ? 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, "商品編號不能為中文且不能為空"); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? rightCell = new PdfPCell(); ? ? ? ? ? ? ? ? Image codeImage = BarcodeUtil.generateBarcodePrefixSuffix(cb, p.getProdLabel(), 72, 24); ? ? ? ? ? ? ? ? Phrase imageP = new Phrase("", fontChinese); ? ? ? ? ? ? ? ? //自己調整偏移值 主要是負值往下 ? ? ? ? ? ? ? ? imageP.add(new Chunk(codeImage, 15, -4)); ? ? ? ? ? ? ? ? String textNmShow ="商品名稱:" + (StringUtils.isNotEmpty(p.getProdNm()) ? p.getProdNm() : ""); ? ? ? ? ? ? ? ? String textUnitShow ="計量單位:" + (StringUtils.isNotEmpty(p.getUnitNm()) ? p.getUnitNm() : "") ; ? ? ? ? ? ? ? ? String textSpecShow ="規(guī)格: " + (StringUtils.isNotEmpty(p.getSpec()) ? p.getSpec() : ""); ? ? ? ? ? ? ? ? String textModelAndUnitShow ="型號:" + (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自動換行 ? ? ? ? ? ? ? ? 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(); ? ? ? ? ? ? } ? ? ? ? } ? ? }
到此這篇關于Java 批量生成條碼的示例代碼的文章就介紹到這了,更多相關Java 批量生成條碼內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:
相關文章
Windows10系統(tǒng)下JDK1.8環(huán)境變量的配置
今天帶大家學習在Windows10系統(tǒng)下怎么配置JDK1.8環(huán)境變量,文中有非常詳細的安裝及配置教程,對正在學習的小伙伴們很有幫助,需要的朋友可以參考下2021-05-05云服務器環(huán)境搭建及部署(jdk、mysql、redis、nginx環(huán)境搭建)詳細步驟
這篇文章主要給大家介紹了關于云服務器環(huán)境搭建及部署(jdk、mysql、redis、nginx環(huán)境搭建)詳細步驟的相關資料,要在云服務器上搭建JDK、MySQL、Redis和Nginx的環(huán)境,可以按照以下步驟進行操作,需要的朋友可以參考下2024-01-01在SpringBoot中實現(xiàn)一個訂單號生成系統(tǒng)的示例代碼
在Spring Boot中設計一個訂單號生成系統(tǒng),主要考慮到生成的訂單號需要滿足的幾個要求:唯一性、可擴展性、以及可能的業(yè)務相關性,本文給大家介紹了幾種常見的解決方案及相應的示例代碼,需要的朋友可以參考下2024-02-02