From 06e105234f820a2e791cac3e6c7012cc8bf4d905 Mon Sep 17 00:00:00 2001
From: chenhaozhe <cgz@hz-kingdee.com>
Date: 星期三, 06 五月 2026 17:10:06 +0800
Subject: [PATCH] ZPL 打印模板 新增自动换行功能

---
 WebAPI/Controllers/WebAPIController.cs |   98 +++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 86 insertions(+), 12 deletions(-)

diff --git a/WebAPI/Controllers/WebAPIController.cs b/WebAPI/Controllers/WebAPIController.cs
index c001130..6c68f9e 100644
--- a/WebAPI/Controllers/WebAPIController.cs
+++ b/WebAPI/Controllers/WebAPIController.cs
@@ -24129,6 +24129,11 @@
             // 鍖归厤 {{ 瀛楁鍚� }} 鎵�闇�姝e垯琛ㄨ揪寮� 鏀寔涓枃
             var regex = new Regex(@"{{\s*([\u4e00-\u9fa5a-zA-Z0-9_\s]+?)\s*}}", RegexOptions.Compiled);
 
+            // 鍖归厤 鐭╁舰妗� 浣嶇疆 瀹介珮 鎵�闇�姝e垯琛ㄨ揪寮�
+            var RectRegex = new Regex(@"^\^FO(\d+),(\d+)\^GB(\d+),(\d+)", RegexOptions.Compiled);
+            // 鍖归厤 妯℃澘瀛楃涓� 鎵�鍦ㄨ 浣嶇疆 瀹介珮 鎵�闇�姝e垯琛ㄨ揪寮� 鏀寔涓枃
+            var fieldRegex = new Regex(@"\^FO(\d+),(\d+)\^A[A-Z]+,(\d+),(\d+).*\{\{\s*([\u4e00-\u9fa5a-zA-Z0-9_\s]+?)\s*}\}", RegexOptions.Compiled);
+
             if (ptMode == "ZPL")
             {
                 suffix = ".zpl";
@@ -24201,6 +24206,7 @@
                         {
                             string currentLine = lines[i];
                             string preLine = "";
+                            string concatStr = "";
                             if(i>0)
                             {
                                 // 鑾峰彇褰撳墠琛岀殑涓婁竴琛岋紝鐢ㄤ簬鍒ゆ柇鏄惁鏄煩褰㈡
@@ -24213,20 +24219,26 @@
                                 continue;
                             }
 
-
-                            // 姝e垯琛ㄨ揪寮忓尮閰� {{ 瀛楁鍚� }} 鎵�鍦ㄨ鐨勫瓧娈碉紝濡傛灉鏈夛紝鍒欒繘琛屾浛鎹紝娌℃湁锛屽垯榛樿涓虹┖瀛楃涓�
-                            currentLine = regex.Replace(currentLine, match =>
+                            // 鍒ゆ柇鐭╁舰妗嗭紝濡傛灉鍓嶄竴琛屾槸鐭╁舰妗嗭紝鍒欓渶鍒ゆ柇璇ヨ瀛楃涓查暱搴︽槸鍚﹁秴闀�
+                            if(!getConcatStr(rectRegex: RectRegex, fieldRegex: fieldRegex,
+                                PreLine: preLine, CurrLine: currentLine, fieldObject: item, ref concatStr))
                             {
-                                string fieldName = match.Groups[1].Value.Trim();
-                                if (msg != null && item.ContainsKey(fieldName))
+                                // 姝e垯琛ㄨ揪寮忓尮閰� {{ 瀛楁鍚� }} 鎵�鍦ㄨ鐨勫瓧娈碉紝濡傛灉鏈夛紝鍒欒繘琛屾浛鎹紝娌℃湁锛屽垯榛樿涓虹┖瀛楃涓�
+                                concatStr = regex.Replace(currentLine, match =>
                                 {
-                                    return item[fieldName]?.ToString() ?? "";
-                                }
-                                return "";
-                            });
-                            // 灏嗘墍鏈� \n 鐨勬崲琛岀 鏇挎崲涓� \r\n
+                                    string fieldName = match.Groups[1].Value.Trim();
+                                    if (msg != null && item.ContainsKey(fieldName))
+                                    {
+                                        return item[fieldName]?.ToString() ?? "";
+                                    }
+                                    return "";
+                                });
+                            }
 
