在ASP.NET MVC中用C#借助iTextHelper实现指定样式PDF表格求助
调整iText代码实现目标PDF表格样式
你的代码缺少单元格跨行合并设置,同时字体大小、对齐方式和列宽也需要调整,以下是修改后的完整代码:
PdfPTable InfoTable = new PdfPTable(4); InfoTable.WidthPercentage = 100f; InfoTable.SpacingAfter = 10.5f; // 设置列宽比例,可根据实际需求调整 InfoTable.SetWidths(new float[] { 1f, 2f, 2f, 5f }); // 表头单元格 PdfPCell InfoCellT1 = new PdfPCell(new Phrase("Head 1", getFont(12, strWebPath))); InfoCellT1.HorizontalAlignment = Element.ALIGN_CENTER; InfoCellT1.VerticalAlignment = Element.ALIGN_MIDDLE; InfoCellT1.BackgroundColor = BaseColor.BLUE; InfoCellT1.Padding = 5f; InfoCellT1.Border = PdfPCell.BOX; PdfPCell InfoCellT2 = new PdfPCell(new Phrase("Head 2", getFont(12, strWebPath))); InfoCellT2.HorizontalAlignment = Element.ALIGN_CENTER; InfoCellT2.VerticalAlignment = Element.ALIGN_MIDDLE; InfoCellT2.BackgroundColor = BaseColor.BLUE; InfoCellT2.Padding = 5f; InfoCellT2.Border = PdfPCell.BOX; PdfPCell InfoCellT3 = new PdfPCell(new Phrase("Head 3", getFont(12, strWebPath))); InfoCellT3.HorizontalAlignment = Element.ALIGN_CENTER; InfoCellT3.VerticalAlignment = Element.ALIGN_MIDDLE; InfoCellT3.BackgroundColor = BaseColor.BLUE; InfoCellT3.Padding = 5f; InfoCellT3.Border = PdfPCell.BOX; PdfPCell InfoCellT4 = new PdfPCell(new Phrase("Head 4", getFont(12, strWebPath))); InfoCellT4.HorizontalAlignment = Element.ALIGN_CENTER; InfoCellT4.VerticalAlignment = Element.ALIGN_MIDDLE; InfoCellT4.BackgroundColor = BaseColor.BLUE; InfoCellT4.Padding = 5f; InfoCellT4.Border = PdfPCell.BOX; InfoTable.AddCell(InfoCellT1); InfoTable.AddCell(InfoCellT2); InfoTable.AddCell(InfoCellT3); InfoTable.AddCell(InfoCellT4); // 第一列单元格:跨行4行 PdfPCell Info1Cell1 = new PdfPCell(new Phrase("1", getFont(10, strWebPath))); Info1Cell1.HorizontalAlignment = Element.ALIGN_CENTER; Info1Cell1.VerticalAlignment = Element.ALIGN_MIDDLE; Info1Cell1.BackgroundColor = BaseColor.WHITE; Info1Cell1.Padding = 5f; Info1Cell1.Rowspan = 4; Info1Cell1.Border = PdfPCell.BOX; InfoTable.AddCell(Info1Cell1); // 第二列单元格:跨行4行 PdfPCell Info1Cell2 = new PdfPCell(new Phrase("first", getFont(10, strWebPath))); Info1Cell2.HorizontalAlignment = Element.ALIGN_CENTER; Info1Cell2.VerticalAlignment = Element.ALIGN_MIDDLE; Info1Cell2.Padding = 5f; Info1Cell2.Rowspan = 4; Info1Cell2.Border = PdfPCell.BOX; InfoTable.AddCell(Info1Cell2); // 第三列单元格:跨行4行 PdfPCell Info1Cell3 = new PdfPCell(new Phrase("part 1", getFont(10, strWebPath))); Info1Cell3.HorizontalAlignment = Element.ALIGN_CENTER; Info1Cell3.VerticalAlignment = Element.ALIGN_MIDDLE; Info1Cell3.Padding = 5f; Info1Cell3.Rowspan = 4; Info1Cell3.Border = PdfPCell.BOX; InfoTable.AddCell(Info1Cell3); // 第四列的多行单元格,逐个添加 PdfPCell cell4Row1 = new PdfPCell(new Phrase("1 dot 1", getFont(10, strWebPath))); cell4Row1.HorizontalAlignment = Element.ALIGN_LEFT; cell4Row1.VerticalAlignment = Element.ALIGN_MIDDLE; cell4Row1.Padding = 5f; cell4Row1.Border = PdfPCell.BOX; InfoTable.AddCell(cell4Row1); PdfPCell cell4Row2 = new PdfPCell(new Phrase("2 dot 2", getFont(10, strWebPath))); cell4Row2.HorizontalAlignment = Element.ALIGN_LEFT; cell4Row2.VerticalAlignment = Element.ALIGN_MIDDLE; cell4Row2.Padding = 5f; cell4Row2.Border = PdfPCell.BOX; InfoTable.AddCell(cell4Row2); PdfPCell cell4Row3 = new PdfPCell(new Phrase("2 dot 2", getFont(10, strWebPath))); cell4Row3.HorizontalAlignment = Element.ALIGN_LEFT; cell4Row3.VerticalAlignment = Element.ALIGN_MIDDLE; cell4Row3.Padding = 5f; cell4Row3.Border = PdfPCell.BOX; InfoTable.AddCell(cell4Row3); PdfPCell cell4Row4 = new PdfPCell(new Phrase("3 dot 3", getFont(10, strWebPath))); cell4Row4.HorizontalAlignment = Element.ALIGN_LEFT; cell4Row4.VerticalAlignment = Element.ALIGN_MIDDLE; cell4Row4.Padding = 5f; cell4Row4.Border = PdfPCell.BOX; InfoTable.AddCell(cell4Row4);
关键修改说明
- 列宽设置:通过
SetWidths定义各列宽度比例,适配不同内容的显示需求 - 跨行合并:为第一、二、三列的单元格设置
Rowspan = 4,使其对应第四列的4行内容 - 垂直对齐:添加
VerticalAlignment = Element.ALIGN_MIDDLE,确保合并单元格内的内容垂直居中 - 字体调整:将字体大小从3改为10,避免文字过小无法识别
- 边框与内边距:统一设置
Border = PdfPCell.BOX和一致的内边距,保证表格样式规整
内容的提问来源于stack exchange,提问作者cr7oo
相关产品推荐
相关产品推荐

