【Java】批量生成條形碼-itextpdf

批量生成條形碼
Controller

@ApiOperation("商品一覽批量生成商品條形碼")@PostMapping("/batchGenerateProdBarCode")public void batchGenerateProdBarCode(@RequestBody ProductListCondition productListCondition,HttpServletResponse response){importExportService.batchGenerateProdBarCode(response, productListCondition);}

Service

 /*** 商品一覽批量生成商品條形碼* @param productListCondition* @return*/public void batchGenerateProdBarCode(HttpServletResponse response,ProductListCondition productListCondition){List<MProductEx> productExList = mProductListDao.selectmproduct(productListCondition.getProdCdDis(),productListCondition.getBrand());try {int dataCount = productExList.stream().filter(f -> StringUtils.isNotEmpty(f.getProdLabel())).collect(Collectors.toList()).size();if(dataCount == 0){response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "沒有要生成的條碼請調整查詢條件后重新生成");}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, "沒有要生成的條碼請調整查詢條件后重新生成");}}catch (Exception e){e.printStackTrace();}}
/*** 批量生成商品條形碼pdf文件導出適配條碼打印機** @param productExList 條碼數據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(120F, 85F), 10, 2, 10, 2);PdfWriter writer = PdfWriter.getInstance(document, os);document.open();PdfContentByte cb = writer.getDirectContent();BaseFont bfChinese = BaseFont.createFont("C:\\Windows\\Fonts\\simsun.ttc,1",  BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);Font fontChinese = new Font(bfChinese, 2, Font.NORMAL);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, "商品編號不能為中文且不能為空");}document.newPage();//創建一個一列的表格PdfPTable headerTable = new PdfPTable(1);headerTable.setWidthPercentage(70.0F);PdfPCell rightCell = new PdfPCell();Image codeImage = BarcodeUtil.generateBarcodePrefixSuffix(cb, p.getProdLabel(), 22f, 6f);Phrase imageP = new Phrase("", fontChinese);//自己調整偏移值 主要是負值往下imageP.add(new Chunk(codeImage, 5, -1));String textNmShow ="商品名稱:" + (StringUtils.isNotEmpty(p.getProdNm()) ? p.getProdNm() : "");String textUnitShow ="計量單位:" + (StringUtils.isNotEmpty(p.getUnitNm()) ? p.getUnitNm() : "") ;String textSpecShow ="規格: " + (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(26.0f);//rightCell.setPadding(4.0f);//填充headerTable.addCell(rightCell);
//                headerTable.setSplitLate(false);
//                headerTable.setSplitRows(true);document.add(headerTable);}//document.add(headerTable);os.flush();} catch (DocumentException e) {e.printStackTrace();} finally {if(Objects.nonNull(document)){document.close();}if (Objects.nonNull(os)) {os.close();}}}/*** 批量生成商品條形碼pdf文件導出適配A4紙** @param productExList 條碼數據x信息* @param os    輸出流* @throws IOException*/public static void generateProdBarcodeA4PDF(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();//判斷列,一條數據只允許單列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);//創建一個兩列的表格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 ="規格: " + (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();}}}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/41360.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/41360.shtml
英文地址,請注明出處:http://en.pswp.cn/news/41360.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

使用Spring-Security后,瀏覽器不能緩存的問題

Spring-Security在默認情況下是不允許客戶端進行緩存的&#xff0c;在使用時可以通過禁用Spring-Security中的cacheControl配置項允許緩存。 protected void configure(HttpSecurity http) throws Exception {// 允許緩存配置http.headers().cacheControl().disable(); }

Java中使用流將兩個集合根據某個字段進行過濾去重?

Java中使用流將兩個集合根據某個字段進行過濾去重? 在Java中&#xff0c;您可以使用流(Stream)來過濾和去重兩個集合。下面是一個示例代碼&#xff0c;展示如何根據對象的某個字段進行過濾和去重操作&#xff1a; import java.util.ArrayList; import java.util.List; impor…

“代碼馭寵而行“:探索Python的魔法世界,開啟編程奇幻之旅!

文章目錄 &#x1f340;引言&#x1f340;第一步&#xff1a;安裝Python和開發環境&#x1f340;第二步&#xff1a;掌握基本語法&#x1f340;第三步&#xff1a;使用Python庫和模塊&#x1f340;第四步&#xff1a;實踐項目和練習&#x1f340;第五步&#xff1a;學習進階主題…

origin修改默認的字體等

因為默認是中文宋體&#xff0c;每次切換成英文尤其是希臘字母就很麻煩。 選擇菜單欄的【設置】——【選項】點擊。 彈出窗口中選擇【文本字體】 設置成你需要的字體就好。 這里同樣可以更改頁面、圖等的默認設置。 效果&#xff1a; 選擇插入文字后&#xff0c;自動更改成…

銀河麒麟V10 達夢安裝教程

安裝前先準備要安裝包&#xff0c;包需要需要區分X86和arm架構。 版本為&#xff1a;dm8_20230419_FTarm_kylin10_sp1_64.iso 達夢數據庫下載地址&#xff1a; https://www.aliyundrive.com/s/Qm7Es5BQM5U 第一步創建用戶 su - root 1. 創建安裝用戶組 dminstall。 groupad…

前饋神經網絡優化器

引用的知乎上的文章內容&#xff0c;現在有些地方還不太明白&#xff0c;留待以后查看。 import math import numpy as np import matplotlib.pyplot as pltRATIO 3 # 橢圓的長寬比 LIMIT 1.2 # 圖像的坐標軸范圍class PlotComparaison(object):"""多種優化…

Python爬蟲的應用場景與技術難點:如何提高數據抓取的效率與準確性

作為專業爬蟲程序員&#xff0c;我們在數據抓取過程中常常面臨效率低下和準確性不高的問題。但不用擔心&#xff01;本文將與大家分享Python爬蟲的應用場景與技術難點&#xff0c;并提供一些實際操作價值的解決方案。讓我們一起來探索如何提高數據抓取的效率與準確性吧&#xf…

python3實現線性規劃求解

Background 對于數學規劃問題&#xff0c;有很多的實現。MatlabYALMIPCPLEX這個組合應該是比較主流的&#xff0c;尤其是在電力相關系統中占據著比較重要的地位。MATLAB是一個強大的數值計算工具&#xff0c;用于數學建模、算法開發和數據分析。Yalmip是一個MATLAB工具箱&#…

MongoDB:MySQL,Redis,ES,MongoDB的應用場景

簡單明了說明MySQL,ES,MongoDB的各自特點,應用場景,以及MongoDB如何使用的第一章節. 一. SQL與NoSQL SQL被稱為結構化查詢語言.是傳統意義上的數據庫,數據之間存在很明確的關聯關系,例如主外鍵關聯,這種結構可以確保數據的完整性(數據沒有缺失并且正確).但是正因為這種嚴密的結…

神經網絡基礎-神經網絡補充概念-34-正則化

概念 正則化是一種用于控制模型復雜度并防止過擬合的技術&#xff0c;在機器學習和深度學習中廣泛應用。它通過在損失函數中添加一項懲罰項來限制模型的參數&#xff0c;從而使模型更傾向于選擇簡單的參數配置。 理解 L1 正則化&#xff08;L1 Regularization&#xff09;&a…

數據分析 | Boosting與Bagging的區別

Boosting與Bagging的區別 Bagging思想專注于降低方差&#xff0c;操作起來較為簡單&#xff0c;而Boosting思想專注于降低整體偏差來降低泛化誤差&#xff0c;在模型效果方面的突出表現制霸整個弱分類器集成的領域。具體區別體現在如下五點&#xff1a; 弱評估器&#xff1a;Ba…

vb數控加工技術教學素材資源庫的設計和構建

摘 要 20世紀以來,社會生產力迅速發展,科學技術突飛猛進,人們進行信息交流的深度與廣度不斷增加,信息量急劇增長,傳統的信息處理與決策的手段已不能適應社會的需要,信息的重要性和信息處理問題的緊迫性空前提高了,面對著日益復雜和不斷發展,變化的社會環境,特別是企業…

Windows上使用dump文件調試

dump文件 dump文件記錄當前程序運行某一時刻的信息&#xff0c;包括內存&#xff0c;線程&#xff0c;線程棧&#xff0c;變量等等&#xff0c;相當于調試程序時運行到某個斷點上&#xff0c;把程序運行的信息記錄下來。可以通過Windbg打開dump&#xff0c;查看程序運行的變量…

mysql 修改存儲路徑,重啟失敗授權

目錄 停掉mysql修改mysql 配置文件my.cnf目錄授權重啟mysql 停掉mysql 修改mysql 配置文件my.cnf 更改mysql 存儲位置 到/data/mysql_data目錄下&#xff1a; datadir/data/mysql/mysql_data/socket/data/mysql/mysql_data/mysql.sockmysql 默認路么徑在 /var/lib/mysql/ 防止…

go_并發編程(1)

go并發編程 一、 并發介紹1&#xff0c;進程和線程2&#xff0c;并發和并行3&#xff0c;協程和線程4&#xff0c;goroutine 二、 Goroutine1&#xff0c;使用goroutine1&#xff09;啟動單個goroutine2&#xff09;啟動多個goroutine 2&#xff0c;goroutine與線程3&#xff0…

在 React 中獲取數據的6種方法

一、前言 數據獲取是任何 react 應用程序的核心方面。對于 React 開發人員來說&#xff0c;了解不同的數據獲取方法以及哪些用例最適合他們很重要。 但首先&#xff0c;讓我們了解 JavaScript Promises。 簡而言之&#xff0c;promise 是一個 JavaScript 對象&#xff0c;它將…

Python Web:Django、Flask和FastAPI框架對比

原文&#xff1a;百度安全驗證 Django、Flask和FastAPI是Python Web框架中的三個主要代表。這些框架都有著各自的優點和缺點&#xff0c;適合不同類型和規模的應用程序。 1. Django&#xff1a; Django是一個全功能的Web框架&#xff0c;它提供了很多內置的應用程序和工具&am…

排序+運算>直接運算的效率的原因分析

大家好,我是愛編程的喵喵。雙985碩士畢業,現擔任全棧工程師一職,熱衷于將數據思維應用到工作與生活中。從事機器學習以及相關的前后端開發工作。曾在阿里云、科大訊飛、CCF等比賽獲得多次Top名次。現為CSDN博客專家、人工智能領域優質創作者。喜歡通過博客創作的方式對所學的…

ADIS16470和ADIS16500從到手到讀出完整數據,附例程

由于保密原因&#xff0c;不能上傳我這邊的代碼&#xff0c;我所用的開發環境是IAR&#xff0c; 下邊轉載別的博主的文章&#xff0c;他用的是MDK 下文的博主給了你一個很好的思路&#xff0c;特此提出表揚 最下方是我做的一些手冊批注&#xff0c;方便大家了解這個東西 原文鏈…

如何利用 ChatGPT 進行自動數據清理和預處理

推薦&#xff1a;使用 NSDT場景編輯器助你快速搭建可二次編輯的3D應用場景 ChatGPT 已經成為一把可用于多種應用的瑞士軍刀&#xff0c;并且有大量的空間將 ChatGPT 集成到數據科學工作流程中。 如果您曾經在真實數據集上訓練過機器學習模型&#xff0c;您就會知道數據清理和預…