-                            sb.Append(currentLine);
+
+                            
+                            // 灏嗘墍鏈� \n 鐨勬崲琛岀 鏇挎崲涓� \r\n
+                            sb.Append(concatStr);
                             sb.Append("\r\n"); // 鏍囧噯鎹㈣
                         }
                     }
@@ -24251,6 +24263,69 @@
             }
 
         }
+
+        private bool getConcatStr(Regex rectRegex, Regex fieldRegex,
+            string PreLine, string CurrLine, JObject fieldObject, ref string concatStr)
+        {
+            var rectRegexResult = rectRegex.Match(PreLine);
+            var fieldRegexResult = fieldRegex.Match(CurrLine);
+            if (!rectRegexResult.Success)
+            {
+                return false;
+            }
+
+            if(!fieldRegexResult.Success)
+            {
+                return false;
+            }
+
+            // 鐭╁舰 浣嶇疆 瀹介珮
+            int rectLocationX = int.Parse(rectRegexResult.Groups[1].Value);
+            int rectLocationY = int.Parse(rectRegexResult.Groups[2].Value);
+            int rectWidth = int.Parse(rectRegexResult.Groups[3].Value);
+            int rectHeight = int.Parse(rectRegexResult.Groups[4].Value);
+
+            // 妯℃澘瀛楃涓� 浣嶇疆 瀛楃瀹介珮
+            int fieldLocationX = int.Parse(fieldRegexResult.Groups[1].Value);
+            int fieldLocationY = int.Parse(fieldRegexResult.Groups[2].Value);
+            int fieldWidth = int.Parse(fieldRegexResult.Groups[3].Value);
+            int fieldHeight = int.Parse(fieldRegexResult.Groups[4].Value);
+            string fieldName = fieldRegexResult.Groups[5].Value;
+
+            string fieldValue = fieldObject[fieldName].ToString() ?? "";
+            if(string.IsNullOrWhiteSpace(fieldValue))
+            {
+                return false;
+            }
+
+            if(fieldValue.Length * fieldWidth > rectWidth)
+            {
+                // 瀛楃涓查暱搴﹀ぇ浜庣煩褰㈢殑鏈�澶ч暱搴� 鍒欐枃鏈渶瑕佷粠鐭╁舰鐨刌浣嶇疆寮�濮嬫覆鏌�
+                fieldLocationY = rectLocationY;
+                // 鑾峰彇鎹㈣鎵�闇�鐨勮鏁�
+                int lineCount = int.Parse(Math.Ceiling((float)fieldValue.Length / fieldWidth).ToString());
+                
+                // 璁$畻鎹㈣鍚庣殑鏈�澶ц鏁� * 瀛楃楂樺害 鏄惁澶т簬鐭╁舰鏈�澶ч珮搴�
+                if(lineCount * fieldHeight > rectHeight)
+                {
+                    fieldWidth = int.Parse(Math.Floor((float)fieldHeight / lineCount).ToString());
+                    fieldHeight = int.Parse(Math.Floor((float)fieldHeight / lineCount).ToString());
+                }
+
+                for(uint i=0;i<lineCount; i++)
+                {
+                    concatStr += fieldRegex.Replace(CurrLine, match =>
+                    {
+                        return "";
+                    });
+                }
+
+            }
+
+            return true;
+
+        }
+        #endregion
         [HttpGet]
         [Route("Web/getSysParameter")]
         public object getSysParameter(string HClientID)
@@ -24296,4 +24371,3 @@
         }
     }
 }
-#endregion
\ No newline at end of file

--
Gitblit v1.9.1