嵌套表格,即在一张表格中的特定单元格中再插入一个或者多个表格,使用嵌套表格的优点在于能够让内容的布局更加合理,同时也方便程序套用。下面的示例中,将介绍如何通过C#编程来演示如何插入嵌套表格到PDF文档。

要点概括:

1. 插入嵌套表格

2. 插入文字到嵌套表格

3. 插入图片到嵌套表格


使用工具

注:

1.这里使用的版本为4.9.7,经测试,对于代码中涉及的PdfGridCellContentList类和PdfGridCellContent类仅在使用该版本或者以上版本可用。使用时,请注意版本信息。

2.下载安装后,在编辑代码时,请注意添加引用Spire.Pdf.dll(dll文件可在安装路径下的Bin文件夹下获取)

C# 在PDF中插入嵌套表格-LMLPHP

示例代码(供参考)


  1. using Spire.Pdf;
  2. using Spire.Pdf.Graphics;
  3. using Spire.Pdf.Grid;
  4. using System.Drawing;
  5. using System.Windows.Forms;
  6. using System;

  7. namespace NestedTable_PDF
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             //实例化PdfDocument类,并添加页面到新建的文档
  14.             PdfDocument pdf = new PdfDocument();
  15.             PdfPageBase page = pdf.Pages.Add();

  16. //添加字体、画笔,写入文本到PDF文档
  17.             PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("行楷", 11f), true);
  18.             PdfPen pen = new PdfPen(Color.Gray);
  19.             string text = "2018 Pyeongchang Olympic Winter Games Medal Ranking";
  20.             page.Canvas.DrawString(text, font, pen, 100, 50);

  21.             //创建一个PDF表格,并添加两行
  22.             PdfGrid grid = new PdfGrid();
  23.             PdfGridRow row1 = grid.Rows.Add();
  24.             PdfGridRow row2 = grid.Rows.Add();

  25.             //设置表格的单元格内容和边框之间的上、下边距
  26.             grid.Style.CellPadding.Top = 5f;
  27.             grid.Style.CellPadding.Bottom = 5f;

  28.             //添加三列,并设置列宽
  29.             grid.Columns.Add(3);
  30.             grid.Columns[0].Width = 120f;
  31.             grid.Columns[1].Width = 150f;
  32.             grid.Columns[2].Width = 120f;

  33.             //创建一个一行两列的嵌套表格
  34.             PdfGrid embedGrid1 = new PdfGrid();
  35.             PdfGridRow newRow = embedGrid1.Rows.Add();
  36.             embedGrid1.Columns.Add(2);

  37.             //设置嵌套表格的列宽
  38.             embedGrid1.Columns[0].Width = 50f;
  39.             embedGrid1.Columns[1].Width = 60f;

  40.             //初始化SizeF类,设置图片大小
  41.             SizeF imageSize = new SizeF(45, 35);
  42.             //实例化PdfGridCellContentList、PdfGridCellContent类,加载添加到嵌套表格的图片
  43.             PdfGridCellContentList contentList = new PdfGridCellContentList();
  44.             PdfGridCellContent content = new PdfGridCellContent();
  45.             content.Image = PdfImage.FromFile("1.png");
  46.             content.ImageSize = imageSize;
  47.             contentList.List.Add(content);
  48.             //实例化PdfStringFormat、PdfTrueTypeFont类,设置单元格文字对齐方式、字体、字号等
  49.             PdfStringFormat stringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);

  50.             //设置嵌套表格的单元格的值,并应用格式
  51.             newRow.Cells[0].Value = "Norway";
  52.             newRow.Cells[0].StringFormat = stringFormat;
  53.             newRow.Cells[1].Value = contentList; //将图片添加到嵌套表格的第二个单元格
  54.             newRow.Cells[1].StringFormat = stringFormat;

  55.             //设置第一个表格的单元格的值和格式
  56.             row1.Cells[0].Value = "Rank";
  57.             row1.Cells[0].StringFormat = stringFormat;
  58.             row1.Cells[0].Style.Font = font;
  59.             row1.Cells[0].Style.BackgroundBrush = PdfBrushes.LightSalmon;
  60.             row1.Cells[1].Value = "Country";
  61.             row1.Cells[1].StringFormat = stringFormat;
  62.             row1.Cells[1].Style.Font = font;
  63.             row1.Cells[1].Style.BackgroundBrush = PdfBrushes.LightSalmon;
  64.             row1.Cells[2].Value = "Total";
  65.             row1.Cells[2].StringFormat = stringFormat;
  66.             row1.Cells[2].Style.Font = font;
  67.             row1.Cells[2].Style.BackgroundBrush = PdfBrushes.LightSalmon;

  68.             row2.Cells[0].Value = "1";
  69.             row2.Cells[0].StringFormat = stringFormat;
  70.             row2.Cells[0].Style.Font = font;
  71.             row2.Cells[1].Value = embedGrid1; //将嵌套表格添加到第一个表格的第二行第二个单元格
  72.             row2.Cells[1].StringFormat = stringFormat;

  73.             row2.Cells[2].Value = "39";
  74.             row2.Cells[2].StringFormat = stringFormat;
  75.             row2.Cells[2].Style.Font = font;

  76.             //将表格绘制到页面指定位置
  77.             grid.Draw(page, new PointF(30f, 90f));

  78.             //保存文档并打开
  79.             pdf.SaveToFile("result.pdf");
  80.             System.Diagnostics.Process.Start("result.pdf");
  81.         }
  82.     }
  83. }

代码完成后,调试程序,生成文档,嵌套表格绘制效果如下:

C# 在PDF中插入嵌套表格-LMLPHP

以上是本次C#绘制PDF嵌套表格的全部内容。

09-03 19:55
查看更多