1. 程式人生 > >C# 繪製PDF巢狀表格

C# 繪製PDF巢狀表格

巢狀表格,即在一張表格中的特定單元格中再插入一個或者多個表格,使用巢狀表格的優點在於能夠讓內容的佈局更加合理,同時也方便程式套用。下面的示例中,將介紹如何通過C#程式設計來演示如何插入巢狀表格到PDF文件。
要點概括:

  1. 插入巢狀表格
  2. 插入文字到巢狀表格
  3. 插入圖片到巢狀表格

使用工具

  • Spire.PDF 4.9.7
  • Visual Studio
    注:
    1.這裡使用的版本為4.9.7,經測試,對於程式碼中涉及的PdfGridCellContentList類和PdfGridCellContent類僅在使用該版本或者以上版本可用。使用時,請注意版本資訊。
    2.下載安裝後,在編輯程式碼時,請注意新增引用Spire.Pdf.dll(dll檔案可在安裝路徑下的Bin資料夾下獲取)
    C# 繪製PDF巢狀表格

    示例程式碼(供參考)

using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Grid;
using System.Drawing;
using System.Windows.Forms;
using System;

namespace NestedTable_PDF
{
    class Program
    {
        static void Main(string[] args)
        {
            //例項化PdfDocument類,並新增頁面到新建的文件
            PdfDocument pdf = new PdfDocument();
            PdfPageBase page = pdf.Pages.Add();

           //新增字型、畫筆,寫入文字到PDF文件
            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("行楷", 11f), true);
            PdfPen pen = new PdfPen(Color.Gray);
            string text = "2018 Pyeongchang Olympic Winter Games Medal Ranking";
            page.Canvas.DrawString(text, font, pen, 100, 50);

            //建立一個PDF表格,並新增兩行
            PdfGrid grid = new PdfGrid(); 
            PdfGridRow row1 = grid.Rows.Add();
            PdfGridRow row2 = grid.Rows.Add();

            //設定表格的單元格內容和邊框之間的上、下邊距
            grid.Style.CellPadding.Top = 5f;
            grid.Style.CellPadding.Bottom = 5f;

            //新增三列,並設定列寬
            grid.Columns.Add(3);
            grid.Columns[0].Width = 120f;
            grid.Columns[1].Width = 150f;
            grid.Columns[2].Width = 120f; 

            //建立一個一行兩列的巢狀表格
            PdfGrid embedGrid1 = new PdfGrid();
            PdfGridRow newRow = embedGrid1.Rows.Add();
            embedGrid1.Columns.Add(2);

            //設定巢狀表格的列寬
            embedGrid1.Columns[0].Width = 50f;
            embedGrid1.Columns[1].Width = 60f;

            //初始化SizeF類,設定圖片大小
            SizeF imageSize = new SizeF(45, 35);
            //例項化PdfGridCellContentList、PdfGridCellContent類,載入新增到巢狀表格的圖片
            PdfGridCellContentList contentList = new PdfGridCellContentList();
            PdfGridCellContent content = new PdfGridCellContent();
            content.Image = PdfImage.FromFile("1.png");
            content.ImageSize = imageSize;
            contentList.List.Add(content);
            //例項化PdfStringFormat、PdfTrueTypeFont類,設定單元格文字對齊方式、字型、字號等
            PdfStringFormat stringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);         

            //設定巢狀表格的單元格的值,並應用格式
            newRow.Cells[0].Value = "Norway";
            newRow.Cells[0].StringFormat = stringFormat;
            newRow.Cells[1].Value = contentList; //將圖片新增到巢狀表格的第二個單元格
            newRow.Cells[1].StringFormat = stringFormat;           

            //設定第一個表格的單元格的值和格式
            row1.Cells[0].Value = "Rank";
            row1.Cells[0].StringFormat = stringFormat;
            row1.Cells[0].Style.Font = font;
            row1.Cells[0].Style.BackgroundBrush = PdfBrushes.LightSalmon;
            row1.Cells[1].Value = "Country";
            row1.Cells[1].StringFormat = stringFormat;
            row1.Cells[1].Style.Font = font;
            row1.Cells[1].Style.BackgroundBrush = PdfBrushes.LightSalmon;
            row1.Cells[2].Value = "Total";
            row1.Cells[2].StringFormat = stringFormat;
            row1.Cells[2].Style.Font = font;
            row1.Cells[2].Style.BackgroundBrush = PdfBrushes.LightSalmon;

            row2.Cells[0].Value = "1";
            row2.Cells[0].StringFormat = stringFormat;
            row2.Cells[0].Style.Font = font;
            row2.Cells[1].Value = embedGrid1; //將巢狀表格新增到第一個表格的第二行第二個單元格
            row2.Cells[1].StringFormat = stringFormat;

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

            //將表格繪製到頁面指定位置
            grid.Draw(page, new PointF(30f, 90f));

            //儲存文件並開啟
            pdf.SaveToFile("result.pdf");
            System.Diagnostics.Process.Start("result.pdf");
        }
    }
}

程式碼完成後,除錯程式,生成文件,巢狀表格繪製效果如下:
C# 繪製PDF巢狀表格
以上是本次C#繪製PDF巢狀表格的全部內容。
示例拓展:

本文完