惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Engineering at Meta
Engineering at Meta
G
Google Developers Blog
WordPress大学
WordPress大学
M
MIT News - Artificial intelligence
D
DataBreaches.Net
云风的 BLOG
云风的 BLOG
爱范儿
爱范儿
Microsoft Security Blog
Microsoft Security Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Blog — PlanetScale
Blog — PlanetScale
T
Tailwind CSS Blog
S
SegmentFault 最新的问题
阮一峰的网络日志
阮一峰的网络日志
博客园 - 三生石上(FineUI控件)
酷 壳 – CoolShell
酷 壳 – CoolShell
Recent Announcements
Recent Announcements
T
The Blog of Author Tim Ferriss
I
InfoQ
MyScale Blog
MyScale Blog
V
V2EX
B
Blog
罗磊的独立博客
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - woody.wu

开发我的第一个openclaw插件,接入本地查询客户信息的api接口。 在Ubuntu 24.04中安装openclaw全过程 配置qwen-code实现AI智能体编程,从此轻松让AI帮你写代码 AI写代码-window11安装claude code,以及在vs code中使用插件。 sqlserver 日志空间太满,导致磁盘空间不够的处理 在c#中利用MagickImage来实现图片的留白清除功能 利用扣子创建AI智能体大合集,智能体中包含有调用外部接口、意图识别、图片生成、大模型调用等 .NET8作为VUE打包项目宿主部署的配置 myql 8.0 安装随笔 SkiaSharp 在 Linux 上依赖一些系统级的库,需要确保这些库已经安装 2024 年年终总结 Quartz.NET学习笔记四->批量处理JOB 不同的JOB不同的时间执行不同的事情 真正实现可以编辑选择的下拉框--jquery 实现 jquery 实现图片轮换功能 - woody.wu - 博客园 在SQL Navigator 中做 oracle pl/sql SQL分析 [instant message]用asp.net+ jquery实现comet即时消息机制 ORACLE DBA学习笔记--使用LOGON TRIGGER限制用户登陆 ORACLE DBA学习笔记--管理归档日志文件 [简单分页]C#+JQUERY+ORACLE分页效果
使用spire.pdf绘制表格,当表格的内容超过一定长度会触发Inde...
woody.wu · 2025-07-24 · via 博客园 - woody.wu

最近在写代码时,发现代码报错,原来在spire.pdf绘制表格的时候,如果某个字段的内容超过一定的长度就会出现index and length must refer to a location within the string的报错

PdfGrid grid = new PdfGrid();

//设置单元格边距和表格默认字体
grid.Style.CellPadding = new PdfPaddings(1, 1, 1, 1);

Font font = new Font("Arial", 8, FontStyle.Regular, GraphicsUnit.Point, 1, true);
grid.Style.Font = new PdfTrueTypeFont(new Font("Arial Unicode MS", 8f), true);
//10列的表格
grid.Columns.Add(11);
//设置列宽
foreach (PdfGridColumn col in grid.Columns)
{
    col.Width = 45f;

PdfPageBase page = sec.Pages.Add();  

grid.Draw(page, new PointF(0, y));
当表格中某一列内容出现多种字符的时候,会出现bug

foreach (PdfGridColumn col in grid.Columns)
{
   // col.Width = 45f;

 row1 = grid.Rows.Add();
 row1.Cells[0].Value = minMonthAssCode.Substring(0, 4) + "-" + minMonthAssCode.Substring(4, 2);
 row1.Cells[0].StringFormat = new PdfStringFormat(PdfTextAlignment.Center);
 row1.Cells[0].Style.BackgroundBrush = PdfBrushes.White;
 string voucherDesc = drV["voucher_desc"].ToString();
 row1.Cells[1].Value = voucherDesc; //voucherDesc 这个变量如果出现多种字符就容易报错
 row1.Cells[1].StringFormat = new PdfStringFormat(PdfTextAlignment.Center);
 row1.Cells[1].Style.BackgroundBrush = PdfBrushes.White;

 row1.Cells[2].ColumnSpan = 2;
 row1.Cells[2].Value = "期初余额";
 row1.Cells[2].StringFormat = new PdfStringFormat(PdfTextAlignment.Left);
 row1.Cells[2].Style.BackgroundBrush = PdfBrushes.White;

 row1.Cells[4].Value = " ";
 row1.Cells[4].ColumnSpan = 2;
 row1.Cells[4].StringFormat = new PdfStringFormat(PdfTextAlignment.Center);
 row1.Cells[4].Style.BackgroundBrush = PdfBrushes.White;

 row1.Cells[6].Value = " ";
 row1.Cells[6].ColumnSpan = 2;
 row1.Cells[6].StringFormat = new PdfStringFormat(PdfTextAlignment.Center);
 row1.Cells[6].Style.BackgroundBrush = PdfBrushes.White;

 row1.Cells[8].Value = amt_begin_dir;
 row1.Cells[8].StringFormat = new PdfStringFormat(PdfTextAlignment.Center);
 row1.Cells[8].Style.BackgroundBrush = PdfBrushes.White;

 row1.Cells[9].Value = (amt_begin_dir == "借" ? amt_begin_dr.ToString("n2") : amt_begin_dir == "贷" ? amt_begin_cr.ToString("n2") : " ");
 row1.Cells[9].ColumnSpan = 2;
 row1.Cells[9].StringFormat = new PdfStringFormat(PdfTextAlignment.Right);
 row1.Cells[9].Style.BackgroundBrush = PdfBrushes.White;

经过几天调试发现,这个指定列宽导致上面的问题出现,注释后就再也没报错了。怀疑这个spire.pdf 在计算列宽的时候,自动切割出错。因为我的内容里面包含多种字符,比如汉字,特殊字符,全角半角的大小括号。所幸终于解决了。