如何在Node.js的PDFKIT中显示带圈字符或将其转换为普通字符
方案2:带圈字符转换为普通字符串
如果不需要保留带圈样式,可以通过Unicode编码匹配写转换函数,批量替换所有带圈字符:
步骤1:编写转换工具函数
function convertCircledChars(input: string): string { return input.replace(/./gu, (char) => { const code = char.codePointAt(0); // 处理带圈数字 ①-⑳ 转换为1-20 if (code >= 0x2460 && code <= 0x2473) return String(code - 0x2460 + 1); // 处理带圈小写字母 ⒜-⒵ 转换为(a)-(z) if (code >= 0x249C && code <= 0x24B5) return `(${String.fromCodePoint(code - 0x249C + 97)})`; // 处理带圈大写字母 Ⓐ-Ⓩ 转换为(A)-(Z) if (code >= 0x24B6 && code <= 0x24CF) return `(${String.fromCodePoint(code - 0x24B6 + 65)})`; // 更多带圈字符范围可按需补充规则 return char; }); }
步骤2:嵌入到你的文本渲染逻辑中
修改你的text方法,所有输出文本自动经过转换:
private text(value: string, x?: number, y?: number) { const originalX = this.doc.x; // 先转换特殊字符再渲染 const processedValue = convertCircledChars(value); return this.doc.text(processedValue, x, y).text('', originalX); }
内容的提问来源于stack exchange,提问作者hans1125
相关产品推荐
相关产品推荐

